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

Nginx Rewrite module application

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

Share

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

Application scenario 1: www.accp.com-the old domain name of a jump company based on domain name needs to be replaced by a new domain name www.kgc.com due to changes in business requirements.

1. The old domain name cannot be abolished.

two。 Jump from the old domain name to the new domain name and keep its parameters unchanged

Deployment environment A Linux server (192.168.142.130) a test host windows 71, install 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 [root@localhost ~] # yum install nginx-y # yum install nginx2 Modify nginx default configuration file [root@localhost ~] # vim / etc/nginx/conf.d/default.conf # # modify 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 bind parsing service [root@localhost ~] # yum install bind-y4, modify the main configuration file (named.conf) [root@localhost ~] # vim / etc/named.conf options {listen-on port 53 {any;}; # # listen on all Allow-query {any;}; # # allow all 5, modify the zone configuration file (named.rfc1912.zones) [root@localhost ~] # vim / etc/named.rfc1912.zones # # configure the zone profile zone "accp.com" IN {type master; file "accp.com.zone" # # accp region data configuration file allow-update {none;};} 6. Modify the zone data configuration file (accp.com.zone) [root@localhost ~] # cd / var/named/ [root@localhost named] # cp-p named.localhost accp.com.zone # # copy template [root@localhost named] # vim accp.com.zone # # modify 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.142.130 # # Local address [root@localhost named] # systemctl start named # # enable dns service [root@localhost named] # systemctl stop firewalld.service # # turn off firewall [root@localhost named] ] # setenforce 0 [root@localhost named] # systemctl start nginx # # enable nginx service 7 Test the web page with a testing machine

8. Modify the configuration file, set the domain name redirection [root@localhost named] # vim / etc/nginx/conf.d/default.conf # # modify the 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 / {if ($host = "www.accp.com") {# # match if the domain name is an old domain name rewrite ^ / (. *) $http://www.kgc.com/$1 permanent # # permanently set to jump to a new domain name} root / usr/share/nginx/html; index index.html index.htm;} 9, and add a new domain name resolution [root@localhost named] # vim / etc/named.rfc1912.zones zone "kgc.com" IN {type master File "kgc.com.zone"; # # accp zone data configuration file allow-update {none;};} [root@localhost named] # cp-p / var/named/accp.com.zone / var/named/kgc.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 10, access with the old domain name, view web page redirection

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

Application scenario 2 IP-based on client IP access to jump company business version online, all IP access anything shows a fixed maintenance page, only company IP access normal 1, modify Nginx 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 legal IP flag set $rewrite true; # # sets the variable to true # determines whether it is a legitimate IP if ($remote_addr = "192.168.142.120") {set $rewrite false # # match legal IP, set variable to false, normal jump page} # illegal IP to judge and mark if ($rewrite = true) {# # match illegal IP, jump to main's web page rewrite (. +) / main.html } # match tag to redirect 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 illegal IP site and main web page [root@localhost conf.d] # cd / usr/share/nginx/html/ # # switch to site [root@localhost html] # vim main.html # # Edit illegal IP access web content this is test web [root@localhost html] # systemctl restart nginx # # restart Nginx service 3, visit the test web page

Application scenario 3Mui-based on the old, the new domain name is redirected and the directory is added. All the posts under the domain name http://bbs.accp.com are redirected to http://www.accp.com/bbs and the parameters remain unchanged after the domain name jump. Modify the Nginx default configuration file [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 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 with location # # permanent redirect} 2, modify dns's zone data configuration file (accp.com.zone) [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.142.130 [root@localhost named] # systemctl restart named # # restart the resolution service [root@localhost named] # systemctl restart nginx # # restart the Nginx service [root@localhost named] # echo "nameserver 192.168.142.130" > / etc/resolv.conf # # put the resolution server address in the local resolution configuration file 3 Test the web page

Application scenario 4Mel-Redirect browser access based on parameter matching: http://www.accp.com/100-(100|200)-100.html jumps to http://www.accp.com page 1 and modifies the Nginx 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 # # permanent Redirect to Home Page} 2 Modify dns zone data configuration file [root@localhost conf.d] # vim / var/named/accp.com.zone # # modify zone data configuration file www IN A 192.168.142.130 [root@localhost conf.d] # systemctl restart named # # restart resolution service [root@localhost conf.d] # systemctl restart nginx # # restart Nginx service 3, test web page

Application scenario 5 PHP-based on all PHP files in the directory jump access http://www.accp.com/upload/1.php jump to home page 1, modify the Nginx default configuration file [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 upload regardless of case # # Redirect to the home page} [root@localhost conf.d] # systemctl restart nginx # # restart Nginx service 2 and test the web page

Application scenario 6 url-based on the jump of the most common url request, visit a specific page to jump to page 1, modify the Nginx default configuration file [root@localhost ~] # cd / etc/nginx/conf.d/ [root@localhost conf.d] # vim default.conf # # modify the 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 {# # match a specific web page rewrite (. +) http://www.accp.com permanent; # # Jump to the home page} [root@localhost conf.d] # systemctl restart nginx # # restart Nginx service 2 to test the web page

Thank you for reading!

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