Nginx服务中Rewrite的应用
rewrite
if
location
proxy_ pass
^
$
*
+
?
.
\
\d
{n}
{n,}
[c]
[a-z]
[a-zA-Z]
rewrite <regex> <replacement> [flag]; <regex> //正则 <replacement> //跳转后的内容 [flag] //rewrite支持flag标记
last
Apache
[L]
break
redirect
302
URL
url
permanent
301
server
location = patt {} [精准匹配] location patt {} [一般匹配] location ~ patt {} [正则匹配]
~
~*
!~
!~*
^~
=
@
proxy_pass
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 ~*
[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 //编辑跳转的网页内容 <h2>this is test web</h2> :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服务