Nginx Rewrite achieve Jump

Original article in Public number: ape Zhou Xiansen program. This platform is not updated regularly, like my article, I welcome attention to the micro-channel public number.
file

An article on the Location of Nginx configuration were explained herein, this mainly for Nginx Rewrite jump in to explain. Because there is a lot of front-end development work will choose to use Nginx as a reverse proxy server, but the usual business needs will inevitably encounter rewritten URL, Nginx Rewrite jump What's the use of scenario?

  • Jump to a new domain name when the company needed to replace the domain name to access the old domain name
  • Jump to CDN static file request
  • Depending on the user equipment to jump to different sites (pc terminal, a mobile terminal)

I have to say that the Apache server rule base is very strong, so the jump is also very simple, but using Nginx Rewrite jump higher efficiency, so that is what we need to learn the purpose of Nginx Rewrite module resides. Nginx Rewrite is static rewrite module, jump usage scenarios are the following situations:

  • The user can change the access URL
  • Dynamic URL can be disguised as a static URL to provide services
  • Jump to a new domain name that can access the old domain
  • You can jump variable according to a different URL, directory, client information, etc.

How to Rewrite it possible to jump?

Nginx using ngx_http_rewrite_module rewrite request parsing and processing module. Rewrite for implementing URL rewriting, in fact, somewhat similar to redirect feature, you can override the user's request to another directory, improve site security to some extent. Rewrite support if conditional, but not else to judge. And Rewrite need PCRE support, a redirection can jump up to 10 times, more than 10 times will return a 500 error. Rewrite module contains the set command, the recording condition can be used to create variable identifier or transmitted to other variables in Location. Nginx Rewrite actually use existing global variable or variables set command to set the combination of regular expressions to achieve URL rewriting.

Rewrite usage scenarios

Rewrite realize the jump using the following three scenarios in Nginx in:

  • Jump directly matched with Rewrite
  • If the use of global variables matching jump
  • Use location and then jump match

So rewrite statement is only allowed on the server {}, if {}, location {} in. Next we take a look if the instruction.

if instruction

instructions for determining if the condition is matched, select a different configuration according to the determination result Nginx, disposed in the server or location. If the instruction Nginx support only single determination, it does not support multiple determination. We simply look at an example:

location /test{
> index index.html;

    if ( $scheme = http ){    
        rewrite / https://www.niyueling.cn permanent; 
    }  

    if  ($scheme = https ){    
        echo "if ---> $scheme";  
    }
   }

location last article talked about, we will not go into detail, let's look rewrite grammar.

rewrite grammar

  • rewrite regex replacement [flag]

rewrite the URL requested by a user based on a regular expression regex checks to replace match the regular expression corresponding to the new URL. If a plurality of rewrite rules with a configuration module, will check the top down. replacement was content after the jump. [In Flag] as an identifier for the control loop mechanism, URL is http or https after the beginning if the replacement will direct permanent 301 redirect.

flag parameters introduced

There are four state flag rewrite statements: redirect, permanent, break, last. The first two belong to the client browser to re-initiate a request for a new address, the latter two is to achieve a jump in internal WEB server.

  • redirect # temporary redirect, rewrite after the completion of a temporary redirect directly returned after rewriting generated new URL to the client, the client has to re-initiate the request, using a relative path, http: // or https: // at the beginning, status code: 302
  • permanent # permanent redirect, in a permanent manner to return redirection generated after overwriting new URL directly to the client, the client initiates a new request again, status code: 301
  • After the last # rewrite complete stop on the current location of the other subsequent rewrite operation, and then start a new round of new URL rewrite check is not recommended for use in the location in
  • After completion of rewriting break # to stop the current URL in the current location of the other subsequent rewrite operation, then jump directly to other configurations after the rewrite rule matching block; end of the cycle is recommended to use in the location in
    file

rewrite grammar finished we can take a look at how to achieve a jump in several ways:

(1) in a location permanently redirect the old domain to the new domain name

location / {
root /data/html;
index index.html;
rewrite / http://www.niyueling.cn permanent;
}

Here it is necessary to mention at the difference between temporary and permanent redirect redirect:

Temporary redirect the new domain name resolution records are not cached, but a permanent redirect will cache the parsed record a new domain name.

(2) http https automatically jump

Sometimes the company needs of the project will require the entire site are using https, this time for the user experience, we need to automatically jump to https sites when users visit http site.

LOCATION / {
> the root / opt / Blog;
index index.html;
IF ($ scheme = http) {
the rewrite / https://www.niyueling.cn Permanent;
}
}
The above configuration is actually time if the user requests the http protocol use rewrite jump to the corresponding https site. But if the statement can not be removed, otherwise it will be caught in an endless loop.

(3) If the user requests a URL does not exist Go Home

location / {
root /opt/blog;
index index.html;
if ( !-f $request_filename ){
rewrite (.*) http://www.niyueling.cn;
}
}

(4) to achieve anti-theft chain

Anti-theft chain is actually based front-end to carry referer achieve, referer mark can record information about the user interface from which came the jump. Nginx by referer whether information check request module ungx_http_referrer_module effective to achieve the security chain function

^ ~ LOCATION / {Test
the root / opt / Blog;
index index.html;
valid_referers none server_names blocked . .niyueling.cn www.niyueling
. api.online.test / V1 / .google the hostlist ~ ~ # define valid .baidu .; the referer
IF ($ invalid_referer) {# If it is valid to use other access referer:
return 403; # 403 returns a status code
}
}

If you like my articles, my personal welcome attention to public numbers
file

Guess you like

Origin www.cnblogs.com/niyueling/p/11563357.html