Nginx config URL rewrite

Source: Internet
Author: User

Request Forwarding (forward) is the URL that the server accesses the destination address directly, and then reads the contents of the response and sends the content to the browser.

Because this jump process is implemented inside the server, the browser does not know where the server sends the content from, so its address bar is still the original address.

Redirection (redirect, or rewrite, rewrite) is the server that, based on logic, returns a status code to the browser, telling it to re-request the address, so the address bar shows the new URL.

Temporary redirection (302 status code), the search engine thinks the new URL is only temporary, so it will crawl new content while retaining the old URL.

Permanent Redirection (301 status code), the search engine will crawl the new content, but also replace the old URL to redirect the URL.

The differences between forwarding and redirection are as follows:

    • Browser Address bar

      The address in the Address bar does not change when the request is forwarded, and the address in the Address bar becomes the new URL when the redirect is requested.

    • Data sharing

      When a request is forwarded, the forwarded page and the forwarded page can share the data in the request, and the data cannot be shared when the redirect is requested.

    • Application scope

      Request forwarding is typically used when a user logs on, and is forwarded to the appropriate place according to the role.

      Request redirection is typically used when a user logs off to return to the main page and jumps to another site.

    • Efficiency

      The efficiency of forwarding is higher than the efficiency of redirection.

Rewrite directives

The Nginx ngx_http_rewrite_module module is used to rewrite the request URI through the PCRE regular expression. The break, if, return, rewrite, and set directives (rewrite directives) in this module are processed in the following order:

When these rewrite directives are in the server context, they are executed sequentially before the requested location is determined.

Find the location based on the request URI

If these rewrite instructions are also included in the location, they are executed sequentially.

If the rewrite instruction in the location generates a new URI, the new location is determined based on the new URI. Such loops can be executed up to 10 times, and more than 500 errors will be returned after the Nginx.

The break instruction (server, location, and if context) is used to complete the current set of rules and to stop performing other rewrite instructions.

if ($slow) {    limit_rate 10k;    break;}

If the $slow is true, set Limit_rate, and then execute the break instruction.

The IF directive (server and location context) is used to determine a condition and, if the condition is true, executes the statement it contains.

if ($http_user_agent ~ MSIE) {    rewrite ^(.*)$ /msie/$1 break;}

If user-agent matches MSIE, the rewrite instruction rewrite URL is executed.

The conditions that can be judged by the IF directive include:

Variable name, or False if the value of the variable is an empty string or 0.

Use the = and! = operators to compare strings.

Use ~* and ~ to match regular expressions.

* indicates case insensitivity.

Use-F and!-f to check if a file exists.

Use-D and!-d to check if the directory exists.

Use-e and!-e to check whether a file, directory, or soft link exists.

Use the-X and!-x to check if it is an executable file.

The return instruction (server, location, and if context) finishes executing the configuration statement and returns the status code for the client.

Returning a non-standard 444 status code closes the connection without sending any response headers.

The rewrite instruction (server, location, and if context) modifies the URI according to the relevant regular expression and string, which executes in the order in which it appears in the configuration file.

server {    ...    rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;    return  403;    ...}

If the request URL begins with/download/and contains/media/, it is rewritten as a URL that contains/mp3/and ends with MP3.

If these instructions are placed in the location/download/{...} context, the last flag should be replaced with the break flag, otherwise Nginx will perform 10 loops and return a 500 error.

If the replacement string contains a new request parameter, the previous request parameter is appended. If this is not expected, then place at the end of the replacement string? Characters to avoid attaching.

Jo Zheng The expression contains} or; character, the entire expression should be included in the ' or '.

If the replacement string starts with http://, https://, or $scheme, the request is redirected and no more rewrite instructions are executed.

Rewrite the tail of the instruction can be labeled (flag), the tag can be the following values:

Last, after completing the rewrite instruction, search for the location that matches the new URI.

Break to stop execution of other rewrite instructions after completing the rewrite instruction.

Redirect, returns 302 temporary redirect. If the replacement string is not used at the beginning of http://https://or $scheme.

Permanent, returns 301 permanent redirection.

If a redirect is using a relative path (no hostname part), Nginx will use the first name specified by the host header or server_name instruction that matches the server_name instruction as its hostname during the redirection process.

If the host header does not match or does not exist and server_name is not set, the local hostname will be used. If you always want Nginx to use the Host header, you can use the * wildcard character in server_name.

The rewrite log of the notice tag is logged in error log when the Rewrite_log instruction (HTTP, server, location, and if context) is enabled.

The value is on or off.

The set directive (server, location, and if context) is used to set a variable and assign it a value that can be text, a variable, or a combination of them.

You cannot set the value of a $http _xxx header variable using the SET directive.

The Uninitialized_variable_warn directive (HTTP, server, location, and if context) is used to turn on or off logging of warning logs in uninitialized variables.

The value is on or off.

Configure URL Rewriting

The configuration of URL rewriting in Nginx is as follows:

location ^~ / {    root   html;    index  index.html index.htm;    rewrite ^/bbs/(.*)$ http://192.168.0.202/forum/$1;}

Redirects the request beginning with/bbs/to http://192.168.0.202/forum/$1.

Internal implementation

The rewrite instructions for the Ngx_http_rewrite_module module are compiled into internal instructions that are interpreted during request processing during the configuration phase, and the interpreter is a simple virtual stack. For example, the following directives:

location /download/ {    if ($forbidden) {        return 403;    }    if ($slow) {        limit_rate 10k;    }    rewrite ^/(download/.*)/media/(.*)\..*$ /$1/mp3/$2.mp3 break;}

will be interpreted as the following instruction:

variable $forbiddencheck against zero    return 403    end of codevariable $slowcheck against zeromatch of regular expressioncopy "/"copy $1copy "/mp3/"copy $2copy ".mp3"end of regular expressionend of code

Since the Limit_rate directive is not related to the Ngx_http_rewrite_module module, there is no directive for it.

This creates a separate configuration for the If block and executes the instructions it contains if the condition is true.

If the first "/" character of the rewrite instruction is placed in "()", that is:

rewrite ^(/download/.*)/media/(.*)\..*$ /$1/mp3/$2.mp3 break;

The instruction that it is interpreted as follows, which reduces a copy statement.
match of regular expression copy $1 copy "/mp3/" copy $2 copy ".mp3" end of regular expression end of code

Nginx config URL rewrite

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.