preface
It consists of kernel and Nginx module.
Nginx itself does little work. When it receives an HTTP request, it just maps the request to a location block by looking up the configuration file, and each instruction configured in the location will start different modules to complete the work. Therefore, the module can be regarded as the real labor worker of nginx.
Usually, the instructions in a location will involve one handler module and multiple filter modules (of course, multiple locations can reuse the same module). The handler module is responsible for processing the request and generating the response content, while the filter module processes the response content. The modules developed by users according to their own needs belong to the third-party module. It is with the support of so many modules that the function of Nginx will be so powerful.
1: Rewrite
1.1: Rewrite jump scene
The URL looks more standardized and reasonable
Enterprises will disguise dynamic URL addresses as static addresses to provide services
After changing the domain name of the website, let the old access jump to the new domain name
Some business adjustments on the server
1.2: Rewrite jump implementation
1.3: Rewrite practical scenario
Implementation of Nginx jump requirements
Match jump using rewrite
Jump after matching global variables with if
Use location to match and then jump
rewrite is placed in the server {}, if {}, location {} sections
For domain name or parameter string
Use if global variable matching
Using proxy_pass reverse proxy
1.4. Regular expression metacharacter
2: Rewrite command
2.1: rewrite syntax
2.2 flag mark
2.3 comparison between last and break
3: location
3.1: classification of location
location = patt {} [Accurate matching] ocation patt {} [General matching ] location ~ patt {} [Regular matching]
3.1.1: common expressions for regular matching
3.2 priority of location
For expressions of the same type, the long string will be matched first
By priority
= type ^~ Type expression Regular expressions (and*)type General string matching type, matching by prefix Universal matching(/),If there is no other match, any request will match
3.3: differences between rewrite and location
Same point Can jump difference rewrite Is to change the path to obtain resources within the same domain name location It is used to control access or reverse proxy for a class of paths. It can also proxy_pass To other machines write Will write in location Execution sequence implement server Inside the block rewrite instructions implement location matching Execute the selected location Medium rewrite instructions
3.4: location priority rule
Match a specific file ( location = (full path)>( location ^~ (full path)>( location ~* (full path)>( location ~ (full path)>( location (full path)>( location /) Use directory matching to access a file ( location = Table of contents)>( location ^~ Table of contents)>( location ~ Table of contents)> ( location ~* Table of contents)>( location Table of contents)>( location /)
3.5: example of location priority
location = / { '//Exact match /, host name cannot be followed by any string ' [configuraion A ] } location / { '//All addresses start with /, this rule will match all requests, but regular and longest strings will match first ' [configuraion B ] } location /documents/ { '//Match any address starting with / documents /, and it will work only when the following regular expression does not match. ' [configuraion C ] } location ~ /documents/abc { '//Match any address starting with / documents/abc. It will work only when the following regular expression does not match. ' [configuraion D ] } location ^~ /images/ { '//For addresses starting with / images /, stop matching after matching " [configuraion E ] } location ~*\.(gif|jpg|gpeg)$ { '//Match all requests ending in gif, jpg or jpeg, and the pictures under Images / will be processed by [configuration E], because ^ ~ has higher priority. ' [configuraion F ] } location /images/abc { '//Match the longest character to / images/abc, with the lowest priority ' [configuraion G ] } location ~ /images/abc { '//'priority level for those starting with / Images/abc' [configuraion H ] } location /images/abc/1.html { '//If and regular ~ images / ABC / 1 Htm has higher priority than regular ' [configuraion I ]
IV Specific six configuration experiments
Environmental preparation
Manually compile and install nginx (this nginx is used in the experiment)
Or install nginx using yum or up2date
##Install nginx using yum or up2date rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm '// 'install nginx source' yum install nginx -y
VMware software
One CentOS 7 server (IP address 192.168.100.55)
A Windows host (IP address 192.168.100.66)
4.1: domain name based jump
4.1.1: demand
Now the old domain name of the company is www.old Com has changed its business requirements and needs 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 following parameters remain unchanged
[root@ shanan ~]# vim /etc/nginx/conf.d/default.conf
if ($host = 'www.old.com'){ rewrite ^/(.*)$ http://www.new.com/$1 permanent; }
[root@ shanan ~]# vim /etc/named.conf
[root@ shanan ~]# vim /etc/named.rfc1912.zones
[root@ shanan ~]# cd /var/named [root@ shanan named]# cp -p named.localhost old.com.zone [root@ shanan named]# cp -p named.localhost new.com.zone
4.2 jump based on client IP access
4.2.1 requirements:
Today, the company's business version is online, and any visitor will jump to a fixed maintenance page. Only the company's IP (192.168.100.60) can access normally
server { listen 80; server_name www.old.com; #charset koi8-r; access_log /var/log/nginx/www.old.com-access.log main; set $rewrite true; if ( $remote_addr = "192.168.100.66"){ set $rewrite false; } if ( $rewrite = true ){= rewrite (.+) /error.html; } location = /error.html { root /usr/share/nginx/html; }
[root@ shanan html]# ls 50x.html error.html index.html [root@ shanan html]# Vi error.html
The address is illegal. You don't have permission to access, BYE
Access test:
4.3 jump and add directory based on old and new domain names
4.3.1 requirements:
Based on the old domain name, jump to the new domain name followed by the directory. For example, what you are visiting now is htp://bbs.old.com/post , now you need to jump all the posts under this domain name to http://www.old.com/bbs Pay attention to keep the parameters after domain name jump unchanged
server { listen 80; server_name bbs.old.com; #charset koi8-r; access_log /var/log/nginx/www.old.com-access.log main; location /post { rewrite (.+) http://www.old.com/bbs$1 permanent; } location / { root /usr/share/nginx/html; index index.html index.htm; }
4.4: jump based on parameter matching
4.4.1: demand
For example, visit now http://www.old.com/100- (100|200)-100.html jump to http://www.new.com page
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$ { rewrite (.+) http://www.old.com permanent; } location / { root /usr/share/nginx/html; index index.html index.htm; }
4.5: jump based on the file with the end of php file in the directory upload
4.5.1: demand
Visit a shopping page without login and jump to the home page
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$ { rewrite (.+) http://www.old.com permanent; } location / { root /usr/share/nginx/html; index index.html index.htm; }
4.6: jump based on the most common URL request
4.6.1: demand
Visit a specific page and jump to the home page
server { listen 80; server_name www.old.com; #charset koi8-r; access_log /var/log/nginx/www.old.com-access.log main; location ~* ^/abc/test.html { rewrite (.+) http://www.old.com permanent; } location / { root /usr/share/nginx/html; index index.html index.htm; }