一、 Rewrite 跳转实现
服务协议功能模块
url 资源定位路径
- nginx————支持url重写、支持if条件判断,但不支持else
- 跳转————循环最多可以执行10次,超过后nginx将返回500代码错误
- rewrite————使用nginx’全局变量或自己设置的变量,结合正则表达式和标志位实现url重写以及重定向
二、Rewrite 使用场景 - 使用rewrite进行匹配跳转
- 使用if匹配全局变量后跳转
- 使用location匹配再跳转
1.rewrite放在 server{},if{},location{}段中
2.对域名或参数字符串:使用if全局变量匹配、使用proxy_pass反向代理
三、nginx正则表达式
常用的正则表达式元字符
四、Rewrite 命令
location分类
正则匹配的常用表达式
五、location优先级
按优先级排列: - = 类型
- ^~ 类型表达式
- 正则表达式(和)类型
- 常规字符串匹配类型,按前缀匹配
- 通用匹配(/),如果没有其他匹配,任何请求都会匹配到
location优先级规则(从高到低排列)
1.匹配某个具体的文件 - location = 完整路径
- location ^~ 完整路径
- location ~* 完整路径
- location ~ 完整路径
- location 完整路径
- location /
2.用目录做匹配访问某个文件 - location = 目录
- location ^~ 目录
- location ~ 目录
- location ~* 目录
- location 目录
- location /
六、应用场景测试
公司旧域名 www.domain.com 因业务需求有变更,需要使用新域名 www.newdomain.com 代替,不能废除旧域名,从旧域名跳转到新域名,且保持其参数不变[[email protected] bin]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm 获取http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm 警告:/var/tmp/rpm-tmp.IHyTHc: 头V4 RSA/SHA1 Signature, 密钥 ID 7bd9bf62: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:nginx-release-centos-7-0.el7.ngx ################################# [100%] [[email protected] bin]# yum install nginx -y
[[email protected] ~]# mkdir /abc [[email protected] ~]# mount.cifs //192.168.56.1 /mnt Password for [email protected]//192.168.254.10/linuxs: [[email protected] ~]# cd /abc/LNMP-C7/LNMP-C7/ [[email protected] LNMP-C7]# ls Discuz_X3.4_SC_UTF8.zip mysql-boost-5.7.20.tar.gz ncurses-5.6.tar.gz nginx-1.12.2.tar.gz php-5.6.11.tar.bz2 php-7.1.10.tar.bz2 php-7.1.20.tar.bz2 php-7.1.20.tar.gz zend-loader-php5.6-linux-x86_64_update1.tar.gz [[email protected] LNMP-C7]# tar -zxvf nginx-1.12.2.tar.gz -C /opt
[[email protected] LNMP-C7]# useradd -M -s /sbin/nologin nginx [[email protected] LNMP-C7]# cd /opt/nginx-1.12.2/ [[email protected] nginx-1.12.2]# ls auto CHANGES.ru configure html man src CHANGES conf contrib LICENSE README [[email protected] nginx-1.12.2]# yum install gcc gcc-c++ pcre pcre-devel make zlib-devel -y [[email protected] nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module [[email protected] nginx-1.12.2]# make && make install
[[email protected] conf]# vim nginx.conf 37 server_name www.accp.com; 41 access_log logs/www.accp.com/access.log main;
[[email protected] conf]# yum install bind -y [[email protected] conf]# vim /etc/named.conf 13 listen-on port 53 { any; }; 21 allow-query { any; }; [[email protected] conf]# vim /etc/named.rfc1912.zones 25 zone "accp.com" IN { 26 type master; 27 file "accp.com.zone"; 28 allow-update { none; }; 29 }; [[email protected] conf]# cd /var/named/ [[email protected] named]# ls data named.ca named.localhost slaves dynamic named.empty named.loopback [[email protected] named]# cp -p named.localhost accp.com.zone [[email protected] named]# vim accp.com.zone www IN A 192.168.247.202 [[email protected] named]# systemctl start named [[email protected] named]# systemctl stop firewalld.service [[email protected] named]# setenforce 0 [[email protected] named]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ [[email protected] named]# cd /usr/local/nginx/ [[email protected] nginx]# ls conf html logs sbin [[email protected] nginx]# cd logs/ [[email protected] logs]# mkdir www.accp.com [[email protected] logs]# ls error.log www.accp.com [[email protected] logs]# nginx [[email protected] logs]# netstat -natp | grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6
打开win10客户机,配置dns服务器,并使用域名访问。
[[email protected] logs]# vim /usr/local/nginx/conf/nginx.conf location / { #域名重定向 ‘ if ($host = ‘www.accp.com‘) { ’ rewrite ^/(.*)$ http://www.kgc.com/$1 permanent; ‘ } root /html; index index.html index.htm; }
[[email protected] logs]# vim /etc/named.rfc1912.zones zone "kgc.com" IN { type master; file "kgc.com.zone"; allow-update { none; }; }; [[email protected] logs]# cd /var/named/ [[email protected] named]# ls accp.com.zone dynamic named.empty named.loopback data named.ca named.localhost slaves [[email protected] named]# cp -p accp.com.zone kgc.com.zone
[[email protected] named]# systemctl restart named [[email protected] named]# killall -1 nginx
[[email protected] html]# vim /usr/local/nginx/conf/nginx.conf 37 server_name bbs.accp.com; 42 location /post { 43 rewrite (.+) http://www.accp.com/bbs$1 permanent; 44 }
[[email protected] html]# cd - /var/named [[email protected] named]# vim accp.com.zone [[email protected] named]# cat accp.com.zone $TTL 1D @ IN SOA @ rname.invalid. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS @ A 127.0.0.1 bbs IN A 192.168.247.202
[[email protected] named]# killall -3 nginx [[email protected] named]# nginx [[email protected] named]# systemctl restart named
基于客户端IP访问跳转[[email protected] named]# vim /usr/local/nginx/conf/nginx.conf 42 #设置是否合法的IP标志 43 set $rewrite true; 44 #判断是否为合法IP,是否是允许的IP 45 if ($remote_addr = "192.168.247.139"){ 46 set $rewrite false; 47 } 48 #不被允许的IP进行判断,打上标记 49 if ($rewrite = true){ 50 rewrite (.+) /maintenance.html; 51 } 52 #匹配标记进行跳转站点 53 location = /maintenance.html { 54 root html; 55 }
[[email protected] named]# cd /usr/local/nginx/html/ [[email protected] html]# ls 50x.html index.html [[email protected] html]# vim maintenance.html [[email protected] html]# killall -3 nginx [[email protected] html]# nginx
基于参数匹配的跳转———跳转到首页[[email protected] named]# vim /usr/local/nginx/conf/nginx.conf 37 server_name www.accp.com; 42 if ($request_uri ~ ^/100-(100|200)-(\d+).html$){ 43 rewrite (.*) http://www.accp.com permanent; 44 } [[email protected] named]# vim accp.com.zone [[email protected] named]# cat accp.com.zone $TTL 1D @ IN SOA @ rname.invalid. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS @ A 127.0.0.1 www IN A 192.168.247.202
[[email protected] named]# systemctl restart named [[email protected] named]# killall -3 nginx [[email protected] named]# nginx
基于目录下所有PHP文件跳转[[email protected] named]# vim /usr/local/nginx/conf/nginx.conf 42 location ~* /upload/.*\.php$ { 43 rewrite (.+) http://www.accp.com permanent; 44 }
[[email protected] named]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [[email protected] named]# killall -3 nginx [[email protected] named]# nginx
基于最普通URL请求的跳转——跳转到首页[[email protected] named]# vim /usr/local/nginx/conf/nginx.conf 42 location ~* ^/abc/123.html { 43 rewrite (.+) http://www.accp.com permanent; 44 }
基于最普通URL请求的跳转——跳转到首页[[email protected] named]# vim /usr/local/nginx/conf/nginx.conf 42 location ~* ^/abc/123.html { 43 rewrite (.+) http://www.accp.com permanent; 44 }
[[email protected] named]# killall -3 nginx [[email protected] named]# nginx
原文地址:https://blog.51cto.com/14557905/2463924
时间: 2024-11-08 18:36:15