博客
关于我
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 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
    查看>>
    mysql 5.6 修改端口_mysql5.6.24怎么修改端口号
    查看>>
    MySQL 8.0 恢复孤立文件每表ibd文件
    查看>>
    MySQL 8.0开始Group by不再排序
    查看>>
    mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
    查看>>
    multi swiper bug solution
    查看>>
    MySQL Binlog 日志监听与 Spring 集成实战
    查看>>
    MySQL binlog三种模式
    查看>>
    multi-angle cosine and sines
    查看>>
    Mysql Can't connect to MySQL server
    查看>>
    mysql case when 乱码_Mysql CASE WHEN 用法
    查看>>
    Multicast1
    查看>>
    MySQL Cluster 7.0.36 发布
    查看>>
    Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
    查看>>
    MySQL Cluster与MGR集群实战
    查看>>
    multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
    查看>>
    mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
    查看>>
    Multiple websites on single instance of IIS
    查看>>
    mysql CONCAT()函数拼接有NULL
    查看>>
    multiprocessing.Manager 嵌套共享对象不适用于队列
    查看>>