ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

nginx rewrite模块

2022-06-13 03:00:08  阅读:173  来源: 互联网

标签:index www rewrite nginx html 模块 xhx com


1、rewrite的概述

rewrite功能就是,使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标志位实现url重写以及重定向

rewrite只能放在server{},location{},if{}中,并且默认只能对域名后边的除去传递的参数外的字符串起作用,

 

2、rewrite 过程:

(1) 执行 server 块里面的 rewrite 指令

(2) 执行 location 匹配

(3) 执行选定的 location 中的 rewrite 指令

语法: rewrite [flag];

regex :表示正则匹配规则

replacement :表示跳转后的内容

flag :表示 rewrite 支持的 flag 标记

flag标记说明:

last :本条规则匹配完成后,继续向下匹配新的location URI规则,一般用在 server 和 if 中

break :本条规则匹配完成即终止,不再匹配后面的任何规则,一般使用在 location 中

redirect:返回302临时重定向,浏览器地址会显示跳转后的URL地址

permanent:返回301永久重定向,浏览器地址栏会显示跳转后的URL地址。

 

3、基于域名跳转

操作步骤

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

server {

listen 80;

server_name www.xhx.com; #域名修改

charset utf-8;

access_log /var/log/nginx/www.xhx.com-access.log; #日志修改

location / { #添加域名重定向

if ($host = 'www.xhx.com'){ #$host为rewrite全局变量,代表请求主机头字段或主机名

rewrite ^/(.*)$ http://www.hello.com/$1 permanent; #$1为正则匹配的内容,即域名后边的字符串

}

root html;

index index.html index.htm;

}

}

echo "172.16.10.101 www.xhx.com www.hello.com" >> /etc/hosts

cd /usr/local/nginx/html

mkdir test

cd test

echo "<h1>xhx<h1>" > index.html

systemctl restart nginx #重启服务

浏览器输入模拟访问 http://www.xhx.com/test/index.html会跳转到www.hello.com/test/index.html,查看元素可以看到返回301,实现了永久重定向跳转,而且域名后的参数也正常跳转

 

 

 

4.基于客户端 IP 访问跳转

操作步骤

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

server {

            listen 80;

            server_name www.xhx.com; #域名修改

              charset utf-8;

             access_log /var/log/nginx/www.xhx.com.access.log; #日志修改

            set $rewrite true;

            if ($remote_addr = "172.16.10.101") {      #当客户端IP为172.16.10.101时,将变量值设为false,不进行重写

            set $rewrite false; }                                  #除了合法IP,其它都是非法IP,进行重写跳转维护页面

              if ($rewrite = true){                               #当变量值为true时,进行重写

               rewrite (.+) /test.html;                     #重写在访问IP后边插入/index.html,例如172.16.10.101/index.html

               }

              location = /test.html {

               root /var/www/html;                             #网页返回/var/www/html/index.html的内容

               }

             location / {

               root html;

               index index.html index.htm;

}

}

 

mkdir -p /var/www/html/

echo "<h1>正在维护</h1>" > /var/www/html/index.html

systemctl restart nginx

 本机访问

其他主机访问

 

 

 

5、基于旧域名跳转到新域名后面加目录

操作步骤

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

server {

listen 80;

server_name bbs.xhx.com; #域名修改

charset utf-8;

access_log /var/log/nginx/bbs.xhx.com.access.log;

#添加

location /test {

rewrite (.+) http://www.xhx.com/bbs$1 permanent; #这里的$1为位置变量,代表/test

}

location / {

root html;

index index.html index.htm;

}

}

 

 

 

 

 

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

echo "<h1>this is web<h1>" >> /usr/local/nginx/html/bbs/test/1.html

echo "172.16.10.101 www.xhx.com bbs.xhx.com" >> /etc/hosts systemctl restart nginx

使用浏览器访问 http://bbs.xkq.com/ test/1.html 跳转到 http://www.xkq.com/bbs/test/1.html

 

 

 

 

6.基于参数匹配的跳转

步骤

 

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

server {

listen 80;

server_name www.xhx.com; #域名修改

charset utf-8;

access_log /var/log/nginx/xhx.kgc.com-access.log;

if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {

rewrite (.*) http://www.xhx.com permanent;

}

location / {

root html;

index index.html index.htm;

}

systemctl restart nginx

 

 

 用浏览器访问 http://www.xhx.com/100-200-100.html 或 http://www.xhx.com/100-100-100.html 跳转到http://www.xhx.com页面

 

 

 

 

 

 

 

7.基于目录下所有 php 结尾的文件跳转

操作步骤

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

server {

listen 80;

server_name www.xhx.com; #域名修改

charset utf-8;

access_log /var/log/nginx/www.xhx.com.access.log ;

location ~* /upload/.*.php$ {

rewrite (.+) http://www.xhx.com permanent;

}

location / {

root html;

index index.html index.htm;

}

}

 

 

 

 

 

systemctl restart nginx

浏览器访问 http://www.xhx.com/upload/888.php 跳转到http://www.xhx.com页面

 

 

 

 

 

 

 

 

 

 

标签:index,www,rewrite,nginx,html,模块,xhx,com
来源: https://www.cnblogs.com/xhx1991874414/p/16369448.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有