Nginx URL rewriting and nginxurl Rewriting

Source: Internet
Author: User

Nginx URL rewriting and nginxurl Rewriting

Directory:

1.1 URL rewriting
1.2 if command
1.3 rewrite command
1.4 differences between URL rewriting and reverse proxy

1.1 Introduction

Url rewriting is provided by the ngx_http_rewrite_module. It is installed by default, but the function implementation of this module requires pcre. The URL rewriting technology requires not only understanding the syntax of several commands, but also familiar with simple regular expressions, but also the meaning of each variable of nginx as much as possible. The more familiar variables, the better. Most of the variables needed are provided by the http_core module. For their meanings, see the official manual http_core built-in variables.

The rewrite module provides five commands: break, return, set, rewrite, And if.

  • Break is used to complete the current function set and does not execute the rewrite command.
  • Return status code. The available status codes are 204/301/302/303/307/308/400/402-406/408/410-411/413/416/500-504. Return Syntax:
    return code [text];return code URL;return URL;
  • Set is used to define variables. The value assigned to a variable can be a combination of variables, text, and text variables (Syntax: set variable value ;)
  • If is used to set judgment conditions. Format: if (condition ){}
  • Rewrite is used to set URL rewrite Rules (Syntax: rewrite regex replacement [flag];)

1.2 if command

If does not support nesting, and does not support "&" and "|" multi-object operators. Syntax:

if (condition) {}

The test conditions can be defined as follows:

(1). You can use "=" and "! = "Operator.
(2). You can use "~ "And "~ * ", The former indicates case-sensitive regular matching, and the latter indicates case-insensitive matching.
(3). You can add an exclamation point before regular expression matching "!~ "And "!~ * "Indicates the inverse, that is, mismatch.
(4). "-f" and "! -F "determines whether a file exists.
(5). "-d" and "! -D "to determine whether a directory exists.
(6). "-e" and "! -E "determines whether a file, directory, or soft link exists.
(7). "-x" and "! -X "determines whether the file is executable.

If supports regular expressions from $1 to $9 for reverse reference.

The following are examples:

# If ($ http_user_agent ~ MSIE) {rewrite ^ (. *) $/msie/$1 break;} # When the http request Method is POST, the 405 status code is directly returned, that is, Method not Allowedif ($ request_method = POST) {return 405 ;}# if the requested resource file does not exist, exit the current match and proxy to the local machine. In this case, the local machine provides services, such as if (! -F $ request_filename) {break; proxy_pass http: // 127.0.0.1;} # When any host under longshuai.com is accessed, It is redirected to the corresponding directory under www.longshuai.com if ($ http_host ~ * "^ (. *) \. Longshuai \. com $") {set $ domain $1; rewrite ^ (. *) http://www.longshuai.com/?domain/ break ;}

The last one aboveThe URL after URL rewriting is a new host name site, but the efficiency of using URL rewriting is relatively low, it is far inferior to directly defining a virtual host for this site. So rewrite it:

server {    listen 80;    server_name .longshuai.com;    return 302 http://www.longshuai.com/$request_uri;}server {    listen 80;    server_name www.longshuai.com;}

1.3 rewrite command

Rewrite can be written in the server segment, location segment, and if segment. Syntax:

rewrite regexp replacement [flag]

If the replacement part starts with "http: //" or "https: //" or "$ schema", a temporary redirect is directly performed. See the redirect mark in the following table.

Flag is a flag. There are four types of tags. Their functions are shown in the following table.

Flag Description
Last Stop Other rewrite module commands in the current context and re-match the context for the rewritten uri.
Break Like the break command, it stops processing other rewrite module commands in the current context.
Redirect The temporary redirect status code 302 is returned. When replacement is not started with "http: //", "https: //", or "$ schema", the "$ schema" variable indicates what protocol is used.
Permanent Returns the permanent redirect status code 301.

In the above flag,Last and break are used to rewrite the URL. The address in the browser will not change, but the resources and paths accessed on the server have actually changed. Redirect and permanent are used to implement URL jump. The address in the browser is changed to the address after the jump..

Use the break flag when using the proxy_pass command. After this rewrite rule is executed, the last flag continues to initiate a match request for the rewritten address in the current context, and the break stops matching again after this match is completed. For example, the following two rewrite rules.

rewrite "^/bbs/(.*)/images/(.*)\.jpg$" www.longshuai.com/bbs/$2/images/$1.jpg last;rewrite "^/bbs/(.*)/images/(.*)\.jpg$" www.longshuai.com/bbs/$2/images/$1.jpg break;

If you are accessing the ingress^/bbs/(.*)/images/(.*)\.jpg$If the last tag is used, the URL will be overwritten again, resulting in a URL rewriting loop. nginx supports 10 loops by default, and then returns the 500 status code. If the break flag is used, the rewriting will not be matched again after the rewriting is completed.

For example, the following rewrite example redirects any access ending with longshuai.com to www.longshuai.com.

server_name  www.longshuai.com;rewrite (.*).longshuai.com www.longshuai.com permanent;

The example below will redirect all www.longshuai.com/bbs/shards to www.longshuai.com/forum /*.

server {    listen 80;    server_name www.longshuai.com;    location /{        root /www/longshuai/;        index index.html;        rewrite "/bbs/(.*)" "/forum/$1" last;    }}

1.4 differences between URL rewriting and reverse proxy

Both URL rewriting and reverse proxy can forward requests to other hosts. But they are quite different.

1. URL rewriting can implement forwarding that cannot be implemented by reverse proxy.
2. URL rewriting can change the browser address.
3. More reverse proxies are used with upstream to achieve load balancing. URL rewriting cannot directly achieve Load Balancing through forwarding.
4. There are many other differences. You don't need to worry about their differences. You can either rewrite the URL or reverse proxy to achieve a certain requirement. You can simply use one method.

Go back to the Linux series article outline: workshop!

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.