Nginx forwarding rules

Source: Internet
Author: User
Tags domian

Regular Expression matching of nginx forwarding rules, where :*~ Case-sensitive matching *~ * Case-insensitive matching *!~ And !~ * These are case-insensitive and case-insensitive Files And Directories. *-f and! -F is used to determine whether a file exists *-d and! -D is used to determine whether a directory *-e and! -E is used to determine whether a file or directory exists *-x and! -X is used to determine whether a file can run the flag: * last is equivalent to the [L] Mark in Apache, indicating that rewrite * break is completed to terminate the match, no longer match the following rule * redirect returns 302 the address bar of the temporary redirection will display the address after the jump * permanent returns 301 permanent redirection address bar will display the address after the jump some available global variables are, conditional judgment can be used (to be supplemented) $ args $ content_length $ content_type $ document_root $ document_uri $ host $ response $ http_cookie $ limit_rate $ response $ request_method $ response $ remote_port $ remote_user $ response $ request_uri $ query_string $ scheme $ server _ Protocol $ server_addr $ server_name $ server_port $ uri combined with the QeePHP example if (! -D $ request_filename) {rewrite ^/([a-z-A-Z] +)/([a-z-A-Z] + )/? (. *) $/Index. php? Namespace = user & amp; controller = $1 & amp; action = $2 & amp; $3 last; rewrite ^/([a-z-A-Z] + )/? $/Index. php? Namespace = user & amp; controller = $1 last; break; Convert multiple directories to the parameter abc.domian.com/sort/2 => abc.domian.com/index.php? Act = sort & name = abc & id = 2if ($ host ~ * (. *)/. Domain/. com) {set $ sub_name $1; rewrite ^/sort // (/d + )//? $/Index. php? Act = sort & cid = $ sub_name & id = $1 last;} directory swap/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: if ($ http_user_agent ~ MSIE) {rewrite ^ (. *) $/nginx-ie/$1 break;} automatically adds "/" if (-d $ request_filename) {rewrite ^ /(. *) ([^/]) $ http: // $ host/$1 $2/permanent;} disable htaccesslocation ~ //. Ht {deny all;} multiple directories are prohibited from location ~ ^/(Cron | templates)/{deny all; break;} prohibit requests for files starting with/data from/data/directory levels such as .log.txt. location ~ ^/Data {deny all; the specified directory cannot be banned. log.txt can request location/searchword/cron/{deny all;} to disable the location of a single file ~ /Data/SQL/data. SQL {deny all; specify the expiration time for favicon.icoand robots.txt. in this case, if favicon.icois set to 99 days, And robots.txt is set to 7 days, the location of the 404 error log is not recorded ~ (Favicon. ico) {log_not_found off; expires 99d; break;} location ~ (Robots.txt) {log_not_found off; expires 7d; break;} sets the expiration time of a file, which is 600 seconds, and does not record the access log location ^ ~ /Html/scripts/loadhead_1.js {access_log off; root/opt/lampp/htdocs/web; expires 600; break ;} file anti-leeching and set the expiration time. Here, return 412 is the custom http status code. The default value is 403. It is convenient to find the correct request "rewrite ^/http://leech.c1gstudio.com/leech.gif?##display an anti-leech Image; "No access logs are recorded, reducing the pressure" expires 3d ". All files are cached in the browser for three days. location ~ * ^. + /. (Jpg | jpeg | gif | png | swf | rar | zip | css | js) $ {valid_referers none blocked * .c1gstudio.com * .c1gstudio.net localhost 208.97.167.194; if ($ invalid_referer) {rewrite ^/http://leech.c1gstudio.com/leech.gif; return 412; break;} access_log off; root/opt/lampp/htdocs/web; expires 3d; break;} only allows a fixed ip to access the website, add the password root/opt/htdocs/www; allow 208.97.167.194; allow 222.33.1.2; allow 231.152.49.4; deny all; auth_basic "C1G_ADMIN"; auth_basic_user_file htpasswd; convert a file under a multi-level Directory into a file, enhanced seo effect/job-123-456-789.html pointing to/job/123/456/789.html rewrite ^/job-([0-9] +)-([0-9] +) -([0-9] + )/. html $/job/$1/$2/jobshow_#3.html last; point a folder under the root directory to a 2-level directory such as/shanghaijob/to/area/shanghai/If you change last to permanent, the address bar of the browser is/location/shanghai/rewrite ^/([0-9a-z] +) job /(. *) $/area/$1/$2 last; the problem in the above example is that the rewrite ^/([0-9a-z] +) will not match when accessing/shanghai) job $/area/$1/ Last; 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, such. the actual address of/list_1.html is/area/shanghia/list_1.html, which is converted to/list_1.html and cannot be accessed. Then I cannot add automatic redirect (-d $ request_filename). It has a condition that it must be a real directory, while my rewrite is not, so there is no effect if (-d $ request_filename) {rewrite ^ /(. *) ([^/]) $ http: // $ host/$1 $2/permanent;} it's easy to understand the cause, let me jump to rewrite ^/([0-9a-z] +) job $/$ 1job/permanent; rewrite ^/([0-9a-z] +) job /(. *) $/area/$1/$2 last; redirection when the file and directory do not exist: if (! -E $ request_filename) {proxy_pass http: // 127.0.0.1 ;}

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.