博客
关于我
Nginx使用之location和rewrite用法
阅读量:374 次
发布时间:2019-03-05

本文共 3323 字,大约阅读时间需要 11 分钟。

Nginx配置指南:location和rewrite的使用

1. 常见的Nginx正则表达式

Nginx中常用的正则表达式符号及其含义:

  • ^:匹配输入字符串的起始位置
  • $:匹配输入字符串的结束位置
  • *****:匹配前面的字符零次或多次
  • +:匹配前面的字符一次或多次
  • ?:匹配前面的字符零次或一次
  • [abc]:匹配单个字符a、b、c中的一个
  • [a-zA-Z0-9]:匹配字母和数字
  • ():用于包围表达式
  • |:表示或运算符

2. location的使用

2.1 location的分类

  • 精准匹配location = / { ... }:只匹配根路径
  • 一般匹配location / { ... }:匹配所有路径
  • 正则匹配location ~ / { ... }:结合正则表达式进行匹配

2.2 location的常用规则

  • 精确匹配location = / { ... }:严格匹配根路径
  • 前缀匹配^~ /:表示普通字符匹配
  • 区分大小写匹配~ /:与^~类似但区分大小写
  • 不区分大小写匹配~* /:与~类似但不区分大小写
  • 非匹配!~ /:与~类似但匹配非指定路径
  • 不区分大小写非匹配!~* /:与~*类似但匹配非指定路径

2.3 location的优先级

优先级从高到低依次为:

  • 精准匹配(location = /
  • 一般路径匹配(location /
  • 前缀匹配(^~ /
  • 正则匹配(~ /~* /
  • 部分起始路径匹配
  • 2.4 location的示例

  • 精准匹配location = / { proxy_pass http://tomcat_server/; }:只匹配根路径,所有其他路径不匹配
  • 一般路径匹配location / { proxy_pass http://tomcat_server/; }:匹配所有路径
  • 正则匹配location ~ /images/ { proxy_pass http://backend/; }:匹配以/images/开头的路径
  • 混合使用location /static/ { root /webroot/static/; } location ~* \.(html|gif|jpg|jpeg|png|css|js|ico)$ { root /webroot/res/; }:分别处理静态文件和其他文件类型
  • 3. rewrite的使用

    3.1 rewrite的作用

    • URL重写:根据正则表达式重组或重定向URL
    • 支持全局变量:如$host$request_uri
    • 支持标记:如lastbreakredirectpermanent

    3.2 rewrite的语法

    rewrite regex replacement [flag];
    • regex:正则表达式
    • replacement:重写后的内容
    • flag:标记(可选)

    3.3 rewrite的示例

    基于域名的跳转

    server {    listen       80;    server_name  www.zhangsan.com;    charset utf-8;    access_log  /var/log/nginx/www.zhangsan.com.access.log;    location / {        if ($host = 'www.zhangsan.com') {            rewrite ^/(.*)$ http://www.lisi.com/$1 permanent;        }        root   html;        index  index.html index.htm;    }}

    基于客户端IP的跳转

    server {    listen       80;    server_name  www.zhangsan.com;    charset utf-8;    access_log  /var/log/nginx/bbs.zhangsan.com.access.log;    if ($remote_addr = "192.168.172.10") {        set $rewrite false;    }    if ($rewrite = true) {        rewrite (.+) /weihu.html;    }    location /weihu.html {        root /var/www/html;    }    location / {        root   html;        index  index.html index.htm;    }}

    基于旧域名跳转到新域名下目录

    server {    listen       80;    server_name  bbs.zhangsan.com;    charset utf-8;    access_log  /var/log/nginx/bbs.zhangsan.com.access.log;    location /post {        rewrite (.+) http://www.zhangsan.com/bbs/$1 permanent;    }    location / {        root   html;        index  index.html index.htm;    }}

    基于参数匹配的跳转

    server {    listen       80;    server_name  www.zhangsan.com;    charset utf-8;    access_log  /var/log/nginx/www.zhangsan.com.access.log;    if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {        rewrite (.+) http://www.zhangsan.com permanent;    }    location / {        root   html;        index  index.html index.htm;    }}

    基于目录下所有PHP文件的跳转

    server {    listen       80;    server_name  www.zhangsan.com;    charset utf-8;    access_log  /var/log/nginx/www.zhangsan.com.access.log;    location ~* /upload/.*\.php$ {        rewrite (.+) http://www.zhangsan.com permanent;    }    location / {        root   html;        index  index.html index.htm;    }}

    4. 配置示例

    4.1 静态文件配置

    location ^~ /static/ {    root /webroot/static/;}location ~* \.(html|gif|jpg|jpeg|png|css|js|ico)$ {    root /webroot/res/;}

    4.2 动态文件配置

    location / {    proxy_pass http://tomcat_server;}

    5. 优先级总结

    优先级从高到低依次为:

  • location = /
  • location /
  • location ^~ /
  • location ~ /location ~* /
  • location 正则匹配
  • 通过合理配置locationrewrite,可以实现对网站请求的精细化控制和重写,提升Nginx的性能和功能。

    转载地址:http://xzag.baihongyu.com/

    你可能感兴趣的文章
    MySQL 数据类型和属性
    查看>>
    mysql 敲错命令 想取消怎么办?
    查看>>
    Mysql 整形列的字节与存储范围
    查看>>
    mysql 断电数据损坏,无法启动
    查看>>
    MySQL 日期时间类型的选择
    查看>>
    Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
    查看>>
    MySQL 是如何加锁的?
    查看>>
    MySQL 是怎样运行的 - InnoDB数据页结构
    查看>>
    mysql 更新子表_mysql 在update中实现子查询的方式
    查看>>
    MySQL 有什么优点?
    查看>>
    mysql 权限整理记录
    查看>>
    mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
    查看>>
    MYSQL 查看最大连接数和修改最大连接数
    查看>>
    MySQL 查看有哪些表
    查看>>
    mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
    查看>>
    MySql 查询以逗号分隔的字符串的方法(正则)
    查看>>
    MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
    查看>>
    mysql 查询数据库所有表的字段信息
    查看>>
    【Java基础】什么是面向对象?
    查看>>
    mysql 查询,正数降序排序,负数升序排序
    查看>>