Nginx rewrite pseudo-static configuration parameters and Examples

Source: Internet
Author: User
Tags domian

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.
* break terminates matching, do not match the following rules
* redirect returns 302 the address bar of the temporary redirection will display the address after the jump
* If the permanent returns 301, the address bar of the permanent redirection will display the address after the jump
some available global variables are, it can be used for condition determination (to be supplemented) copy Code the code is as follows: $ 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

Example of combining qeephpCopy codeThe Code is as follows: if (! -D $ request_filename ){
Rewrite ^/([A-z-A-Z] +)/([A-z-A-Z] + )/? (. *) $/Index. php? Namespace = user & controller = $1 & Action = $2 & $3 last;
Rewrite ^/([A-z-A-Z] + )/? $/Index. php? Namespace = user & controller = $1 last;
Break;

Convert multiple directories to parametersCopy codeThe Code is as follows: abc.domian.com/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 swapCopy codeThe Code is as follows:/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:Copy codeThe Code is as follows: if ($ http_user_agent ~ MSIE ){
Rewrite ^ (. *) $/nginx-ie/$1 break;
}

Automatically add "/" to the directoryCopy codeThe Code is as follows: if (-d $ request_filename ){
Rewrite ^/(. *) ([^/]) $ http: // $ host/$1 $2/permanent;
}

Disable htaccessCopy codeThe Code is as follows: Location ~ /\. Ht {
Deny all;
}

Prohibit multiple directoriesCopy codeThe Code is as follows: Location ~ ^/(Cron | templates )/{
Deny all;
Break;
}

Prohibit files starting with/Data
You can disable/data/multi-level directory. log.txt and other requests;Copy codeThe Code is as follows: Location ~ ^/Data {
Deny all;
}

Disable a single directory
Unable to stop. log.txt requestsCopy codeThe Code is as follows: Location/searchword/cron /{
Deny all;
}

Prohibit a single fileCopy codeThe Code is as follows: Location ~ /Data/SQL/data. SQL {
Deny all;
}

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.Copy codeThe Code is as follows: Location ~ (Favicon. ico ){
Log_not_found off;
Expires 99d;
Break;
}

Location ~ (Robots.txt ){
Log_not_found off;
Expires 7d;
Break;
}

Set the expiration time of a file. The value is 600 seconds and no access logs are recorded.Copy codeThe Code is as follows: Location ^ ~ /Html/scripts/loadhead_1.js {
Access_log off;
Root/opt/lampp/htdocs/web;
Expires 600;
Break;
}

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.c1gstudio.com/leech.gif?#display an anti-leech Image
"Access_log off;" does not record access logs, reducing pressure
"Expires 3D" browser cache for all files for 3 daysCopy codeThe Code is as follows: 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 use a fixed IP address to access the website and add a password.Copy codeThe Code is as follows: 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 files under multi-level directories into one file to improve Seo PerformanceCopy codeThe Code is as follows:/job-123-456-789.html points 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 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/Copy codeThe Code is as follows: rewrite ^/([0-9a-z] +) job/(. *) $/area/$1/$2 last;

The problem in the above example is that the access/Shanghai will not matchCopy codeThe Code is as follows: rewrite ^/([0-9a-z] +) 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,
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 effectCopy codeThe Code is as follows: if (-d $ request_filename ){
Rewrite ^/(. *) ([^/]) $ http: // $ host/$1 $2/permanent;
}

After knowing the reason, let me jump to it manually.Copy codeThe Code is as follows: 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:Copy codeThe Code is as follows: if (! -E $ request_filename ){
Proxy_pass http: // 127.0.0.1;
}

Domain jumpCopy codeThe Code is as follows: Server
{
Listen 80;
SERVER_NAME jump.c1gstudio.com;
Index index.html index.htm index. php;
Root/opt/lampp/htdocs/WWW;
Rewrite ^/http://www.c1gstudio.com /;
Access_log off;
}

Multi-domain redirectionCopy codeThe Code is as follows: SERVER_NAME www.c1gstudio.com www.c1gstudio.net;
Index index.html index.htm index. php;
Root/opt/lampp/htdocs;
If ($ host ~ "C1gstudio \. Net "){
Rewrite ^ (. *) http://www.c1gstudio.com $1 permanent;
}

Third-level domain jumpCopy codeThe Code is as follows: if ($ http_host ~ * "^ (. *) \. I \. c1gstudio \. com $ "){
Rewrite ^ (. *) http://top.yingjiesheng.com $1;
Break;
}

Domain Name mirrorCopy codeThe Code is as follows: Server
{
Listen 80;
SERVER_NAME pai.c1gstudio.com;
Index index.html index.htm index. php;
Root/opt/lampp/htdocs/WWW;
Rewrite ^/(. *) http://www.c1gstudio.com/#1 last;
Access_log off;
}

A subdirectory for mirroringCopy codeThe Code is as follows: Location ^ ~ /Zhaopinhui {
Rewrite ^. + http://zph.c1gstudio.com/last;
Break;
}
Discuz ucenter home (uchome) rewrite

Rewrite ^/(space | Network)-(. +) \. html $/$ 1.php? Rewrite = $2 last;
Rewrite ^/(space | Network) \. html $/$ 1.php last;
Rewrite ^/([0-9] +) $/space. php? Uid = $1 last;
Discuz 7 rewrite

Rewrite ^ (. *)/archiver/(FID | tid)-[\ W \-] + \. html) $1/archiver/index. php? $2 last;
Rewrite ^ (. *)/Forum-([0-9] +)-([0-9] +) \. html $1/Forumdisplay. php? FID = $2 & page = $3 last;
Rewrite ^ (. *)/thread-([0-9] +)-([0-9] +)-([0-9] + )\. HTML $1/viewthread. PHP? Tid = $2 & extra = Page \ % 3d $4 & page = $3 last;
Rewrite ^ (. *)/profile-(username | UID)-(. +) \. html $1/viewpro. php? $2 = $3 last;
Rewrite ^ (. *)/space-(username | UID)-(. +) \. html $1/space. php? $2 = $3 last;
Rewrite ^ (. *)/Tag-(. +) \. html $1/Tag. php? Name = $2 last;

