Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to apply rewrite Module in Nginx

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to apply the rewrite module in Nginx. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Experimental environment

Linux CentOS7 Virtual Machine (IP:192.168.52.132)

Win10 virtual machine (as a test machine)

1. Install Nginx and bind service [root@localhost ~] # rpm- Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm get http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm warning: / var/tmp/rpm-tmp.PtKluI: header V4 RSA/SHA1 Signature Key ID 7bd9bf62: NOKEY in preparation. # # [100%] upgrading / installing... 1:nginx-release-centos-7-0.el7.ngx # # # [root@localhost] # yum install nginx bind-y. / / omit the installation process 2. Modify the main configuration file named.conf [root@localhost sbin] # vim / etc/named.confoptions {listen-on port 53 {any }; / 127.0.0.1 change to 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 changed to any [root@localhost sbin] # 3, modify the zone profile named.rfc1912.zones [root@localhost sbin] # vim / etc/named.rfc1912.zoneszone "old.com" IN {/ / add a zone information type master File "old.com.zone"; allow-update {none;};} [root@localhost sbin] # 4 / modify the zone data configuration file [root@localhost sbin] # cd / var/named/ [root@localhost named] # lsdata dynamic named.ca named.empty named.localhost named.loopback slaves [root@localhost named] # cp-p named.localhost old.com.zone / / copy template and name [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.1www IN A 192.168.52.132 / / add resolution address [root@localhost named] # 5, Enable dns service and Nginx service [root@localhost named] # systemctl start named / / enable dns service [root@localhost named] # systemctl stop firewalld.service / / turn off firewall [root@localhost named] # setenforce 0 / / disable enhanced security function [root@localhost named] # systemctl start nginx / / enable Nginx service [root@localhost named] # netstat-ntap | grep 80 / / View port tcp 000.0. 0.0root@localhost named 80 0.0.0.0 LISTEN 51197/nginx: master [root@localhost named] # 6, Set the dns address of the win10 host to 192.168.52.132

7. Use the win10 host to test whether it can be parsed (successfully)

Application example 1 Mui-Domain name-based Jump

Now the company's old domain name www.old.com has business needs and changes.

Need to use the new domain name www.new.com instead, but the old domain name cannot be abolished

You need to jump to the new domain name, and the subsequent parameters remain the same.

1. Modify the configuration file and set the domain name redirection [root@localhost named] # cd / etc/nginx/ [root@localhost nginx] # vim conf.d/default.conf # modify the configuration file 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") {# match if the domain name is the old domain name rewrite ^ / (. *) $http://www.new.com/$1 permanent; # then permanently set the jump to the new domain name} root / usr/share/nginx/html; index index.html index.htm Add a new domain name resolution [root@localhost nginx] # vim / etc/named.rfc1912.zones zone "new.com" IN {type master; file "new.com.zone"; # new zone data configuration file allow-update {none;};} [root@localhost nginx] # cd / var/named/ [root@localhost named] # lsdata dynamic named.ca named.empty named.localhost named.loopback old.com.zone slaves [root@localhost named] # cp-p old.com.zone new.com.zone# replication zone data configuration file is kgc data configuration file [root@localhost named] # systemctl restart named # restart resolution service [root@localhost named] # systemctl restart nginx # restart nginx service [root@localhost named] # 3, access with the old domain name View Jump

4. Add parameters to the old domain name to check whether there are parameters when jumping to the new domain name.

Application example 2Mel-access redirection based on client IP

All IP access to anything displays a fixed maintenance page, and only company IP: 192.168.52.129 access is normal.

1. Check the IP address of win10 host

2. Modify the Nginx default configuration file [root@localhost named] # vim / etc/nginx/conf.d/default.confserver {listen 80; server_name www.old.com; # charset koi8-r; access_log / var/log/nginx/www.old.com-access.log main; # to set the legal IP flag set $rewrite true # determine whether it is a legitimate IP if ($remote_addr = "192.168.52.128") {# win10 host ip is 192.168.52.129, then 129 is illegal ip set $rewrite false;} # illegal IP to judge and mark if ($rewrite = true) {rewrite (. +) / main.html } # match tag to jump to site location = / main.html {root / usr/share/nginx/html;} location / {root / usr/share/nginx/html; index index.html index.htm 3. Create illegal IP site and main web page [root@localhost named] # cd / usr/share/nginx/html/ # switch to [root@localhost html] # ls50x.html index.html [root@localhost html] # vim main.html # Edit illegal IP access web content this is error page [root@localhost html] # systemctl restart nginx # restart Nginx service [root@localhost html] # 4, win10 host visits web page (in this case win10ip is illegal ip)

