这篇文章将为大家详细讲解有关Nginx中如何进行rewrite模块应用,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
Linux CentOS7虚拟机(IP:192.168.52.132)
win10虚拟机(作为测试机)
[root@localhost ~]# 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.PtKluI: 头V4 RSA/SHA1 Signature, 密钥 ID 7bd9bf62: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:nginx-release-centos-7-0.el7.ngx ################################# [100%] [root@localhost ~]# yum install nginx bind -y ..........//省略安装过程
[root@localhost sbin]# vim /etc/named.conf options { listen-on port 53 { any; }; //127.0.0.1改为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; }; //localhost改为any [root@localhost sbin]#
[root@localhost sbin]# vim /etc/named.rfc1912.zones zone "old.com" IN { //添加一个区域信息 type master; file "old.com.zone"; allow-update { none; }; }; [root@localhost sbin]#
[root@localhost sbin]# cd /var/named/ [root@localhost named]# ls data dynamic named.ca named.empty named.localhost named.loopback slaves [root@localhost named]# cp -p named.localhost old.com.zone //复制模板并命名 [root@localhost named]# vim old.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.52.132 //添加解析地址 [root@localhost named]#
[root@localhost named]# systemctl start named //开启dns服务 [root@localhost named]# systemctl stop firewalld.service //关闭防火墙 [root@localhost named]# setenforce 0 //关闭增强性安全功能 [root@localhost named]# systemctl start nginx //开启Nginx服务 [root@localhost named]# netstat -ntap | grep 80 //查看端口 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 51197/nginx: master [root@localhost named]#
现在公司旧域名 www.old.com 有业务需求有变更;
需要使用新 域名 www.new.com 代替,但是旧域名不能废除;
需要跳转到新域名上,而且后面的参 数保持不变。
[root@localhost named]# cd /etc/nginx/ [root@localhost nginx]# vim conf.d/default.conf #修改配置文件 server { listen 80; server_name www.old.com; #charset koi8-r; access_log /var/log/nginx/www.old.com-access.log main; location / { if ($host = "www.old.com") { #匹配如果域名是老域名 rewrite ^/(.*)$ http://www.new.com/$1 permanent; #则永久设置跳转新域名 } root /usr/share/nginx/html; index index.html index.htm; }
[root@localhost nginx]# vim /etc/named.rfc1912.zones zone "new.com" IN { type master; file "new.com.zone"; #new区域数据配置文件 allow-update { none; }; }; [root@localhost nginx]# cd /var/named/ [root@localhost named]# ls data dynamic named.ca named.empty named.localhost named.loopback old.com.zone slaves [root@localhost named]# cp -p old.com.zone new.com.zone #复制区域数据配置文件为kgc的数据配置文件 [root@localhost named]# systemctl restart named #重启解析服务 [root@localhost named]# systemctl restart nginx #重启nginx服务 [root@localhost named]#
所有IP 访问任何内容都显示一个固定维护页面,只有公司 IP :192.168.52.129访问正常。
[root@localhost named]# vim /etc/nginx/conf.d/default.conf server { listen 80; server_name www.old.com; #charset koi8-r; access_log /var/log/nginx/www.old.com-access.log main; #设置是否合法的IP标志 set $rewrite true; #判断是否为合法IP if ($remote_addr = "192.168.52.128"){ #win10主机ip为192.168.52.129,此时129为不合法ip set $rewrite false; } #非法IP进行判断打上标记 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; }
[root@localhost named]# cd /usr/share/nginx/html/ #切换到站点中 [root@localhost html]# ls 50x.html index.html [root@localhost html]# vim main.html #编辑非法IP访问网页内容 <h2>this is error page</h2> [root@localhost html]# systemctl restart nginx #重启Nginx服务 [root@localhost html]#
[root@localhost html]# vim /etc/nginx/conf.d/default.conf #判断是否为合法IP if ($remote_addr = "192.168.52.129"){ #将合法ip改为win10主机IP地址 set $rewrite false; } [root@localhost html]# systemctl restart nginx #重启服务 [root@localhost html]#
例如现在访问的是 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.old.com; #修改服务名称 #charset koi8-r; access_log /var/log/nginx/www.old.com-access.log main; location /post { #用location匹配post目录 rewrite (.+) http://www.old.com/bbs$1 permanent; #永久重定向跳转 } location / { root /usr/share/nginx/html; index index.html index.htm; }
[root@localhost html]# cd /var/named/ [root@localhost named]# ls data dynamic named.ca named.empty named.localhost named.loopback new.com.zone old.com.zone slaves [root@localhost named]# vim old.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.52.132 #www改为bbs [root@localhost named]# systemctl restart named #重启解析服务 [root@localhost named]# systemctl restart nginx #重启Nginx服务 [root@localhost named]#
http://www.accp.com/100-(100|200)-100.html 跳转到 http://www.accp.com 页面。
[root@localhost named]# vim /etc/nginx/conf.d/default.conf server { listen 80; server_name www.old.com; #charset koi8-r; access_log /var/log/nginx/www.old.com-access.log main; if ($request_uri ~ ^/100-(100|200)-(\d+).html$) { #匹配正则开头为100-(100|200)-一次多次的整数html为结尾的 rewrite (.*) http://www.old.com permanent; #永久重定向跳转到主页 }
[root@localhost named]# vim /var/named/old.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.52.132 [root@localhost named]# systemctl restart named #重启解析服务 [root@localhost named]# systemctl restart nginx #重启解析服务 [root@localhost named]#
[root@localhost named]# vim /etc/nginx/conf.d/default.conf #修改默认配置文件 server { listen 80; server_name www.old.com; #charset koi8-r; access_log /var/log/nginx/www.old.com-access.log main; location ~* /upload/.*\.php$ { #匹配不分大小写,匹配upload后零次或多次以.php为结尾的 rewrite (.+) http://www.old.com permanent; #跳转到首页 } [root@localhost named]# systemctl restart nginx #重启Nginx服务 [root@localhost named]#
[root@localhost named]# vim /etc/nginx/conf.d/default.conf #修改Nginx默认配置文件 server { listen 80; server_name www.old.com; #charset koi8-r; access_log /var/log/nginx/www.old.com-access.log main; location ~* ^/abc/123.html { #匹配某一个特定的网页 rewrite (.+) http://www.old.com permanent; #跳转到首页 } location / { root /usr/share/nginx/html; index index.html index.htm; } [root@localhost named]# systemctl restart nginx #重启Nginx服务 [root@localhost named]#
关于Nginx中如何进行rewrite模块应用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。