Configure a domain name for a forum in discuzCopy codeThe Code is as follows: SERVER_NAME bbs.c1gstudio.com news.c1gstudio.com;

Location = /{
If ($ http_host ~ News \ .c1gstudio.com $ ){
Rewrite ^. + http://news.c1gstudio.com/forum-831-1.html last;
Break;
}
}

Optimization of discuz ucenter profile rewriteCopy codeThe Code is as follows: Location ^ ~ /Ucenter {
Location ~ . * \. Php? $
{
# Fastcgi_pass Unix:/tmp/php-cgi.sock;
Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;
Fcinclude gi. conf;
}

Location/ucenter/data/Avatar {
Log_not_found off;
Access_log off;
Location ~ /(. *) _ Big \. jpg $ {
Error_page 404/ucenter/images/noavatar_big.gif;
}
Location ~ /(. *) _ Middle \. jpg $ {
Error_page 404/ucenter/images/noavatar_middle.gif;
}
Location ~ /(. *) _ Small \. jpg $ {
Error_page 404/ucenter/images/noavatar_small.gif;
}
Expires 300;
Break;
}
}

Jspace rewriteCopy codeThe Code is as follows: Location ~ . * \. Php? $
{
# Fastcgi_pass Unix:/tmp/php-cgi.sock;
Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;
Fcinclude gi. conf;
}

Location ~ * ^/Index. php/
{
Rewrite ^/index. php/(. *)/index. php? $1 break;
Fastcgi_pass 127.0.0.1: 9000;
Fastcgi_index index. php;
Fcinclude gi. conf;
}

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.