Nginx的URL重写

1. 概述

url重写是指通过配置conf文件,以让网站的url中达到某种状态时则定向/跳转到某个规则,比如常见的伪静态、301重定向、浏览器定向等

2. 语法

rewrite             [flag];

关键字        正则            替代内容          flag标记

关键字

其中关键字error_log不能改变

正则

perl兼容正则表达式语句进行规则匹配

替代内容

将正则匹配的内容替换成replacement

flag标记

rewrite支持的flag标记

说明:

  1. last ---> 本条规则匹配完成后,继续向下匹配新的location URI规则, 浏览器地址栏URL地址不变
  2. break ---> 本条规则匹配完成即终止,不再匹配后面的任何规则, 浏览器地址栏URL地址不变
  3. redirect ---> 返回302临时重定向,浏览器地址会显示跳转后的URL地址
  4. permanent ---> 返回301永久重定向,浏览器地址栏会显示跳转后的URL地址

3. rewrite参数的标签可使用的位置

server, location, if

4. 简单示例

server {
    # 访问 /last.html 的时候,页面内容重写到 /index.html 中
    rewrite /last.html /index.html last;

    # 访问 /break.html 的时候,页面内容重写到 /index.html 中,并停止后续的匹配
    rewrite /break.html /index.html break;

    # 访问 /redirect.html 的时候,页面直接302定向到 /index.html中
    rewrite /redirect.html /index.html redirect;

    # 访问 /permanent.html 的时候,页面直接301定向到 /index.html中
    rewrite /permanent.html /index.html permanent;

    # 把 /html/*.html => /post/*.html ,301定向
    rewrite ^/html/(.+?).html$ /post/$1.html permanent;

    # 把 /search/key => /search.html?keyword=key
    rewrite ^/search\/([^\/]+?)(\/|$) /search.html?keyword=$1 permanent;
}

5. if判断

简单重写很多时候满足不了需求,比如需要判断当文件不存在时、当路径包含xx时等条件,则需要用到if

5.1 语法

if (表达式) {
   ... ...
}

注意:

  • 当表达式只是一个变量时,如果值为空或任何以0开头的字符串都会当做false
  • 直接比较变量和内容时,使用=或!=
  • 正则表达式匹配,*不区分大小写的匹配,!~区分大小写的不匹配

一些内置的条件判断:

  • -f 和 !-f用来判断是否存在文件
  • -d 和 !-d用来判断是否存在目录
  • -e 和 !-e用来判断是否存在文件或目录
  • -x 和 !-x用来判断文件是否可执行

内置的全局变量:

query_string

$content_length : 请求头中的Content-length字段。

$content_type : 请求头中的Content-Type字段。

$document_root : 当前请求在root指令中指定的值。

$host : 请求主机头字段,否则为服务器名称。

$http_user_agent : 客户端agent信息

$http_cookie : 客户端cookie信息

$limit_rate : 这个变量可以限制连接速率。

$request_method : 客户端请求的动作,通常为GET或POST。

$remote_addr : 客户端的IP地址。

$remote_port : 客户端的端口。

$remote_user : 已经经过Auth Basic Module验证的用户名。

$request_filename : 当前请求的文件路径,由root或alias指令与URI请求生成。

$scheme : HTTP方法(如http,https)。

$server_protocol : 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。

$server_addr : 服务器地址,在完成一次系统调用后可以确定这个值。

$server_name : 服务器名称。

$server_port : 请求到达服务器的端口号。

$request_uri : 包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。

uri不包含主机名,如”/foo/bar.html”。

uri相同。

5.2 小示例

server {
    # 如果文件不存在则返回400
    if (!-f $request_filename) {
        return 400;
    }
    
    # 如果host不是xuexb.com,则301到xuexb.com中
    if ( $host != "xuexb.com" ){
        rewrite ^/(.*)$ https://xuexb.com/$1 permanent;
    }
    
    # 如果请求类型不是POST则返回405
    if ($request_method = POST) {
        return 405;
    }
    
    # 如果参数中有 a=1 则301到指定域名
    if ($args ~ a=1) {
        rewrite ^ http://example.com/ permanent;
    }
}

结合location使用:

server {
    
    listen 80;
    server_name lotus.com;
    
    location = /test.html {
        # 默认值为lotus
        set $name lotus;
    
        # 如果参数中有 name=xx 则使name用该值
        if ($args ~* name=(\w+?)(&|$)) {
            set $name $1;
        }
    
        # 301
        rewrite ^ /$name.html permanent;
    }
}

上面表示:

  • /test.html => /lotus.html
  • /test.html?name=jack => /jack.html?name=jack

6. 完整示例

编辑 conf.d/rewrite.conf配置文件:

server {
    listen 80;
    server_name lotus.com www.lotus.com;

    if ( $host != 'www.abc.com'  ) {
        rewrite ^/(.*) http://www.abc.com/$1 permanent;
    }

    location / {
        root /data/www/www;
        index index.html index.htm;
    }

    error_log    logs/error_www.abc.com.log error;
    access_log    logs/access_www.abc.com.log    main;
}

你可能感兴趣的:(Nginx的URL重写)