After checking and testing on the Internet, we found that the Rewrite rules of Nginx are not very different from the Rewite rules of Apache and can be used almost directly. For example, write rules in Apache.
The code is as follows: |
Copy code |
Rewrite ^/(%0-9%5%%%%.html $/viewthread. php? Tid = $1 last; |
However, writing in Nginx in this way cannot be started. The solution is to add two double quotation marks:
The code is as follows: |
Copy code |
Rewrite "^/(%0-9%5%%%%.html $"/viewthread. php? Tid = $1 last; |
At the same time, the RewriteRule is Rewrite, and www.111cn.net basically implements the conversion from Nginx Rewrite rules to Apache Rewite rules.
Flags of Rewrite
Last-this Flag is basically used.
Break-abort Rewirte and do not continue matching
Redirect-return the HTTP status 302 of the temporary redirect
Permanent-returns the HTTP status 301 for permanent redirection
Example
Convert multiple directories to parameters
The code is as follows: |
Copy code |
Www.111cn.net/sort/2 => abc.domian.com/index.php? Act = sort & name = abc & id = 2 If ($ host ~ * (. *) .Domain.com ){ Set $ sub_name $1; Rewrite ^/sort/(d + )/? $/Index. php? Act = sort & cid = $ sub_name & id = $1 last; }
|
Directory swap
The code is as follows: |
Copy code |
/123456/xxxx->/xxxx? Id = 123456 Rewrite ^/(d +)/(. +) // $2? Id = $1 last;
|
For example, set nginx to redirect to the/nginx-ie directory when you use ie:
The code is as follows: |
Copy code |
If ($ http_user_agent ~ MSIE ){ Rewrite ^ (. *) $/nginx-ie/$1 break; }
|
Automatically add "/" to the Directory
The code is as follows: |
Copy code |
If (-d $ request_filename ){ Rewrite ^/(. *) ([^/]) $ http: // $ host/$1 $2/permanent; }
|
Disable htaccess
The code is as follows: |
Copy code |
Location ~ /. Ht { Deny all; }
|
Prohibit multiple directories
The code is as follows: |
Copy code |
Location ~ ^/(Cron | templates )/{ Deny all; Break; }
|
Prohibit files starting with/data
You can disable/data/multi-level Directory. Log.txt and other requests;
The code is as follows: |
Copy code |
Location ~ ^/Data { Deny all; }
|
Disable a single directory
Unable to stop. Log.txt requests
The code is as follows: |
Copy code |
Location/searchword/cron /{ Deny all; }
|
Prohibit a single file
The code is as follows: |
Copy code |
Location ~ /Data/SQL/data. SQL { Deny all; } |
Discuz! Rewrite in Nginx
It should be noted that all the rewrites that have been circulating on the Internet are incorrect.
In the following Rewrite, the semicolon contains an additional transfer character "", which is required in Apache, but not in Nginx.
Rewrite ^/thread-(0-9000000000000-(0-9000000000000-(0-90000000000000000.html $/viewthread. php? Tid = $1 & extra = page % 3D $3 & page = $2 last;
Which of the following statements is true?
The code is as follows: |
Copy code |
Rewrite ^/thread-(0-9000000000000-(0-9000000000000-(0-90000000000000000.html $/viewthread. php? Tid = $1 & extra = page % 3D $3 & page = $2 last; |
This error basically exists on all websites that use Nginx as the server and have enabled Rewrite. Including Discuz! The official website has provided feedback to cnteacher.
Complete and correct Discuz! Rewrite in Nginx is as follows:
The code is as follows: |
Copy code |
Rewrite ^/archiver/(fid | tid0000-0000w-00000000.html) $/archiver/index. php? $1 last; Rewrite ^/forum-(%0-9%%%%-(%0-9%%%%%%.html $/forumdisplay. php? Fid = $1 & page = $2 last; Rewrite ^/thread-(0-9000000000000-(0-9000000000000-(0-90000000000000000.html $/viewthread. php? Tid = $1 & extra = page % 3D $3 & page = $2 last; Rewrite ^/space-(username | uid316-(.20.0000.html $/space. php? $1 = $2 last; Rewrite ^/tag-(.20.20..html $/tag. php? Name = $1 last; Break; |
To sum up
Regular Expression Matching, where:
*~ Case-sensitive matching
*~ * Case-insensitive match
*!~ And !~ * Case-insensitive and case-insensitive
File and directory match, where:
*-F and! -F is used to determine whether a file exists.
*-D and! -D is used to determine whether a directory exists.
*-E and! -E is used to determine whether a file or directory exists.
*-X and! -X is used to determine whether a file is executable.
Flag labels include:
* Last is equivalent to the [L] Mark in Apache, indicating that rewrite is completed.
* The break terminates the match and does not match the subsequent rules.
* If redirect returns the 302 temporary redirection address bar, the redirected address is displayed.
* If permanent returns 301, the address bar of the permanent redirection will display the address after the jump.
Some available global variables can be used for condition determination (to be supplemented)
$ 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