In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Rewrite definition
The rewrite function is to use global variables provided by nginx or variables set by yourself, combined with regular expressions and flag bits to achieve url rewriting and redirection.
The rewrite loop can be executed up to 10 times. If it exceeds nginx, 500error will be returned.
Support for pcre language, regular expressions, support for rewriting module set instructions
Rewrite syntax
Rewrite regex replacement [flag]
Flag tags supported by rewrite for content after regular jump
If relative domain names or parameter strings work, you can use global variable matching or proxy_pass reverse proxies. From the above shows that rewrite and location function is a bit like, can achieve jump. The main difference is that rewrite changes the path to obtain resources within the same domain name, while location controls access or reverse proxies for a class of paths and can proxy_pass to other machines. In many cases, rewrite is also written in location, and the order in which they are executed is:
1 execute the rewrite instruction of the server block
2 perform location matching
3 execute the rewrite instruction in the selected location
If one of the steps URI is rewritten, cycle through 1-3 until a real file is found; if you loop more than 10 times, a 500Internal Server Error error is returned.
Rewrite practical scenario
Use rewrite for matching jumps
Jump after matching global variables with if
Use location to match and then jump
Rewrite is placed in sever {}, if {}, location {} segments
For a domain name or parameter string
Use if global variable matching
Use proxy_pass reverse proxy
Regular expression metacharacters commonly used in rewrite
Flag tag description
Comparison between last and break
Classification of location detailed explanation
Location = patt {} [exact match]
Location = patt {} [general match]
Location ~ patt {} [regular match]
Common expressions for regular matching
Expressions of the same type of location priority, long strings will be arranged by priority.
= Typ
^ ~ Type expression
Regular expression (~ and ~ *) types
Regular string matching type, matching by prefix
Universal match (/). If there is no other match, any request will match to
Compare the similarities between rewrite and location
Can realize the jump.
Differences
Rewrite actually changes the path to get resources within the same domain name.
Location is used to control access or reverse proxy for a class of paths, and can also proxy_ to other machines.
Rewrite will be written in location, in order of execution.
Execute the rewrite instruction in the server block
Perform location matching
Execute the rewrite instruction in the selected location
Three message level structures of http server event
Priority actual column location = / {[configuration A] # # exact match /, hostname cannot be followed by any string} location / {[configuration B] # # all addresses begin with /, this rule will match all requests, but regular and longest strings will match} location / documents/ {[configuration C] # # to match any address that begins with / documents/ It only works when the following regular expression does not match.} location ~ / documents/abc {[configuration D] # # matches any address that begins with / documents/abc. When the following regular expression does not match, it works.} location ^ ~ / images/ {[configuration E] # # address starting with / images/, after matching symbol Stop matching down} location ~ *\. (gif | jpg | peg) ${[configuration F] # # matches all requests ending with gif,jpg or jpeg, and the picture under / images/ is processed because ^ ~ has a higher priority} location / images/abc {[configuration G] # the longest character matches / images/abc, and the lowest priority} location ~ / images/abc {[configuration H] # # begins with / images/abc Second priority} location / images/abc/1.html {[configuration I] # # if compared with regular ~ / images/abc/1.html, regular priority is higher} location priority rule matches a specific file
(location = full path) > (location ^ ~ full path) > (location ~ full path) > (location ~ directory) > (location directory) > (location /)
Use directory matching to access a file
(location = directory) > (location ^ ~ directory /) > (location ~ directory) > (location ~ * directory) > (location directory) > (location /)
Specific use scenarios-1. Domain name-based jump installation nginx official source [root@localhost] # rpm- Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpmRetrieving http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpmwarning: / var/tmp/rpm-tmp.VbwudT: Header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEYPreparing... # # [100%] Updating / installing... 1:nginx-release-centos-7-0.el7.ngx # # install nginx and configure nginx [root@localhost ~] # yum install nginx- y [root@localhost ~] # vim / etc/nginx/conf.d/default.conf 3 server_name www.chen.com # Line 3 add your old domain name 6 access_log / var/log/nginx/www.chen.com-access.log main; # Line 6 add your old domain name log message installation DNS service parsing [root@localhost] # yum install bind-y configuration DNS main configuration file [root@localhost ~] # vim / etc/named.conf 13 listen-on port 53 {any;}; 21 allow-query {any;} Configure the DNS zone profile [root@localhost ~] # vim / etc/named.rfc1912.zones zone "chen.com" IN {type master; file "chen.com.zone"; allow-update {none;};} Configure the DNS zone data configuration file [root@localhost ~] # cd / var/named/ [root@localhost named] # cp-p named.localhost chen.com.zone [root@localhost named] # cp-p chen.com.zone accp.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.136.181 [root@localhost named] # systemctl start named [root@localhost named] # systemctl stop firewalld.service [root@localhost named] # setenforce 0 [root@localhost named] # systemctl start nginx [root@localhost named] # netstat-ntap | grep nginxtcp 0 0 0.0.0. * LISTEN 81426/nginx: master goes to the client to test the page that accesses Nginx. First, the client DNS server selects the address of our DNS server.
Configure the Nginx main configuration file and convert the old domain name to the new domain name [root@localhost named] # vim / etc/nginx/conf.d/default.conflocation / {# Domain name Redirect if ($host = "www.chen.com") {# if you visit the old domain name rewrite ^ / (. *) $http://www.accp.com/$1 permanent # Jump to match multiple arbitrary characters that begin with / and end with a variable of $1. Permanently jump} root / usr/share/nginx/html; index index/html index.html;} # and then add the new domain name to the DNS service to resolve [root@localhost named] # vim / etc/nginx/conf.d/default.conf zone "accp.com" IN {type master; file "accp.com.zone" Allow-update {none;};}; [root@localhost named] # cp-p chen.com.zone accp.com.zone [root@localhost named] # systemctl restart named [root@localhost named] # systemctl restart nginx and then go to the client to verify access to the old domain name will be transferred to the new domain name
Specific use scenarios-- 2. Check the address of the client first based on the client IP access redirect. This address is illegal.
Configure the nginx main configuration file to delete the original 3dd [root@localhost ~] # vim / etc/nginx/conf.d/default.conf if ($host = "www.chen.com") {rewrite ^ / (.*) $http://www.accp.com/$1 permanent;} # set the legal IP flag set $rewrite true # determine whether it is a legitimate IPif ($remote_addr = "192.168.136.140") {# 140Let's set this address to a legitimate IP set $rewrite false;} # illegal IP to judge and mark if ($rewrite = true) {rewrite (. +) / main.html;} # to redirect the site location = / main.html {root / usr/share/nginx/html } [root@localhost ~] # systemctl restart nginx [root@localhost ~] # cd / usr/share/nginx/html/ [root@localhost html] # ls50x.html index.html to create a maintenance page (page accessed by an illegal address) [root@localhost html] # vim main.htmlthis is test web go to the client to test the illegal page first (the address of www.accp.com is 192.168.136.139)
Then test the legitimate page and change it to a fixed IP before you visit it.
Specific use scenarios-- 3. Jump to a directory after the new domain name based on the old domain name For example, if you are visiting http://bbs.accp.com, you need to jump all the posts under this domain name to http://www.accp.com/bbs[root@localhost html] # vim / etc/nginx/conf.d/default.conf## and delete the original 14dd 7 location / post {# match to / post to perform the following operations: 8 rewrite (. +) http://www.accp.com/bbs$1 permanent # any character, www.accp.com/bbs $1 adjusts the parameters of any previous character, permanently jump to 9} [root@localhost named] # cd / var/named/ [root@localhost named] # vim accp.com.zone bbs IN A 192.168.136.182 [root@localhost named] # systemctl restart nginx [root@localhost named] # systemctl restart named to the server website to test
Specific use scenarios-4. Jump based on parameter matching [root@localhost named] # vim / etc/nginx/conf.d/default.conf # remember to delete the original if ($request_uri ~ ^ / 100-(100 | 200)-(\ d +) .html $) {# $request_uri is a built-in variable, starting with 100 to 100 or 200,\ d + represents any number from 1 to 9, and then the html rewrite (. *) http://www.accp.com permanent at the end # just jump to. * the page representing 0 or more times of this domain name permanently} [root@localhost named] # vim accp.com.zone www IN A 192.168.136.182 [root@localhost named] # systemctl restart nginx [root@localhost named] # systemctl restart named to test on the client.
Specific use scenarios-- 5. Based on all the php files in the directory, jump to [root@localhost named] # vim / etc/nginx/conf.d/default.conf # to delete the original 3dd location ~ * / upload/.*\ .php ${# match regular is not case-sensitive, upload represents a directory. Matches all files at the end of php,\. Translation. The symbol rewrite (. *) http://www.accp.com permanent; # permanently jumps to the home page} [root@localhost named] # vim / etc/nginx/conf.d/default.conf [root@localhost named] # systemctl restart nginx to the client to test
Specific use scenarios-- 6. According to the specific page, jump to the home page [root@localhost named] # vim / etc/nginx/conf.d/default.conf # change the first item to a specific page set by yourself, location ~ * ^ / abc/123.html {# [root@localhost named] # systemctl restart nginx that starts with / abc/123.html, go to the client to test.
That's all we have.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.