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

An example of Nginx--rewrite application

2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Nginx--rewrite application example 1. Domain name-based jump

Now the company's old domain name www.old.com has business requirements and needs to be changed, so we need to use the new domain name www.new.com instead, but the old domain name cannot be abolished and needs to be transferred to the new domain name, and the subsequent parameters remain unchanged.

1. Install the Nginx service

[root@localhost ~] # rpm- Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm / / install nginx official source warning: / var/tmp/rpm-tmp.vS0k20: 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-y / / yum install nginx

2. Modify Nginx configuration file

[root@localhost ~] # vim / etc/nginx/conf.d/default.conf / / modify the default configuration file server {listen 80; server_name www.accp.com; / / modify hostname # charset koi8-r; access_log / var/log/nginx/www.accp.com-access.log main / / enable log service

3. Install DNS domain name resolution service

[root@localhost ~] # yum install bind-y

4. Modify the main configuration file

[root@localhost ~] # vim / etc/named.conf options {listen-on port 53 {any;}; / / listen on all addresses 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;}; / / allow all

5. Modify the zone configuration file

[root@localhost ~] # vim / etc/named.rfc1912.zones / / configure the zone profile zone "accp.com" IN {type master; file "accp.com.zone"; allow-update {none;};}

6. Modify the zone data configuration file

[root@localhost ~] # cd / var/named/ [root@localhost named] # cp-p named.localhost accp.com.zone// replication zone data profile template [root@localhost named] # vim accp.com.zone// modifies the zone configuration file $TTL 1D @ IN SOA @ rname.invalid. (1D; refresh 1H; retry 1W Expire 3H) Minimum NS @ A 127.0.0.1www IN A 192.168.131.133 / / Local address [root@localhost named] # systemctl start named / / enable the dns service [root@localhost named] # systemctl stop firewalld.service / / turn off the firewall [root@localhost named] # setenforce 0 [root@localhost named] # systemctl start nginx / / enable the nginx service [root@localhost named] # netstat-ntap | grep nginx / / View port tcp 0 0 0.0 0 root@localhost named 80 0 0 0 LISTEN 4093/nginx: master

7. Set the dns parsing address of the test machine to the virtual machine address, and use the test machine to test the web page

8. Modify the configuration file and set the domain name jump

[root@localhost named] # vim / etc/nginx/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 it is an old domain name rewrite ^ / (. *) $http://www.new.com/$1 permanent / / then permanently set to jump to the new domain name} root / usr/share/nginx/html; index index.html index.htm;}

9, add a new domain name resolution to the DNS configuration file

[root@localhost named] # vim / etc/named.rfc1912.zones zone "kgc.com" IN {type master; file "kgc.com.zone"; allow-update {none;};} [root@localhost named] # cp-p / var/named/accp.com.zone / var/named/kgc.com.zone// copy the original zone data configuration file to kgc.com 's zone data configuration file [root@localhost named] # systemctl restart named/ / restart the dns resolution service [root@localhost named] # systemctl restart nginx / / restart the nginx service

10. Visit with the old domain name to see if the web page jumps to the new domain name.

11. Add parameters to the old domain name to see if there are still parameters when you jump to the new domain name.

Application example 2. Access redirection based on client IP

For example, when the company's business version is online today, any content accessed by IP displays a fixed maintenance page, only the company's IP: 192.168.131.128 access is normal.

1. Modify the default configuration file

[root@localhost ~] # cd / etc/nginx/conf.d/ [root@localhost conf.d] # vim default.conf server {listen 80; server_name www.accp.com; # charset koi8-r; access_log / var/log/nginx/www.accp.com-access.log main; / / sets the IP flag set $rewrite true that allows access / / set the variable to true / / determine whether it is the IP if allowed to access ($remote_addr = "192.168.131.128") {set $rewrite false / / match legal IP, set the variable to false, normal jump page} / / IP that is not allowed to access will judge and mark if ($rewrite = true) {/ / match illegal IP, and jump to the maintenance page rewrite (. +) / main.html. } # match tag to jump site location = / main.html {/ / exactly match root / usr/share/nginx/html; / / site path} location / {root / usr/share/nginx/html; index index.html index.htm;}