5. Modify the Nginx default configuration file [root@localhost html] # vim / etc/nginx/conf.d/default.conf # to determine whether it is a legitimate IP if ($remote_addr = "192.168.52.129") {# change the legitimate ip to the win10 host IP address set $rewrite false;} [root@localhost html] # systemctl restart nginx # restart the service [root@localhost html] # 6, and use the win10 host to visit the web again

Application example 3mi-based on the old domain name, jump to the new domain name and add a directory after it.

For example, if you are accessing http://bbs.accp.com, you need to redirect all the posts under this domain name to http://www.accp.com/bbs. Pay attention to keeping the parameters of the domain name unchanged after the jump.

1. Modify the Nginx default configuration file [root@localhost html] # vim / etc/nginx/conf.d/default.conf # modify the default configuration file server {listen 80; server_name bbs.old.com; # modify the service name # charset koi8-r; access_log / var/log/nginx/www.old.com-access.log main Location / post {# match post directory rewrite (. +) http://www.old.com/bbs$1 permanent; # permanent redirect} location / {root / usr/share/nginx/html; index index.html index.htm with location 2. Modify dns's zone data configuration file (old.com.zone) [root@localhost html] # cd / var/named/ [root@localhost named] # lsdata dynamic named.ca named.empty named.localhost named.loopback new.com.zone old.com.zone slaves [root@localhost named] # vim old.com.zone # modify the zone data configuration file $TTL 1D @ IN SOA @ rname.invalid. (0; serial 1D; refresh 1H; retry 1W; expire 3H) Minimum NS @ A 127.0.0.1bbs IN A 192.168.52.132 # www change to bbs [root@localhost named] # systemctl restart named # restart resolution service [root@localhost named] # systemctl restart nginx # restart Nginx service [root@localhost named] # 3, test the web page with the win10 host

Application example 4 Mel-Jump based on parameter matching

Http://www.accp.com/100-(100|200)-100.html jumps to the http://www.accp.com page.

1. Modify Nginx default configuration file [root@localhost named] # vim / etc/nginx/conf.d/default.confserver {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 $) {# match regular beginning with 100-(100 | 200)-multiple integer html ending with rewrite (. *) http://www.old.com permanent # permanently redirect to the home page} 2, modify the dns zone data configuration file [root@localhost named] # vim / var/named/old.com.zone # modify the zone data configuration file $TTL 1D @ IN SOA @ rname.invalid. (0; serial 1D; refresh 1H; retry 1W; expire 3H) Minimum NS @ A 127.0.0.1www IN A 192.168.52.132 [root@localhost named] # systemctl restart named # restart the resolution service [root@localhost named] # systemctl restart nginx # restart the resolution service [root@localhost named] # 3, test the web page with the win10 host

Application example 5 listen-based on all files ending with php in the directory to jump 1, modify Nginx default configuration file [root@localhost named] # vim / etc/nginx/conf.d/default.conf # modify default configuration file 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 ${# matches rewrite (. +) http://www.old.com permanent; # ending with .php zero or more times after matching upload, regardless of case, to the home page} [root@localhost named] # systemctl restart nginx # restart the Nginx service [root@localhost named] # 2, test the web page with the win10 host

Application example 6 url-based on the most common url request to jump to the home page 1, modify the Nginx default configuration file [root@localhost named] # vim / etc/nginx/conf.d/default.conf # modify the Nginx default configuration file 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 {# match a specific web page rewrite (. +) http://www.old.com permanent; # Jump to the home page} location / {root / usr/share/nginx/html; index index.html index.htm;} [root@localhost named] # systemctl restart nginx # restart the Nginx service [root@localhost named] # 2, test the web page with the win10 host

On how to use the rewrite module in Nginx to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report