Nginx Location and Rewrite

Table of contents

Introduction to Rewrite

1.0 Rewrite actual scene

        1.1 Rewrite jump scene

        1.2 Rewrite jump implementation

        1.3 Nginx regular expressions

        1.4 Rewrite command && syntax format

        1.5 flag mark description 

2 Location classification

         2.1 Location priority

3 Rewrite&&Location comparison

4 Scene jump experiment

4.1 Jump based on domain name


Introduction to Rewrite

The rewrite function is to use the global variables provided by nginx or the variables set by yourself, combined with regular expressions and tag bits to realize URL rewriting and redirection.
For example: after changing the domain name, it is necessary to keep the old domain name able to jump to the new domain name, a certain web page needs to jump to the new page, anti-leeching of the website, etc.

rewrite can only be placed in server{}, location{}, if{}, and by default it can only work on the string after the domain name except the passed parameters

Example: https://mp.csdn.net/mp_block/creation/editor?spm=1000.2415.9901.5352

Can only match characters other than domain name and parameters /mp_block/creation/editor

1.0 Rewrite actual scene

  • Implementation of Nginx jump requirements
  1. Use rewrite for matching jump
  2. Use if to jump after matching global variables
  3. Use location to match and then jump
  • rewrite is placed in the server{}, if{}, location{} segments
  1. location only works on the string after the domain name except the passed parameters
  • For domain names or parameter strings
  1. Use if global variable to match
  2. Use proxy_pass reverse proxy

 

1.1 Rewrite jump scene

  • Use rewrite for matching jump
  • Use if to jump after matching global variables
  • Use location to match and then jump

1.2 Rewrite jump implementation

  • Nginx: supports URL rewriting through the ngx_http_rewrite_module module, only supports if condition judgment , but does not support else
  • Jump: jump from one location to another location, the cycle can be executed up to 10 times, nginx will return 500 error after exceeding
  • PCRE support: syntax rule matching for perl-compatible regular expressions
  • Override the module set directive: create a new variable and set its value

 

1.3 Nginx regular expressions

symbol effect
~ Perform a regex match, case sensitive
~* Perform a regex match, case insensitive
!~ Perform a regex match that neither matches all uppercase nor matches all lowercase
!~* mismatch case insensitive

  ^~

Prefix matching, ordinary character matching, only matches the first successful result

= exact match
@ Define a named Location, used when targeting internally
{n} n represents the number of matches
{n,} matches n or more times
{n,m} Repeat n to m times
[ ] Defines the range of characters to match
[c] matches a single character c
[a-z] Matches any of the az lowercase letters
[a-zA-Z0-9] matches all upper and lower case letters or numbers
() where the expression starts and ends
| or operator

1.4 Rewrite command && syntax format

rewrite    <regex>    <replacement>          [flag]
          正则表达式    跳转后的内容      rewrite支持的flag标记

1.5 flag mark description 

  • last : After the matching of this rule is completed, the rewritten url matching will not be terminated. It is generally used in server and if.

  • break : This rule is terminated after the matching is completed, and the rewritten url matching is terminated. It is generally used in location.

  • redirect : Return 302 temporary redirection, and the browser address will display the redirected URL address.

  • permanent : Return 301 permanent redirection, and the browser address bar will display the redirected URL address

  • set: set a variable after the current rule matches successfully, and pass the variable to the next rule

2 Location classification

  • Location can be roughly divided into five categories:
  • Exact match location = / {...}    
  •  
  • prefix matches location ^~ {...}
  •  
  • Generally match location / {...} 
  •  
  • Regex matches location ~ / {...}
  •  In the configuration file of nginx.conf, the location uses the general matching "location /{...} by default.

2.1 Location priority

 

3 Rewrite&&Location comparison

rewrite: perform local page redirection on the accessed domain name or the URL path address within the domain name
location: perform access control on the accessed path or perform URL redirection across servers

In terms of function, rewrite and location seem a bit similar, both of which can realize jumping. The main difference is that rewrite is to change the path to obtain resources within the same domain name, while location is to control access or reverse proxy for a type of path, and proxy_pass can also be used to other machines.

4 Scene jump experiment

4.1 Jump based on domain name

 Now the company’s old domain name www.lbj.com has changed business requirements and needs to be replaced by the new domain name www.jokic.com, but the old domain name cannot be abolished and needs to be redirected to the new domain name, and the parameters behind it remain unchanged.

 


   vim /usr/local/nginx/conf/nginx.conf
server {
	listen       80;
	server_name  www.lbj.com;			
	charset utf-8;
	access_log  /var/log/nginx/www.lbj.com-access.log;		
	location / {
	#添加域名重定向
        if ($host = 'www.lbj.com'){						
			rewrite ^/(.*)$ http://www.jokic.com/$1 permanent;	
        }
        root   html;
        index  index.html index.htm;
    }
}


echo "192.168.181.101 www.lbj.com www.jokic.com" >> /etc/hosts
systemctl restart nginx

Browser input simulates access to http://www.lbj.com/test/1.html (although the content of this request does not exist)
will jump to www.jokic.com/test/1.html, you can see the elements When 301 is returned, the permanent redirection is realized, and the parameters after the domain name are also redirected normally.

 

Guess you like

Origin blog.csdn.net/Sp_Tizzy/article/details/131419042