2. Create IP sites that are not allowed to visit and maintain web pages

[root@localhost conf.d] # cd / usr/share/nginx/html/ switch to the site [root@localhost html] # vim main.html / / Edit illegal IP access to web content this is test web [root@localhost html] # systemctl restart nginx/ / restart Nginx service

3. Use the page that pops up when access to IP is not allowed

4. Pages that can be accessed during legal IP access

Application example 3. Jump to the new domain name and add a directory based on the old domain name

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 default configuration file of Nginx

[root@localhost ~] # cd / etc/nginx/conf.d/ [root@localhost conf.d] # vim default.conf / / modify the default configuration file server {listen 80; server_name bbs.accp.com; / / modify the service name # charset koi8-r; access_log / var/log/nginx/www.accp.com-access.log main Location / post {/ / match post directory rewrite (. +) http://www.accp.com/bbs$1 permanent; / / permanent redirect} with location

2. Modify the zone data configuration file accp.com.zone of dns

[root@localhost conf.d] # cd / var/named/ [root@localhost named] # vim accp.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.131.133 [root@localhost named] # systemctl restart named / / restart dns Domain name Resolution Service [root@localhost named] # systemctl restart nginx / / restart Nginx Service [root@localhost named] # echo "nameserver 192.168.13.144" > / etc/resolv.conf / / Put the resolution server address in the local resolution configuration file

Entering the post directory and parameters will jump to bbs and the following parameters. Here, the parameters after the domain name cannot be displayed due to the version of the browser.

Application example 4. Jump based on parameter matching

For example, if you are interviewing http://www.accp.com/100-(100|200)-100.html, you will jump to the http://www.accp.com page.

1. Modify the default configuration file

[root@localhost ~] # cd / etc/nginx/conf.d/ [root@localhost conf.d] # vim default.conf server {listen 80; server_name www.accp.com; # charset koi8-r; access_log / var/log/nginx/www.accp.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.accp.com permanent; # # permanently redirect to home page}

2. Modify the zone data configuration file accp.com.zone of dns

[root@localhost conf.d] # vim / var/named/accp.com.zone / / modify zone data configuration file www IN A 192.168.131.133 [root@localhost conf.d] # systemctl restart named/ / restart dns domain name resolution service [root@localhost conf.d] # systemctl restart nginx / / restart Nginx service

Enter 100,100-99.html at the end of the page to jump to the accp page home page

Application example 5. Jump files at the end of all php in the directory

Visit http://www.accp.com/upload/1.php to jump to the home page

Modify Nginx default profile

[root@localhost ~] # cd / etc/nginx/conf.d/ [root@localhost conf.d] # vim default.conf / / modify the default configuration file server {listen 80; server_name www.accp.com; # charset koi8-r; access_log / var/log/nginx/www.accp.com-access.log main Location ~ * / upload/.*\ .php ${/ / matches rewrite (. +) http://www.accp.com permanent that ends with .php zero or more times after matching upload regardless of case. / / Jump to the home page} [root@localhost conf.d] # systemctl restart nginx / / restart the Nginx service

Visit / upload/ pages that end with .php to jump to the accp home page

Application example 6. Jump based on the most common url request

Visit a specific page and jump to the home page

Modify Nginx default profile

[root@localhost ~] # cd / etc/nginx/conf.d/ [root@localhost conf.d] # vim default.conf / / modify Nginx default configuration file server {listen 80; server_name www.accp.com; # charset koi8-r; access_log / var/log/nginx/www.accp.com-access.log main Location ~ * ^ / abc/123.html {/ / matches a specific web page rewrite (. +) http://www.accp.com permanent; / / jumps to the home page} [root@localhost conf.d] # systemctl restart nginx / / restart the Nginx service

Visit a specific web page to jump back to the accp home page

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