Nginx服务中Rewrite的应用php
rewrite
进行匹配跳转if
匹配全局变量后跳转location
匹配再跳转if
全局变量匹配proxy_ pass
反向代理字符 | 说明 |
---|---|
^ |
匹配输入字符串的起始位置 |
$ |
匹配输入字符串的结束位置 |
* |
匹配前面的字符零次或屡次 |
+ |
匹配前面的字符一次或屡次 |
? |
匹配前面的字符零次或一次 |
. |
匹配除“\n”以外的任何单个字符。使用诸如"[.\n]"之 类的模式,可匹配包括“n”在内的任意字符 |
\ |
将后面接着的字符标记为一个特殊字符或一个原义字符或一个向后引用 |
\d |
匹配纯数字 |
{n} |
重复n次 |
{n,} |
重复n次或更屡次 |
[c] |
匹配单个字符c |
[a-z] |
匹配a-z小写字母的任意一个 |
[a-zA-Z] |
匹配a-z小写字母或A-Z大写字母的任意一 个 |
rewrite <regex> <replacement> [flag]; <regex> //正则 <replacement> //跳转后的内容 [flag] //rewrite支持flag标记
标记 | 说明 |
---|---|
last |
至关于Apache 的[L] 标记,表示完成rewrite |
break |
本条规则匹配完成即终止,再也不匹配后面的任何规则 |
redirect |
返回302 临时重定向,浏览器地址会显示跳转后的URL 地址,爬虫不会更新url |
permanent |
返回301 永久重定向,浏览器地址栏会显示跳转后的URL 地址,爬虫更新url |
last | break | |
---|---|---|
使用场景 | 通常写在server 和if 中 |
通常使用在location 中 |
URL 匹配 |
不终止重写后的url 匹配 |
终止重写后的url 匹配 |
location = patt {} [精准匹配] location patt {} [通常匹配] location ~ patt {} [正则匹配]
标记 | 说明 |
---|---|
~ |
执行一个正则匹配,区分大小写 |
~* |
执行一个正则匹配,不区分大小写 |
!~ |
执行一个正则匹配,区分大小写不匹配 |
!~* |
执行一个正则匹配,不区分大小写不匹配 |
^~ |
普通字符匹配;使用前缀匹配。若是匹配成功,则再也不匹配其余location |
= |
普通字符精确匹配。也就是彻底匹配 |
@ |
定义一个命名的location ,使用在内部定向时 |
=
类型^~
类型表达式~
和~*
)类型rewrite
是在同一域名内更改获取资源的路径location
是对一-类路径作控制访问或反向代理,还能够proxy_pass
到其余机器server
块里面的rewrite
指令location
匹配location
中的rewrite
指令location =/ { [ configuration A ] //精确匹配/,主机名后面不能带任何字符串 } location/ { [ configuration B ] //全部的地址都以/开头,这条规则将匹配到全部请求,但正则和最长字符串会优先匹配 } location /documents/ { [ configuration C ] //匹配任何以/documents/开头的地址,当后面的正则表达式没有匹配到时,才起做用 } location ~ /documents/abc { [ configuration D ] //匹配任何以/documents/abc开头的地址当后面的正则表达式没有匹配到时,才会起做用 } location ^~ /images/ { [ configuration E ] //以/images/开头的地址,匹配符合后,中止往下匹配 } location ~* \.(gif|jipg|jpeg)$ { [ configuration F ] } //匹配全部以gif,jpg或jpeg结尾的请求,/images/下的图片会被[ configuration E ]处理,由于^~的优先级更高 location /images/abc { [ configuration G ] //最长字符匹配到/images/abc,优先级最低 } location ~ /images/abc { [ configuration H ] //以/images/abc开头的,优先级次之 } location /images/abc/1.html { [ configuration I ] //若是和正则~ /images/abc/1.html相比,正则优先级更高 }
location =
完整路径) > (location ^~
完整路径) > (location ~*
完整路径) > (location ~
完整路径) > (location
完整路径) > (location /
)location=
目录) > (location ^~
目录/) > (location ~
目录) > (location ~*
目录) > (location
目录) > (location /
)[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm //安装nginx官方源码包 获取http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm 警告:/var/tmp/rpm-tmp.ymMjSa: 头V4 RSA/SHA1 Signature, 密钥 ID 7bd9bf62: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:nginx-release-centos-7-0.el7.ngx ################################# [100%] [root@localhost ~]# yum install nginx -y //安装nginx服务 已加载插件:fastestmirror, langpacks 已加载插件:fastestmirror, langpacks base | 3.6 kB 00:00:00 extras | 2.9 kB 00:00:00 ...//省略部份内容... 已安装: nginx.x86_64 1:1.16.1-1.el7.ngx 完毕! [root@localhost ~]# rpm -qc nginx /etc/logrotate.d/nginx /etc/nginx/conf.d/default.conf //配置文件路径 /etc/nginx/fastcgi_params /etc/nginx/koi-utf /etc/nginx/koi-win /etc/nginx/mime.types /etc/nginx/nginx.conf /etc/nginx/scgi_params /etc/nginx/uwsgi_params /etc/nginx/win-utf /etc/sysconfig/nginx /etc/sysconfig/nginx-debug [root@localhost ~]# vim /etc/nginx/conf.d/default.conf //编辑配置文件 server { listen 80; server_name www.aacp.com; //更改域名 #charset koi8-r; access_log /var/log/nginx/www.accp.com-access.log main; //更改日志文件名称,并开启功能 location / { root /usr/share/nginx/html; index index.html index.htm; } ...//省略部份内容... :wq [root@localhost ~]# yum install bind -y //安装DNS功能 已加载插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.163.com ...//省略部份内容... 已安装: bind.x86_64 32:9.11.4-9.P2.el7 ...//省略部份内容... 完毕! [root@localhost ~]# vim /etc/named.conf ...//省略部份内容... options { listen-on port 53 { any; }; listen-on-v6 port 53 { ::1; }; directory "/var/named"; dump-file "/var/named/data/cache_dump.db"; statistics-file "/var/named/data/named_stats.txt"; memstatistics-file "/var/named/data/named_mem_stats.txt"; recursing-file "/var/named/data/named.recursing"; secroots-file "/var/named/data/named.secroots"; allow-query { any; }; ...//省略部份内容... :wq [root@localhost ~]# vim /etc/named.rfc1912.zones ...//省略部份内容... zone "accp.com" IN { type master; file "accp.com.zone"; allow-update { none; }; }; ...//省略部份内容... :wq [root@localhost ~]# cd /var/named/ [root@localhost named]# cp -p named.localhost accp.com.zone [root@localhost named]# vim 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.144.133 :wq [root@localhost named]# systemctl start named //启动DNS服务 [root@localhost named]# systemctl start nginx //启动nginx服务 [root@localhost named]# systemctl stop firewalld.service //关闭防火墙 [root@localhost named]# setenforce 0 //关闭加强性安全功能 [root@localhost named]# netstat -ntap | grep nginx //查看nginx服务是否开启 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2769/nginx: master
[root@localhost named]# vim /etc/nginx/conf.d/default.conf //编辑Nginx配置文件 ...//省略部份内容... #charset koi8-r; access_log /var/log/nginx/www.accp.com-access.log main; location / { if ( $host = "www.accp.com" ){ rewrite ^/(.*)$ http://www.kgc.com/$1 permanent; //在location模块中添加if判断语句,当输入"www.accp.com"访问网页时跳转到www.kgc.com中 } root /usr/share/nginx/html; index index.html index.htm; } #error_page 404 /404.html; ...//省略部份内容... :wq [root@localhost named]# vim /etc/named.rfc1912.zones ...//省略部份内容... zone "accp.com" IN { type master; file "accp.com.zone"; allow-update { none; }; }; zone "kgc.com" IN { type master; file "kgc.com.zone"; allow-update { none; }; }; ...//省略部份内容... :wq [root@localhost named]# cp -p accp.com.zone kgc.com.zone [root@localhost named]# systemctl restart nginx [root@localhost named]# systemctl restart named
[root@localhost conf.d]# vim default.conf //编辑配置文件 ...//省略部份内容... #charset koi8-r; access_log /var/log/nginx/www.accp.com-access.log main; set $rewrite true; //设置是否合法的IP标记 if ($remote_addr = "192.168.144.128"){ //判断是否为合法地址,若是为合法地址执行操做 set $rewrite false; } if ($rewrite = true){ //判断是否为非法地址,若是为非法地址则打上标记,合法地址不作操做 rewrite (.+) /main.html; } location = /main.html { //匹配标记,执行跳转 root /usr/share/nginx/html; } location / { //注意此处删除上面设置的基于域名跳转的配置条目 root /usr/share/nginx/html; index index.html index.htm; } ...//省略部份内容... :wq [root@localhost conf.d]# cd /usr/share/nginx/html/ //进nginx服务站点 [root@localhost html]# ls //查看信息 50x.html index.html [root@localhost html]# vim main.html //编辑跳转的网页内容 <h1>this is test web</h1> :wq [root@localhost html]# systemctl restart nginx //重启网站
http://bbs.accp.com
下面的发帖都跳转到http://www.accp.com/bbs
,且域名跳转后保持参数不变[root@localhost html]# vim /etc/nginx/conf.d/default.conf server { listen 80; server_name bbs.aacp.com; //更改域名 #charset koi8-r; access_log /var/log/nginx/www.accp.com-access.log main; location /post { //设置匹配字段,字段匹配执行跳转操做 rewrite (.+) http://www.accp.com/bbs$1 permanent; } //注意删除上面设置的基于客户端IP访问的跳转的配置条目 location / { root /usr/share/nginx/html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html ...//省略部份内容... :wq [root@localhost html]# vim /var/named/accp.com.zone //编辑DNS服务区域数据文件 $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.144.133 //更改主机头解析 :wq [root@localhost html]# systemctl restart nginx //重启nginx服务 [root@localhost html]# systemctl restart named //重启DNS服务
http://www.accp.com/100-(100|200)-100.html
跳转到http://www.accp.com
页面[root@localhost html]# vim /etc/nginx/conf.d/default.conf server { listen 80; server_name www.aacp.com; //将服务器域名更改会www #charset koi8-r; access_log /var/log/nginx/www.accp.com-access.log main; if ($request_uri ~ ^/100-(100|200)-(\d+).html$){ //删除此处上面设置的基于新、旧域名跳转并加目录跳转的配置条目 rewrite (.*) http://www.accp.com permanent; 并设置基于参数访问时跳转回主网页 } location / { root /usr/share/nginx/html; 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; ...//省略部份内容... :wq [root@localhost html]# vim /var/named/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.144.133 //更改主机头解析 :wq [root@localhost html]# systemctl restart nginx //重启nginx服务 [root@localhost html]# systemctl restart named //重启DNS服务
http://www.accp.com/upload/1.php
跳转到首页[root@localhost html]# vim /etc/nginx/conf.d/default.conf //编辑配置文件 server { listen 80; server_name www.aacp.com; #charset koi8-r; access_log /var/log/nginx/www.accp.com-access.log main; location ~* /upload/.*\.php$ { //删除上面设置的匹配参数跳转访问,配置匹配全部php结尾访问跳转回主页 rewrite (.+) http://www.accp.com permanent; } location / { root /usr/share/nginx/html; index index.html index.htm; } #error_page 404 /404.html; ...//省略部份内容... :wq [root@localhost html]# systemctl restart nginx //重启nginx服务
[root@localhost html]# vim /etc/nginx/conf.d/default.conf //编辑配置文件 server { listen 80; server_name www.aacp.com; #charset koi8-r; access_log /var/log/nginx/www.accp.com-access.log main; location ~* ^/abc/123.html { //删除上面的配置,并从新编辑以具体的某个页面访问网页时跳转回主页 rewrite (.+) http://www.accp.com permanent; } location / { root /usr/share/nginx/html; index index.html index.htm; } #error_page 404 /404.html; ...//省略部份内容... :wq [root@localhost html]# systemctl restart nginx //重启nginx服务