Nginx rewrite rule record

Source: Internet
Author: User

Rewrite is a server rewrite pulse technology that allows servers to support URL rewriting. It is a new and popular server technology. It can also restrict specific IP addresses to access websites. In many cases, access from an IP address can easily cause a CPU usage of 100% (for example, fixed crawling by some search engines and massive collection sites by others ), at this time, we need to use some effective means to block the IP address of the other party so that it cannot consume server resources. There are many methods to block IP addresses, if the rewrite module is installed on your web (nginx | Apache | Other) server, you can use the rewrite rule to block the IP address of the other server.

 

In my recent work, a webserveice was transformed from Java to PhP, and the interface name changed due to program reasons, here, we need to use pseudo-static to make the request interface's request process properly. First, let's take a look at the rewrite command:

1. Set: Used to set variables 2. If: If is used to determine conditions that cannot be directly matched in the rewrite statement, if the check object exists and HTTP, header, and cookie ### if the condition in the IF expression is true, then execute the statement ###### when the expression is only a variable, if the value is null or any string starting with 0 is treated as false ##### when directly comparing content, use = and! #### Regular rules :~ # Case-sensitive matching #~ * # Case-insensitive matching #!~ #! Equivalent to find! -Name "true", indicating no matching #!~ * # Same as above #-F determines whether a file exists-D determines whether a directory exists-e determines whether a file exists, and whether a directory or symbolic link exists-x determines whether a file can be executed example: ### if the User-Agent contains MSIE, rewrite it to the/MSIE/directory ### if ($ http_user_agent ~ MSIE) {rewrite ^ (. *) $/MSIE/$1 break;} ### if the file name does not exist, 444 is returned (no one will use it like this) if (! -E $ request_filename) {return 444; ### rewrite to the specified page in general practice ###}

 

About return commands

Syntax: return code scope: Server, location, if reference: 1xx (temporary response) indicates the status code of the temporary response and requires the requester to continue the operation. Code Description: 100 (CONTINUE) the requester should continue to make the request. The server returns this code, indicating that the first part of the request has been received and is waiting for the remaining part. 101 (switching protocol) the requester has requested the server switching protocol. The server has confirmed and is ready to switch. 2XX indicates that the request status code is successfully processed. Code Description: The 200 (successful) server has successfully processed the request. Generally, this indicates that the server provides the requested webpage. The 201 (created) request is successful and the server creates a new resource. 202 (accepted) the server has accepted the request but has not yet processed it. 203 (unauthorized information) the server has successfully processed the request, but the returned information may come from another source. 204 (NO content) The server successfully processed the request, but did not return any content. 205 (reset content) The server successfully processed the request, but did not return any content. 206 (partial content) The server successfully processes some GET requests. 3xx (redirection) indicates that further operations are required to complete the request. These status codes are usually used for redirection. Code Description 300 (multiple options) for requests, the server can perform multiple operations. The server selects an operation based on the user agent or provides an operation list for the user agent to select. The webpage of the 301 (permanent mobile) request has been permanently moved to a new location. When the server returns this response (response to a get or head request), it automatically redirects the requester to a new location. The 302 (temporary mobile) server currently responds to requests from different locations, but the requester should continue to use the original location for subsequent requests. 303 (view other locations) when the requester uses a separate GET request for different locations to retrieve the response, the server returns this code. 304 (not modified) the requested webpage has not been modified since the last request. When the server returns this response, no webpage content is returned. 305 (using a proxy) the requester can only use a proxy to access the requested webpage. If the server returns this response, it also indicates that the requester should use a proxy. 307 (temporary redirection) The server currently responds to requests from different locations, but the requester should continue to use the original location for subsequent requests. The status code 4xx indicates that the request may be faulty, which hinders server processing. Code Description 400 (incorrect request) the server does not understand the request syntax. 401 (unauthorized) requests require authentication. The server may return this response for webpages that require logon. 403 (Forbidden) The server rejects the request. 404 (not found) The server cannot find the requested webpage. 405 (method disabled) disable the method specified in the request. 406 (unacceptable) unable to respond to the requested webpage using the requested content features. 407 (proxy authorization required) this status code is similar to 401 (unauthorized), but the specified requester should authorize the use of proxy. 408 (request timeout) timeout occurred when the server waited for the request. 409 conflict occurs when the server completes the request. The server must contain conflict information in the response. 410 (Deleted) If the requested resource has been permanently deleted, the server returns this response. The 411 (valid length required) server does not accept requests that do not contain valid Content Length header fields. 412 (not meeting the prerequisites) the server does not meet one of the prerequisites set by the requester in the request. 413 (the Request Entity is too large) The server cannot process the request because the request entity is too large and exceeds the server's processing capability. 414 (the requested URI is too long) The request URI (usually the URL) is too long and cannot be processed by the server. 415 (unsupported media type) The request format is not supported by the request page. 416 (the request range does not meet the requirements) if the page cannot provide the request range, the server returns this status code. 417 (not meeting expectations) the server does not meet the "expectation" request header field requirements. 444 (non-standard HTTP Status Code) ends the connection without sending any header. The status code 5xx indicates that the server encountered an internal error when trying to process the request. These errors may be server errors rather than request errors. Code Description 500 (internal server error) The server encounters an error and cannot complete the request. 501 (not implemented) the server does not have the function to complete the request. For example, this Code may be returned when the server cannot identify the request method. The 502 (error gateway) server acts as a gateway or proxy and receives an invalid response from the upstream server. 503 (Service unavailable) servers are currently unavailable (due to overload or downtime maintenance ). Generally, this is only a temporary status. 504 (gateway timeout) The server acts as a gateway or proxy, but does not receive a request from the upstream server in time. 505 (HTTP Version Not Supported) the server does not support the HTTP protocol version used in the request.

 

