简述Nginx的rewrite规则及其使用

Nginx里的rewrite是实现URL重写的关键指令
语法:
rewrite [flag](标记);

flag标记说明:
last
本条规则匹配完成终止当前location的规则,继续向下匹配新location URI规则
break
本条规则匹配完成即终止,不再匹配后面的任何规则
redirect
返回302临时重定向,浏览器地址会显示跳转后的URL地址,关闭服务,无法重定向。
permanent
返回301永久重定向,浏览器地址栏会显示跳转后的URL地址,关闭服务,依然可以重定向,清除缓存失效。

几个rewrite使用案例:
1、实现域名跳转
为何要实现域名跳转?大家可能知道京东早期的域名是:360buy.com,并不是现在的jd.com。这个域名京东不用了,但有些老客户可能会用到,因此,通过实现域名跳转,访问www.360buy.com时自动跳转到www.jd.com。

2、实现不同终端跳转
还是拿京东来说哈,大家知道,www.jd.com默认是电脑端显示,移动端是m.jd.com。通过电脑访问时,打开电脑端网页,通过移动端访问时,打开移动端网页,以让用户有更好的体验。

3、根据所使用浏览器语言跳转
很多网站有多语言设定,比如中文,英文。如果用户浏览器是英文时,跳转到英文内容,如果是中文时,跳转到中文内容。

要利用rewrite实现URL重定向,首先要确定正则,就是rewrite条件,这个在每个网站请求标头中都会有。比如百度www.baidu.com
标头信息:
简述Nginx的rewrite规则及其使用_第1张图片
要实现上述三个rewrite方法,要用到:
Host
User-Agents
Accept-Language
演示:
1、访问blog .test .com时,跳转到www.test.com

server {
        listen       80;
        server_name  www.test.com  blog.test.com;

        charset utf-8;
        
        location / {
        if ( $host != 'www.test.com') {
           rewrite ^/(.*)$ http://www.test.com/$1 permanent;
        }
           root html;
           index index.html;
        }

可以清楚看到跳转结果,如下,:

root@backupserver:/usr/local/nginx# curl blog.test.com

301 Moved Permanently

301 Moved Permanently


nginx/1.16.1
root@backupserver:/usr/local/nginx# curl blog.test.com -L this is www.test.com root@backupserver:/usr/local/nginx# curl blog.test.com -Lv * Rebuilt URL to: blog.test.com/ * Trying 172.16.0.9... * Connected to blog.test.com (172.16.0.9) port 80 (#0) > GET / HTTP/1.1 > Host: blog.test.com > User-Agent: curl/7.47.0 > Accept: */* > < HTTP/1.1 301 Moved Permanently < Server: nginx/1.16.1 < Date: Fri, 13 Aug 2021 05:47:40 GMT < Content-Type: text/html < Content-Length: 169 < Connection: keep-alive < Location: http://www.test.com/ < * Ignoring the response-body * Connection #0 to host blog.test.com left intact * Issue another request to this URL: 'http://www.test.com/' * Trying 172.16.0.9... * Connected to www.test.com (172.16.0.9) port 80 (#1) > GET / HTTP/1.1 > Host: www.test.com > User-Agent: curl/7.47.0 > Accept: */* > < HTTP/1.1 200 OK < Server: nginx/1.16.1 < Date: Fri, 13 Aug 2021 05:47:40 GMT < Content-Type: text/html; charset=utf-8 < Content-Length: 21 < Last-Modified: Fri, 13 Aug 2021 05:12:37 GMT < Connection: keep-alive < ETag: "6115ff45-15" < Accept-Ranges: bytes < this is www.test.com * Connection #1 to host www.test.com left intact

2 、实现不同终端跳转

    server {
        listen       80;
        server_name  www.test.com  blog.test.com;

        charset utf-8;

        location / {
        
        if ( $http_user_agent ~* "iphone|android" ) {
          rewrite ^/(.*)$ http://m.test.com
        }
           root html;
           index index.html;
        }

3、根据浏览器语言跳转


    server {
        listen       80;
        server_name  www.test.com  blog.test.com;

        charset utf-8;

        location / {
        if ( $http_accept_language ~ "^zh-CN" ) {
            rewrite ^/(.*) /zh/$1 ;
        }
        if ( $http_accept_language ~ "^en" ) {
          rewrite ^/(.*) /en/$1 ;
        }
           root html;
           index index.html;
        }
        location ^~ /zh/ {
          root html/;
          index index.html;
        }
        location ^~ /en/ {
          root html/;
          index index.html;
        }
        }

你可能感兴趣的:(linux,nginx)