Nginx中rewrite示例

x33g5p2x  于2021-10-13 转载在 其他  
字(3.8k)|赞(0)|评价(0)|浏览(327)

前言

环境准备:

  • 安装本地YUM源仓库
  • 源码编译安装Nginx服务
  • VMware,基于CentOS7
  • 一台源主机CentOS7(192.168.126.14)
  • 一台新虚拟机(192.168.126.13)用作测试

一、基于域名的跳转

  • 需求:现在公司旧域名www.xjj.com有业务需求变更,需要使用新域名www.xcf.com代替,但是旧域名不能废除,需要跳转到新域名上,而且后面的参数保持不变
mkdir -p /var/log/nginx/

vim /usr/local/nginx/conf/nginx.conf

    server {
        listen       80;
        server_name  www.xcf.com;
                        #修改域名 

        #charset koi8-r;

        access_log  /var/log/nginx/www.xcf.com-access.log;
                ##日志保存路径修改

        location / {
        if ($host = 'www.xcf.com') {
        #$host为rewrite全局变量,代表请求主机头字段或主机名
                rewrite ^/(.*)$ http://www.zxc.com/$1 permanent;
                                #$1为正则匹配得内容,即域名后边得字符串
        }
            root   html;
            index  index.html index.htm;
        }

echo "192.168.126.14 www.xjj.com" >> /etc/hosts
echo "192.168.126.13 www.xcf.com" >> /etc/hosts

systemctl restart nginx.service

此时打开浏览器,访问 http://www.xjj.com 时会自动跳转至http://www.xcf.com

二、基于客户端IP访问跳转

  • 需求:公司业务新版本上线,要求所有 IP 访问任何内容都显示是一个固定维护的页面,只有公司 IP 192.168.126.14 访问正常
vim /usr/local/nginx/conf/nginx.conf

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

        #charset koi8-r;

        access_log  /var/log/nginx/www.xcf.com-access.log;
        ##设置是否是合法的IP标记
        set $rewrite true;
        #设置变量$rewrite,变量值为boole值true

        #判断是否为合法IP
        if ($remote_addr = "192.168.126.14") {
                set $rewrite false;
                #当客户端IP为192.168.126.15时,将变量值设为flase,不进行重写
        }

        ##除了合法IP,其它都是非法IP,进行重写跳转到维护页面
        if ($rewrite = true) {
        #当变量值为true时,进行重写
                rewrite (.+) /weihu.html;
                #重写在访问IP后边插入/weihu.html,例如192.168.126.65/weihu.html
        }
        location = /weihu.html {
            root /var/www/html;
        #页面返回/var/www/html/weihu.html的内容
        }

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

mkdir -p /var/www/html

echo '<h1>weihu!!</h1>' > /var/www/html/weihu.html

systemctl restart nginx.service

  • 进入测试端虚拟机修改配置文件,用以映射源主机DNS域名解析

  • 在客户端虚拟机上进行测试

  • 只有IP 为 192.168.126.14 的源主机才可以进行正常访问网站

三、基于旧域名跳转到新域名后并加目录

  • 需求:网站(http://bbs.xjj.com)更换域名,需要将旧域名下的访问都跳转至新网址中(http:www.xjj.com/bbs)
vim /usr/local/nginx/conf/nginx.conf

    server {
        listen       80;
        server_name  bbs.xjj.com;
                     #修改域名

        #charset koi8-r;

        access_log  /var/log/nginx/www.xjj.com-access.log;
        #添加
        location /post {
                rewrite (.+) http://www.xjj.com/bbs$1 permanent;
                #这里$1为位置变量,代表/post
        }
        location / {
            root   html;
            index  index.html index.htm;
        }


mkdir -p /usr/local/nginx/html/bbs/post

echo "this is 1.html" >> /usr/local/nginx/html/bbs/post/1.html
echo "192.168.126.14 bbs.xjj.com" >> /etc/hosts

systemctl restart nginx.service

  • 此时用浏览器访问 http://bbs.xjj.com/post/1.html 会自动跳转到 http://www.xjj.com/bbs/post/1.html

四、基于参数匹配的跳转

  • 需求:访问 http://www.xjj.com/100-(100|200)-100.html 会跳转到 http://www.xjj.com的页面
vim /usr/local/nginx/conf/nginx.conf

    server {
        listen       80;
        server_name  www.xjj.com;
                     #修改域名

        #charset koi8-r;

        access_log  /var/log/nginx/www.xjj.com-access.log;

        if ($request_uri ~ ^/100-(100|200)-(\d+)\.html$) {
        #设置正则匹配
                rewrite (.*) http://www.xjj.com permanent;
                #设置重写
        }
        location / {
            root   html;
            index  index.html index.htm;
        }


systemctl restart nginx.service

  • 使用浏览器访问 http://www.xjj.com/100-100-100.html 或 http://www.xjj.com/100-200-100.html会自动跳转到 http://www.xjj.com页面

五、基于目录下所有php结尾的文件跳转

  • 需求:要求访问 http://www.xjj.com/upload/123.php 跳转到首页
server {
        listen       80;
        server_name  www.xjj.com;
                     #修改域名

        #charset koi8-r;

        access_log  /var/log/nginx/www.xjj.com-access.log;

        location ~* /upload/.*\.php$ {
           rewrite (.+) http://www.xjj.com permanent;
        }

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

systemctl restart nginx.service

  • 浏览器访问 http://www.xjj.com/upload/123.php 跳转到 http://www.xjj.com 首页

六、基于最普通一条URL请求的跳转

  • 要求访问一个具体的页面,如: http://www.xjj.com/abc/123.html,能跳转到首页
vim /usr/local/nginx/conf/nginx.conf

    server {
        listen       80;
        server_name  www.xjj.com;
                     #修改域名

        #charset koi8-r;

        access_log  /var/log/nginx/www.xjj.com-access.log;

        location ~* /abc/123.html {
           rewrite (.+) http://www.xjj.com permanent;
        }

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

systemctl restart nginx.service
  • 浏览器访问 http://www.xcf.com/abc/123.html 跳转到 http://www.xjj.com

总结

1.rewrite跳转使用场景:

①rewrite

②location:主要功能proxy_pass

③if :location下,只支持单分支不支持多分支

2.location优先级

  • 匹配的对象为文件或者目录的时候,会有优先级的调整

匹配文件: location中 ~ 和 ~/*,区分大小写 精确,不区分更为精确

匹配目录: location 中 ~/* 和 ~,区分大小写 更为精确,优先级更高

3.nginx中必备一些location规则

①location / {} 加快加载速度

②location /static {} 静态请求匹配

③location 反向代理 反向跳转到配置文件中upstream tomcat_server地址池中,获取发送到后端节点的“目标_IP",跳转的方式直接使用proxy_pass http://tomact_server (函数名)

相关文章