Nginx rewrite 的应用

Nginx rewrite 的应用

一、nginx rewrite模块应用

1.1.1 什么是Nginx rewrite?

和Apache的web服务器软件一样,Nginx rewrite的主要功能也是实现URL地址重写,Nginx的rewrite规则需要pcre软件的支持,即通过Perl兼容正则表达式语法进行规则匹配的。

1.1.2 Nginx rewrite语法

  1. 语法介绍
官方文档:http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
指令语法:rewrite regex replacement [flag]
          指令    需要跳转的(支持正则) 跳转到哪(不支持正则) 标记
  1. [flag]标记的四种常用标记
flag 标记符号 说明
last 本条规则匹配完成后,继续向下匹配新的location URI规则
break 本条规则匹配完成即终止,不在匹配后面的任何规则。
redirect 返回302临时重定向,浏览器地址栏会显示跳转后的URL地址
permanent 返回301永久重定向,浏览器地址栏会显示跳转后的URL地址
  1. rewrite 的企业应用
1) 可以调整用户浏览的URL,看起来更加规范,合乎开发及产品人员的需求    
2) 为了让搜索引擎收录网站内容及用户体验更好,企业会将动态URL的地址伪装成静态的地址提供服务
3)公司网站换新域名后 让旧的域名的访问跳转到行域名上
4)根据特殊变量、目录、客户端的信息进行URL跳转

2.1.2 Nginx rewrite 实战

  1. rewrite 301 跳转
[root@web01 /application/nginx/conf/extra]# cat 01_www.conf
server { 
  listen   80;
  server_name etiantian.org;
  rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
   }    

server {
        listen       80;
        server_name  www.etiantian.org;

        #charset koi8-r;

        access_log  logs/access_www.log  main;

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

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
   }
#将etiantian.org 永久301跳转到www.etiantian.org
  1. 重启服务

[root@web01 /application/nginx/conf/extra]# ../../sbin/nginx -t
nginx: the configuration file /application/nginx-1.16.0/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.16.0/conf/nginx.conf test is successful
[root@web01 /application/nginx/conf/extra]# ../../sbin/nginx -s reload
  1. 测试结果
    图片.png

    访问的是etiantian.org,但是却跳转到www.etiantian.org
[root@web01 /application/nginx/conf/extra]# curl -I etiantian.org
HTTP/1.1 301 Moved Permanently #301 永久跳转
Server: nginx/1.16.0
Date: Mon, 11 May 2020 08:10:54 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: http://www.etiantian.org/ #跳转地址
  1. 将公司的bbs目录下的业务跳转到新站点bbs.etiantian.org
[root@web01 /application/nginx/conf/extra]# cat  01_www.conf
server { 
  listen   80;
  server_name etiantian.org;
  rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
   }    

server {
        listen       80;
        server_name  www.etiantian.org;

        #charset koi8-r;

        access_log  logs/access_www.log  main;

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

        rewrite ^(.*)/bbs/ http://bbs.etiantian.org break;  #这里设置跳转
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

}

[root@web01 /application/nginx/conf/extra]# cat  02_bbs.conf 
   server {   

     listen       80;
     server_name  bbs.etiantian.org;
 
     location / {
     root      html/bbs;      
     index    index.html index.htm;

       }
      error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
      access_log  logs/access_bbs.log  main;
    }
  1. 重启测试
[root@web01 /application/nginx/conf/extra]# ../../sbin/nginx -t
[root@web01 /application/nginx/conf/extra]# ../../sbin/nginx -s reload

结果跳转到bbs了


图片.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容