博客
关于我
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 创建函数 Error Code : 1418
    查看>>
    MySQL 创建新用户及授予权限的完整流程
    查看>>
    mysql 创建表,不能包含关键字values 以及 表id自增问题
    查看>>
    mysql 删除日志文件详解
    查看>>
    mysql 判断表字段是否存在,然后修改
    查看>>
    MySQL 到底能不能放到 Docker 里跑?
    查看>>
    mysql 前缀索引 命令_11 | Mysql怎么给字符串字段加索引?
    查看>>
    MySQL 加锁处理分析
    查看>>
    mysql 协议的退出命令包及解析
    查看>>
    mysql 参数 innodb_flush_log_at_trx_commit
    查看>>
    mysql 取表中分组之后最新一条数据 分组最新数据 分组取最新数据 分组数据 获取每个分类的最新数据
    查看>>
    MySQL 命令和内置函数
    查看>>
    mysql 四种存储引擎
    查看>>
    MySQL 在并发场景下的问题及解决思路
    查看>>
    MySQL 基础架构
    查看>>
    MySQL 基础模块的面试题总结
    查看>>
    MySQL 备份 Xtrabackup
    查看>>
    mYSQL 外键约束
    查看>>
    mysql 多个表关联查询查询时间长的问题
    查看>>
    mySQL 多个表求多个count
    查看>>