Rewrite flag:

### Rewrite check will still be performed after nginx URL rewrite, up to 10 retries. If 10 retries are not terminated, HTTP code 500 will be returned ### break: Stop rewrite detection, that is to say, when a rewrite statement containing break flag is executed, this statement is the final result of rewrite last: Stop rewrite detection, but it is essentially different from break, the last statement is not necessarily the final result, which will be matched with the nginx location later. Redirect: returns 302 temporary redirection, which is usually used to redirect to the complete URL (including http: partial) permanent: returns 301 permanent redirection, which is generally used to redirect to the complete URL (including http: Part) Example: ### Rewrite Rules ### rewrite/test. PHP/last. PHP last; rewrite/301.php/permanent. PHP permanent; [[email protected]-One www. ipython. me] # curl-I 1.1.1.10/test. phphttp/1.1 200 okserver: nginx/1.6.0date: Sun, 07 Sep 2014 01:05:32 gmtcontent-type: text/htmlconnection: Keep-alivevary: Accept-encodingx-powered-: PHP/5.5.13 [[email protected]-One www. ipython. me] # curl-I 1.1.1.10/301. phphttp/1.1 301 moved permanentlyserver: nginx/1.6.0date: Sun, 07 Sep 2014 01:05:35 gmtcontent-type: text/htmlcontent-length: 184 location: http: // 1.1.1.10/permanent. phpconnection: keep-alive

 

Global variables that rewrite can use (meaning portal)

    $args    $content_length    $content_type    $document_root    $document_uri    $host    $http_user_agent    $http_cookie    $limit_rate    $request_body_file    $request_method    $remote_addr    $remote_port   $remote_user    $request_filename    $request_uri    $query_string    $scheme    $server_protocol    $server_addr    $server_name    $server_port    $uri
 
» Reprinted copyright: It Chen Yi» record nginx Rewrite Rules» This article link address: http://www.ipython.me/centos/nginx-rewrite-rules.html» copyright adopted: BY-NC-SA protocol for authorization, reprinted with the source. All articles on this site are original except it-tools, news, and special notes .» If you like: Click here to subscribe to this site

Nginx rewrite rule record

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.