This log is based on the Internet and daily usage experience.
Regular Expression Matching, where:
1 .*~ Case-sensitive matching
2 .*~ * Case-insensitive match
3 .*!~ And !~ * Case-insensitive and case-insensitive
File and directory match, where:
1. *-f and! -F is used to determine whether a file exists.
2. *-d and! -D is used to determine whether a directory exists.
3. *-e and! -E is used to determine whether a file or directory exists.
4. *-x and! -X is used to determine whether a file is executable.
Flag labels include:
1. * last is equivalent to the [L] Mark in Apache, indicating that rewrite is completed.
2. * The break terminates the match and does not match the subsequent rules.
3. * If redirect returns 302, the address bar of the temporary redirection will display the address after the jump.
4. * If permanent returns the 301 permanent redirection address bar, the redirected address is displayed.
Some available global variables can be used for condition determination (to be supplemented)
1. $ args
2. $ content_length
3. $ content_type
4. $ document_root
5. $ document_uri
6. $ host
7. $ http_user_agent
8. $ http_cookie
9. $ limit_rate
10. $ request_body_file
11. $ request_method
12. $ remote_addr
13. $ remote_port
14. $ remote_user
15. $ request_filename
16. $ request_uri
17. $ query_string
18. $ scheme
19. $ server_protocol
20. $ server_addr
21. $ server_name
22. $ server_port
23. $ uri
Example of combining QeePHP
1. if (! -D $ request_filename ){
2. rewrite ^/([a-z-A-Z] +)/([a-z-A-Z] + )/? (. *) $/Index. php? Namespace = user & amp; controller = $1 & amp; action = $2 & amp; $3 last;
3. rewrite ^/([a-z-A-Z] + )/? $/Index. php? Namespace = user & amp; controller = $1 last;
4. break;
Convert multiple directories to parameters
Abc.domian.com/sort/2 => abc.domian.com/index.php? Act = sort & name = abc & id = 2
1. if ($ host ~ * (. *) .Domain.com ){
2. set $ sub_name $1;
3. rewrite ^/sort/(d + )/? $/Index. php? Act = sort & cid = $ sub_name & id = $1 last;
4 .}
Directory swap
/123456/xxxx->/xxxx? Id = 123456
1. rewrite ^/(d +)/(. +)/$2? Id = $1 last;
For example, set nginx to redirect to the/nginx-ie directory when you use ie:
1. if ($ http_user_agent ~ MSIE ){
2. rewrite ^ (. *) $/nginx-ie/$1 break;
3 .}
Automatically add "/" to the Directory
1. if (-d $ request_filename ){
2. rewrite ^/(. *) ([^/]) $ http: // $ host/$1 $2/permanent;
3 .}
Disable htaccess
1. location ~ /. Ht {
2. deny all;
3 .}
Prohibit multiple directories
1. location ~ ^/(Cron | templates )/{
2. deny all;
3. break;
4 .}
Prohibit files starting with/data
You can disable/data/multi-level Directory. Log.txt and other requests;
1. location ~ ^/Data {
2. deny all;
3 .}
Disable a single directory
Unable to stop. Log.txt requests
1. location/searchword/cron /{
2. deny all;
3 .}
Prohibit a single file
1. location ~ /Data/SQL/data. SQL {
2. deny all;
3 .}
Set the expiration time for favicon.icoand robots.txt;
In this example, if favicon.icois set to 404 days, and Robots.txt is set to 7 days, error logs are not recorded.
1. location ~ (Favicon. ico ){
2. log_not_found off;
3. expires 99d;
4. break;
5 .}
6.
7. location ~ (Robots.txt ){
8. log_not_found off;
9. expires 7d;
10. break;
11 .}
Set the expiration time of a file. The value is 600 seconds and no access logs are recorded.
1. location ^ ~ /Html/scripts/loadhead_1.js {
2. access_log off;
3. root/opt/lampp/htdocs/web;
3. expires 600;
5. break;
6 .}
File anti-leeching and set expiration time
Here, return 412 is a custom http status code. The default value is 403, which helps you find the correct request for leeching.
"Rewrite ^/http: // leech. Your domain name/leech.gif;" shows an anti-leech image.
"Access_log off;" does not record access logs, reducing pressure
"Expires 3d" browser cache for all files for 3 days
1. location ~ * ^. +. (Jpg | jpeg | gif | png | swf | rar | zip | css | js) $ {
2. valid_referers none blocked *. Your domain name * .c1gstudio.net localhost 208.97.167.194;
3. if ($ invalid_referer ){
4. rewrite ^/http: // leech. Your domain name/leech.gif;
5. return 412;
6. break;
7 .}
8. access_log off;
9. root/opt/lampp/htdocs/web;
10. expires 3d;
11. break;
12 .}
Only use a fixed ip address to access the website and add a password.
1. root/opt/htdocs/www;
2. allow 208.97.167.194;
3. allow 222.33.1.2;
4. allow 231.152.49.4;
5. deny all;
6. auth_basic "C1G_ADMIN ";
7. auth_basic_user_file htpasswd;
Convert files under multi-level directories into one file to Improve seo performance
/Job-123-456-789.html to/job/123/456/789.html
1. rewrite ^/job-(%0-9%%%%%%-(%0-9%%%%%%%%%%%.html $/job/$1/$2/jobshow_%3.html last;
Point a folder in the root directory to a level 2 Directory
For example,/shanghaijob/points to/area/shanghai/
If you change last to permanent, the address bar of the browser is/location/shanghai/
1. rewrite ^/([0-9a-z] +) job/(. *) $/area/$1/$2 last;
The problem in the above example is that the access/shanghai will not match
1. rewrite ^/([0-9a-z] +) job $/area/$1/last;
2. rewrite ^/([0-9a-z] +) job/(. *) $/area/$1/$2 last;
In this way,/shanghai can also be accessed, but the relative link on the page cannot be used,
For example, if the actual address of./list_1.html is/area/shanghia/list_1.html, it will become/list_1.html and cannot be accessed.
I cannot add automatic jump.
(-D $ request_filename) it has a condition that it must be a real directory, and my rewrite is not, so it has no effect
1. if (-d $ request_filename ){
2. rewrite ^/(. *) ([^/]) $ http: // $ host/$1 $2/permanent;
3 .}
After knowing the reason, let me jump to it manually.
1. rewrite ^/([0-9a-z] +) job $/$ 1job/permanent;
2. rewrite ^/([0-9a-z] +) job/(. *) $/area/$1/$2 last;
Redirection when the file and directory do not exist:
1. if (! -E $ request_filename ){
2. proxy_pass http: // 127.0.0.1;
3 .}
Domain jump
1. server
2 .{
3. listen 80;
4. server_name jump. Your domain name;
5. index index.html index.htm index. php;
6. root/opt/lampp/htdocs/www;
7. rewrite ^/http: // www. Your domain name /;
8. access_log off;
9 .}
Multi-domain redirection
1. server_name www. Your domain name www.c1gstudio.net;
2. index index.html index.htm index. php;
3. root/opt/lampp/htdocs;
4. if ($ host ~ "C1gstudio.net "){
5. rewrite ^ (. *) http: // www. Your domain name $1 permanent;
6 .}
Third-level domain jump
1. if ($ http_host ~ * "^ (. *). I .c1gstudio.com $ "){
2. rewrite ^ (. *) http: // top. Your domain name $1;
3. break;
4 .}
Domain name mirror
1. server
2 .{
3. listen 80;
4. server_name mirror. Your domain name;
5. index index.html index.htm index. php;
6. root/opt/lampp/htdocs/www;
7. rewrite ^/(. *) http: // www. Your domain name/$1 last;
8. access_log off;
9 .}
A subdirectory for mirroring
1. location ^ ~ /Zhaopinhui {
2. rewrite ^. + http: // zph. Your domain name/last;
3. break;
4 .}
Discuz ucenter home (uchome) rewrite
1. rewrite ^/(space | networkapps-(.20.0000.html $/$ 1.php? Rewrite = $2 last;
2. rewrite ^/(space | network).html $/$ 1.php last;
3. rewrite ^/([0-9] +) $/space. php? Uid = $1 last;
Discuz 7 rewrite
1. rewrite ^ (. *)/archiver/(fid | tid0000-0000w-00000000.html) $1/archiver/index. php? $2 last;
2. rewrite ^ (. *)/forum-(%0-9%%*-(%0-9%%%%%%%.html $1/forumdisplay. php? Fid = $2 & page = $3 last;
3. rewrite ^ (. *)/thread-(%0-9%%%%%%-(%0-9%%%%%%%%.html $1/viewthread. php? Tid = $2 & extra = page % 3D $4 & page = $3 last;
4. rewrite ^ (. *)/profile-(username | uid+-(.20.20..html $1/viewpro. php? $2 = $3 last;
5. rewrite ^ (. *)/space-(username | uid0000-(.20.0000.html $1/space. php? $2 = $3 last;
6. rewrite ^ (. *)/tag-(.20.20..html $1/tag. php? Name = $2 last;
Configure a domain name for a forum in discuz
1. server_name bbs. Your domain name news. Your domain name;
2.
3. location = /{
4. if ($ http_host ~ News. Your domain name $ ){
5. rewrite ^. + http: // news. Your domain name/forum-831-1.html last;
6. break;
7 .}
8 .}
Optimization of discuz ucenter profile rewrite
1. location ^ ~ /Ucenter {
2. location ~ . *. Php? $
3 .{
4. # fastcgi_pass unix:/tmp/php-cgi.sock;
5. fastcgi_pass 127.0.0.1: 9000;
6. fastcgi_index. php;
7. include fcgi. conf;
8 .}
9.
10. location/ucenter/data/avatar {
11. log_not_found off;
12. access_log off;
13. location ~ /(. * )_Big.jpg $ {
14. error_page 404/ucenter/images/noavatar_big.gif;
15 .}
16. location ~ /(. * )_Middle.jpg $ {
17. error_page 404/ucenter/images/noavatar_middle.gif;
18 .}
19. location ~ /(. * Cmd_small.jpg $ {
20. error_page 404/ucenter/images/noavatar_small.gif;
21 .}
2. expires 300;
23. break;
24 .}
25 .}
Jspace rewrite
1. location ~ . *. Php? $
2 .{
3. # fastcgi_pass unix:/tmp/php-cgi.sock;
4. fastcgi_pass 127.0.0.1: 9000;
5. fastcgi_index. php;
6. include fcgi. conf;
7 .}
8.
9. location ~ * ^/Index. php/
10 .{
11. rewrite ^/index. php/(. *)/index. php? $1 break;
12. fastcgi_pass 127.0.0.1: 9000;
13. fastcgi_index. php;
14. include fcgi. conf;
15 .}
Wordpress rewrite
1. location /{
2. index index.html index. php;
3. if (-f $ request_filename/index.html ){
4. rewrite (. *) $1/index.html break;
5 .}
6. if (-f $ request_filename/index. php ){
7. rewrite (. *) $1/index. php;
8 .}
9. if (! -E $ request_filename)
10 .{
11. rewrite (. *)/index. php;
12 .}
13 .}
Update
Discuzx 1.5 rewrite
1. rewrite ^ ([^.] *)/topic-(.w.20..html $1/portal. php? Mod = topic & topic = $2 last;
2. rewrite ^ ([^.] *)/article-(%0-9%%%%)-(%0-9%%%%%%%.html $1/portal. php? Mod = view & aid = $2 & page = $3 last;
3. rewrite ^ ([^.] *)/forum-(w1_0000-(0-91_00000000.html $1/forum. php? Mod = forumdisplay & fid = $2 & page = $3 last;
4. rewrite ^ ([^.] *)/thread-(0-9000000000000-(0-9000000000000-(0-90000000000000000.html $1/forum. php? Mod = viewthread & tid = $2 & extra = page % 3D $4 & page = $3 last;
5. rewrite ^ ([^.] *)/group-(0-9000000000000-(0-90000000000000000.html $1/forum. php? Mod = group & fid = $2 & page = $3 last;
6. rewrite ^ ([^.] *)/space-(username | uid+-(.20.20..html $1/home. php? Mod = space & $2 = $3 last;
7. rewrite ^ ([^.] *)/([a-z?##-(.20.20..html $1/$ 2.php? Rewrite = $3 last;
8. if (! -E $ request_filename ){
9. return 404;
10 .}
Dynamic parameter rewrite
Take discuz7.2 to discuzx1.5 as an example
1. if ($ query_string ~ * Tid = ([0-9] + )){
2. set $ id $1;
3. rewrite "^ (. *)/viewthread. php $" $1/forum. php? Mod = viewthread & tid = $ id & extra = page % 3D & page = 1 last;
4 .}
5. if ($ query_string ~ * Gid = ([0-9] + )){
6. set $ id $1;
7. rewrite "^ (. *)/index. php $" $1/forum. php? Gid = $ id last;
8 .}
9. rewrite ^ ([^.] *)/archiver/$1/forum. php? Archiver = 1 last;
Updated on 2011-4-21
Nginx nested if
Nginx does not support if and multi-layer nested if, which makes me have a long headache and needs to be implemented through other methods.
The following is a crawler accessing the image website cnc. Your domain name to the www site.
1. set $ needrewrite '';
2. if ($ http_user_agent ~ * (Baiduspider | googlebot | soso | bing | sogou | yahoo | sohu-search | yodao | YoudaoBot | robozilla | msnbot | MJ12bot | NHN | Twiceler )){
3. set $ needrewrite 'O ';
4 .}
5. if ($ host ~ Cnc.c1gstudio.com ){
6. set $ needrewrite "$ {needrewrite} k ";
7 .}
8. if ($ needrewrite = OK ){
9. # return 403;
10. rewrite ^ (. *) http: // www. Your domain name $1 permanent;
11 .}
After reload nginx, you can use curl for testing.
Curl-I-A "soso" cnc. Your domain name