Linux Nginx rewrite rules working notes

Source: Internet
Author: User
Tags curl

Rewrite is a kind of server rewrite pulse technology, it can enable the server to support URL rewriting, is a popular server technology. It also implements the ability to restrict specific IP access to a Web site. In many cases, access to an IP can easily result in CPU 100% (for example, some search engine's fixed crawl, others a large number of collection site, this time we have to use some effective means to seal off the other side of the IP, so that he can not consume the resources of the server, IP method There are many kinds of, if your Web ( Nginx| Apache|other) server installed Rewrite module, you can also try to use the Rewrite rule to seal off each other's IP. – Excerpted from Baidu Encyclopedia

Recent work encountered such a thing, there is a webserveice from Java to PHP, the program surface causes the name of the interface has changed, here it is necessary to use pseudo static to allow requests to request interface to normal processing, first look at the rewrite instructions:

1, set:
For setting variables

2, if:
If is used to determine some criteria that cannot be directly matched in a rewrite statement, such as the presence of a test file and the HTTP, header, cookie
# # #当if表达式中的条件为true, executes the statement in the IF Block ###
# # #当表达式只是一个变量时, if the value is empty or any string beginning with 0 will be treated as false###
# # #直接比较内容时, using = and!=###

Regular rule:
~ #区分大小写字母的匹配 #
~* #不区分大小写的匹配 #
!~   #! Equal to find! -name "True" to indicate mismatch #
!~* #同上 #

-F judgment File exists
-D Judgment Directory exists
-E judgment file, directory, or symbolic link exists
-X Decision File executable

Example:
# # #如果User-agent contains msie rewrite to/msie/directory ###
if ($http _user_agent ~ msie) {
Rewrite ^ (. *) $/msie/$1 break;
}

# # #如果文件名不存在, return 444 (no one's going to use that!)
if (!-e $request _filename) {
return 444;
# # #常规做法都会rewrite to the specified page ###
}

About return Instructions

Syntax: Return code
Scope: Server,location,if
Reference:
1XX (Temporary response)
A status code that represents a temporary response and requires the requester to continue to perform the operation.

Code description
100 (continued) 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 remainder.
101 (switching protocol) The requestor has asked the server to switch protocols, and the server has confirmed and is ready to switch.

2XX (Success)
Represents the status code that successfully processed the request.
Code description
200 (successful) the server has successfully processed the request. Typically, this indicates that the server has provided the requested Web page.
201 (created) The request was successful and the server created a new resource.
202 (accepted) the server has accepted the request but has not yet processed it.
203 (non-authoritative information) the server has successfully processed the request, but the returned information may be 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 processed a partial GET request.

3xx (redirect)
Indicates that to complete the request, further action is required. Typically, these status codes are used for redirection.

Code description
300 (multiple choices) for a request, the server can perform a variety of operations. The server can select an action based on the requester (user agent) or provide an action list for the requester to choose from.
301 (permanently moved) The requested page has been permanently moved to the new location. When the server returns this response (a response to a GET or head request), the requestor is automatically transferred to the new location.
302 (Temporary mobile) The server is currently responding to requests from Web pages in different locations, but the requester should continue to use the original location for subsequent requests.
303 (View other locations) The server returns this code when the requester should use a separate GET request to retrieve the response for a different location.
304 (not modified) The requested page has not been modified since the last request. When the server returns this response, the content of the Web page is not returned.
305 (using a proxy) the requester can only use the proxy to access the requested Web page. If the server returns this response, it also indicates that the requester should use the proxy.
307 (temporary redirection) The server is currently responding to requests from Web pages in different locations, but the requester should continue to use the original location for subsequent requests.

4xx (Request error)
These status codes indicate a possible error in the request and hinder the processing of the server.

Code description
400 (Error request) The server does not understand the syntax of the request.
401 (not authorized) request authentication. The server may return this response for Web pages that need to log on.
403 (Prohibited) the server refused the request.
404 (Not found) the server could not find the requested Web page.
405 (method Disabled) Disables the method specified in the request.
406 (not accepted) cannot use the requested content attribute to respond to the requested Web page.
407 (proxy authorization required) This status code is similar to 401 (not authorized), but the specified requester should authorize the use of the agent.
408 (Request timed out) timeout occurs when the server waits for a request.
409 (conflicting) the server encountered a conflict while completing the request. The server must contain information about the conflict in the response.
410 (Deleted) If the requested resource has been permanently deleted, the server returns this response.
411 (requires a valid length) the server does not accept requests that do not contain a valid content Length header field.
412 (Prerequisites not met) the server did not meet one of the prerequisites that the requester set in the request.
413 (Request entity too Large) the server was unable to process the request because the request entity was too large to handle the server.
414 (The requested URI is too long) The requested URI (usually the URL) is too long for the server to process.
415 (Unsupported media type) The requested format is not supported by the requested page.
416 (Request range does not meet the requirements) if the page cannot provide the requested scope, the server returns this status code.
417 (expectations not met) the server did not meet the requirements for the "expected" Request header field.
444 (non-standard HTTP status code) ends the connection without sending any header headers.

5XX (server error)
These status codes indicate that the server encountered an internal error while trying to process the request. These errors may be errors on the server itself, not the request.

Code description
500 (server internal error) the server encountered an error and could not complete the request.
501 (not yet implemented) the server does not have the capability to complete the request. For example, this code may be returned when the server does not recognize the request method.
502 (Error Gateway) The server received an invalid response from the upstream server as a gateway or proxy.
503 (Service Unavailable) the server is not currently available (due to overloading or downtime maintenance). Usually, this is only a temporary state.
504 (Gateway Timeout) The server acts as a gateway or proxy, but does not receive requests from upstream servers in a timely manner.
505 (HTTP version is not supported) the server does not support the HTTP protocol version used in the request.

Rewrite logo bit:

# # #Nginx URL rewrite will still be rewrite check, up to retry 10 times, if 10 times are still not terminated will return HTTP CODE 500###

Break: Stops rewrite detection, which means that when a rewrite statement containing a break flag is executed, the statement is the final result of the rewrite
Last: Stop rewrite detection, but it is essentially different from the break, the end of the statement is not necessarily the final result, which will be followed by the Nginx location match mentioned
Redirect: Returns 302 temporary redirection, generally used to redirect to full URL (contains http: part)
Permanent: Returns 301 permanent redirects, typically used to redirect to the full URL (including http: part)

Example:
# # #Rewrite rules###
Rewrite/test.php/last.php last;
rewrite/301.php/permanent.php permanent;

[Root@nginx-one www.111cn.net]# curl-i 1.1.1.10/test.php
http/1.1 OK
server:nginx/1.6.0
Date:sun, Modified SEP 2014 01:05:32 GMT
Content-type:text/html
Connection:keep-alive
Vary:accept-encoding
x-powered-by:php/5.5.13

[Root@nginx-one www.111cn.net]# curl-i 1.1.1.10/301.php
http/1.1 Moved Permanently
server:nginx/1.6.0
Date:sun, Modified SEP 2014 01:05:35 GMT
Content-type:text/html
content-length:184
location:http://1.1.1.10/permanent.php
Connection:keep-alive

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.