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

    你可能感兴趣的文章
    mac mysql 进程_Mac平台下启动MySQL到完全终止MySQL----终端八步走
    查看>>
    Mac OS 12.0.1 如何安装柯美287打印机驱动,刷卡打印
    查看>>
    MangoDB4.0版本的安装与配置
    查看>>
    Manjaro 24.1 “Xahea” 发布!具有 KDE Plasma 6.1.5、GNOME 46 和最新的内核增强功能
    查看>>
    mapping文件目录生成修改
    查看>>
    MapReduce程序依赖的jar包
    查看>>
    mariadb multi-source replication(mariadb多主复制)
    查看>>
    MariaDB的简单使用
    查看>>
    MaterialForm对tab页进行隐藏
    查看>>
    Member var and Static var.
    查看>>
    memcached高速缓存学习笔记001---memcached介绍和安装以及基本使用
    查看>>
    memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
    查看>>
    Memcached:Node.js 高性能缓存解决方案
    查看>>
    memcache、redis原理对比
    查看>>
    memset初始化高维数组为-1/0
    查看>>
    Metasploit CGI网关接口渗透测试实战
    查看>>
    Metasploit Web服务器渗透测试实战
    查看>>
    MFC模态对话框和非模态对话框
    查看>>
    Moment.js常见用法总结
    查看>>
    MongoDB出现Error parsing command line: unrecognised option ‘--fork‘ 的解决方法
    查看>>