博客
关于我
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/

    你可能感兴趣的文章
    mysql8的安装与卸载
    查看>>
    MySQL8,体验不一样的安装方式!
    查看>>
    MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
    查看>>
    Mysql: 对换(替换)两条记录的同一个字段值
    查看>>
    mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
    查看>>
    MYSQL:基础——3N范式的表结构设计
    查看>>
    MYSQL:基础——触发器
    查看>>
    Mysql:连接报错“closing inbound before receiving peer‘s close_notify”
    查看>>
    mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
    查看>>
    mysqldump 参数--lock-tables浅析
    查看>>
    mysqldump 导出中文乱码
    查看>>
    mysqldump 导出数据库中每张表的前n条
    查看>>
    mysqldump: Got error: 1044: Access denied for user ‘xx’@’xx’ to database ‘xx’ when using LOCK TABLES
    查看>>
    Mysqldump参数大全(参数来源于mysql5.5.19源码)
    查看>>
    mysqldump备份时忽略某些表
    查看>>
    mysqldump实现数据备份及灾难恢复
    查看>>
    mysqldump数据库备份无法进行操作只能查询 --single-transaction
    查看>>
    mysqldump的一些用法
    查看>>
    mysqli
    查看>>
    MySQLIntegrityConstraintViolationException异常处理
    查看>>