Nginx rewrite implements automatic completion of pseudo-static and pseudo-static

Source: Internet
Author: User
Tags min php and regular expression browser cache domian
The use of pseudo-static in nginx directly writes rules in nginx. conf, and does not require pseudo-static operations like apache to enable the write module (mod_rewrite.

Nginx only needs to open the nginx. conf configuration file and write the required rules in the server.

Nginx common Rewrite Pseudo-static rules:
Pseudo-static rules are a very heavy parameter of pseudo-static. If we can understand more, we can quickly write the optimal pseudo-static code, the following are some examples to help you.
This log is based on the Internet and daily usage experience.
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
Example of combining QeePHP
 
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 parameters
 
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 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 add "/" to the Directory
 
If (-d $ request_filename ){
Rewrite ^/(. *) ([^/]) $ http: // $ host/$1 $2/permanent;
}
Disable htaccess
 
Location ~ //. Ht {
Deny all;
}
Prohibit multiple directories
 
Location ~ ^/(Cron | templates )/{
Deny all;
Break;
}
Prohibit files starting with/data
You can disable/data/multi-level Directory. Log.txt and other requests;
 
Location ~ ^/Data {
Deny all;
}
Disable a single directory
Unable to stop. Log.txt requests
 
Location/searchword/cron /{
Deny all;
}
Prohibit a single file
 
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.
 
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.
 
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 ^/yun_qi_img/jb51.gif;" displays an anti-Leech image.
"Access_log off;" does not record access logs, reducing pressure
"Expires 3d" browser cache for all files for 3 days
Location ~ * ^. +/. (Jpg | jpeg | gif | png | swf | rar | zip | css | js) $ {
Valid_referers none blocked * .jb51.net * .jbzj.net localhost 1.1.1.1;
If ($ invalid_referer ){
Rewrite ^/yun_qi_img/jb51.gif;
Return 412;
Break;
}
Access_log off;
Root/opt/lampp/htdocs/web;
Expires 3d;
Break;
}
Only a fixed ip address is allowed to access the website and a password is added.
 
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 performance
 
/Job-123-456-789.html 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/
 
Rewrite ^/([0-9a-z] +) job/(. *) $/area/$1/$2 last;
The problem in the above example is that the access/shanghai will not match
 
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 effect
If (-d $ request_filename ){
Rewrite ^/(. *) ([^/]) $ http: // $ host/$1 $2/permanent;
}
After knowing the reason, let me jump to it manually.
 
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;
}
Domain jump
 
Server
{
Listen 80;
Server_name jump.c1gstudio.com;
Index index.html index.htm index. php;
Root/opt/lampp/htdocs/www;
Rewrite ^/http://www.jb51.net /;
Access_log off;
}
Multi-domain redirection
 
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.jb51.net $1 permanent;
}
Third-level domain jump
 
If ($ http_host ~ * "^ (. *)/. I/. c1gstudio/. com $ "){
Rewrite ^ (. *) http://www.jb51.net $1;
Break;
}
Domain name image
 
Server
{
Listen 80;
Server_name pai.c1gstudio.com;
Index index.html index.htm index. php;
Root/opt/lampp/htdocs/www;
Rewrite ^/(. *) http://www.jb51.net/#1 last;
Access_log off;
}
A subdirectory as an image
 
Location ^ ~ /Php {
Rewrite ^. + http://www.jb51.net/last;
Break;
}

Pseudo-static is often required when using nginx + php, which is usually set manually. Is there a way to enable nginx to automatically complete the path?
These two days have been a long time before such a function is realized:
Request/a/B/c
If the file does not exist, search for/a/B/index. php and/c as PATH_INFO;
If the file does not exist, find/a/index. php,/B/c as PATH_INFO;
If the file does not exist, search for/index. php and/a/B/c as PATH_INFO;
If the file does not exist, 404 is returned.
Although this kind of performance loss is not suitable for deployment, it can still bring convenience during local debugging :)
The server should have the following code, and other parts should use their own configuration:
Index. php index.html index.htm;
Location /{
Set $ path $ request_uri;
Set $ path_info "";
Try_files $ uri // @ 404;
}
Location @ 404 {
If ($ path ~ ^ (. *) (/. +) $ ){
Set $ path $1/index. php;
Set $ path_info $2;
Rewrite. * $ path last;
    }
Return 404;
}
Location ~ . +. Php ($ | /){
Fastcgi_split_path_info ^ (. +. php) (/. +) $;
If ($ path_info !~ .*){
Set $ path_info $ fastcgi_path_info;
    }
Try_files $ fastcgi_script_name @ 404php;
Fastcgi_param PATH_INFO $ path_info;
Fastcgi_index index. php;
Include fastcgi. conf;
Fastcgi_pass unix:/usr/local/var/run/php-fpm.sock;
Fastcgi_connect_timeout 60;
Fastcgi_send_timeout 300;
Fastcgi_read_timeout 300;
}
Location @ 404php {
If ($ path =/index. php ){
Return 404;
    }
If ($ path ~ ^ (. *) (/. +)/Index. php $ ){
Set $ path_info $2;
Set $ path $1/index. php;
Rewrite. * $ path last;
    }
Return 404;
}
Nginx common Rewrite pseudo static rules trust most of the Linux VPS partners are using this agile deployment of Nginx, this day to clear the most common PHP French Rewrite (pseudo static rules ).
Wordpress:
Location /{
Index index.html index. php;
If (-f $ request_filename/index.html ){
Rewrite (. *) $1/index.html break;
}
If (-f $ request_filename/index. php ){
Rewrite (. *) $1/index. php;
}
If (! -F $ request_filename ){
Rewrite (. *)/index. php;
}
}
PHPCMS:
Location /{
### PHPCMS pseudo-static rewrite rules
Rewrite ^ (. *) show-([0-9] +)-([0-9] +) \. html $1/show. php? Itemid = $2 & page = $3;
Rewrite ^ (. *) list-([0-9] +)-([0-9] +) \. html $1/list. php? Catid = $2 & page = $3;
Rewrite ^ (. *) show-([0-9] +) \. html $1/show. php? Specialid = $2;
#### PHPWind pseudo-static rewrite rules
Rewrite ^ (. *)-htm-(. *) $ 1.php? $2 last;
Rewrite ^ (. *)/simple/([a-z0-9 \ _] + \. html) $1/simple/index. php? $2 last;
}
ECSHOP:
If (! -E $ request_filename)
{
Rewrite "^/index \. html"/index. php last;
Rewrite "^/category $"/index. php last;
Rewrite "^/feed-c ([0-9] +) \. xml $"/feed. php? Cat = $1 last;
Rewrite "^/feed-B ([0-9] +) \. xml $"/feed. php? Brand = $1 last;
Rewrite "^/feed \. xml $"/feed. php last;
Rewrite "^/category-([0-9] +)-B ([0-9] +)-min ([0-9] +) -max ([0-9] +)-attr ([^-] *)-([0-9] + )-(. +)-([a-zA-Z] + )(. *)\. html $ "/category. php? Id = $1 & brand = $2 & price_min = $3 & price_max = $4 & filter_attr = $5 & page = $6 & sort = $7 & order = $8 last;
Rewrite "^/category-([0-9] +)-B ([0-9] +)-min ([0-9] +) -max ([0-9] +)-attr ([^-] *) (. *)\. html $ "/category. php? Id = $1 & brand = $2 & price_min = $3 & price_max = $4 & filter_attr = $5 last;
Rewrite "^/category-([0-9] +)-B ([0-9] +)-([0-9] + )-(. +)-([a-zA-Z] + )(. *)\. html $ "/category. php? Id = $1 & brand = $2 & page = $3 & sort = $4 & order = $5 last;
Rewrite "^/category-([0-9] +)-B ([0-9] +)-([0-9] + )(. *)\. html $ "/category. php? Id = $1 & brand = $2 & page = $3 last;
Rewrite "^/category-([0-9] +)-B ([0-9] +) (. *) \. html $"/category. php? Id = $1 & brand = $2 last;
Rewrite "^/category-([0-9] +) (. *) \. html $"/category. php? Id = $1 last;
Rewrite "^/goods-([0-9] +) (. *) \. html"/goods. php? Id = $1 last;
Rewrite "^/article_cat-([0-9] +)-([0-9] + )-(. +)-([a-zA-Z] + )(. *)\. html $ "/article_cat.php? Id = $1 & page = $2 & sort = $3 & order = $4 last;
Rewrite "^/article_cat-([0-9] +)-([0-9] +) (. *) \. html $"/article_cat.php? Id = $1 & page = $2 last;
Rewrite "^/article_cat-([0-9] +) (. *) \. html $"/article_cat.php? Id = $1 last;
Rewrite "^/article-([0-9] +) (. *) \. html $"/article. php? Id = $1 last;
Rewrite "^/brand-([0-9] +)-c ([0-9] +)-([0-9] + )-(. +)-([a-zA-Z] + )\. html "/brand. php? Id = $1 & cat = $2 & page = $3 & sort = $4 & order = $5 last;
Rewrite "^/brand-([0-9] +)-c ([0-9] +)-([0-9] + )(. *)\. html "/brand. php? Id = $1 & cat = $2 & page = $3 last;
Rewrite "^/brand-([0-9] +)-c ([0-9] +) (. *) \. html"/brand. php? Id = $1 & cat = $2 last;
Rewrite "^/brand-([0-9] +) (. *) \. html"/brand. php? Id = $1 last;
Rewrite "^/tag-(. *) \. html"/search. php? Keywords = $1 last;
Rewrite "^/snatch-([0-9] +) \. html $"/snatch. php? Id = $1 last;
Rewrite "^/group_buy-([0-9] +) \. html $"/group_buy.php? Act = view & id = $1 last;
Rewrite "^/auction-([0-9] +) \. html $"/auction. php? Act = view & id = $1 last;
Rewrite "^/exchange-id ([0-9] +) (. *) \. html $"/exchange. php? Id = $1 & act = view last;
Rewrite "^/exchange-([0-9] +)-min ([0-9] +)-max ([0-9] +) -([0-9] + )-(. +)-([a-zA-Z] + )(. *)\. html $ "/exchange. php? Cat_id = $1 & integral_min = $2 & integral_max = $3 & page = $4 & sort = $5 & order = $6 last;
Rewrite ^/exchange-([0-9] +)-([0-9] + )-(. +)-([a-zA-Z] + )(. *)\. html $ "/exchange. php? Cat_id = $1 & page = $2 & sort = $3 & order = $4 last;
Rewrite "^/exchange-([0-9] +)-([0-9] +) (. *) \. html $"/exchange. php? Cat_id = $1 & page = $2 last;
Rewrite "^/exchange-([0-9] +) (. *) \. html $"/exchange. php? Cat_id = $1 last;
}
SHOPEX:
Location /{
If (! -E $ request_filename ){
Rewrite ^/(. + \. (html | xml | json | htm | php | jsp | asp | shtml) $/index. php? $1 last;
}
}
SaBlog 2.0:
# Archiving with only months
Rewrite "^/date/([0-9] {6 })/? ([0-9] + )? /? $ "/Index. php? Action = article & setdate = $1 & page = $2 last;
# No category paging
Rewrite ^/page/([0-9] + )? /? $/Index. php? Action = article & page = $1 last;
# Category
Rewrite ^/category/([0-9] + )/? ([0-9] + )? /? $/Index. php? Action = article & cid = $1 & page = $2 last;
Rewrite ^/category/([^/] + )/? ([0-9] + )? /? $/Index. php? Action = article & curl = $1 & page = $2 last;
# Archive and advanced search
Rewrite ^/(archives | search | article | links )/? $/Index. php? Action = $1 last;
# Full-data criticism, tag list, and reference list with pagination
Rewrite ^/(comments | tagslist | trackbacks | article )/? ([0-9] + )? /? $/Index. php? Action = $1 & page = $2 last;
# Tags
Rewrite ^/tag/([^/] + )/? ([0-9] + )? /? $/Index. php? Action = article & item = $1 & page = $2 last;
# Article
Rewrite ^/archives/([0-9] + )/? ([0-9] + )? /? $/Index. php? Action = show & id = $1 & page = $2 last;
# RSS rewrite ^/rss/([0-9] + )? /? $/Rss. php? Cid = $1 last;
Rewrite ^/rss/([^/] + )/? $/Rss. php? Url = $1 last;
# User rewrite ^/uid/([0-9] + )/? ([0-9] + )? /? $/Index. php? Action = article & uid = $1 & page = $2 last;
Rewrite ^/user/([^/] + )/? ([0-9] + )? /? $/Index. php? Action = article & user = $1 & page = $2 last;
# MAP files
Rewrite sitemap. xml sitemap. php last;
# Custom links
Rewrite ^ (. *)/([0-9a-zA-Z \-\ _] + )/? ([0-9] + )? /? $1/index. php? Action = show & alias = $2 & page = $3 last;
Discuz 7:
Rewrite ^/archiver/(fid | tid)-[\ w \-] + \. html) $/archiver/index. php? $1 last;
Rewrite ^/forum-([0-9] +)-([0-9] +) \. html $/forumdisplay. php? Fid = $1 & page = $2 last;
Rewrite ^/thread-([0-9] +)-([0-9] +)-([0-9] +) \. html $/viewthread. php? Tid = $1 & extra = page \ % 3D $3 & page = $2 last;
Rewrite ^/space-(username | uid)-(. +) \. html $/space. php? $1 = $2 last;
Rewrite ^/tag-(. +) \. html $/tag. php? Name = $1 last;
Typecho:
Location /{
Index index.html index. php;
If (-f $ request_filename/index.html ){
Rewrite (. *) $1/index.html break;
}
If (-f $ request_filename/index. php ){
Rewrite (. *) $1/index. php;
}
If (! -F $ request_filename ){
Rewrite (. *)/index. php;
}
}
 

--------------------------------------------------------------------------------

4.8.5 enable pseudo-static in shopex
......
Location/
    {
If (! -E $ request_filename ){
Rewrite ^/(. *) $/index. php? $1 last;
    }
    }
......
 
1. Enable pseudo-static
......
# Begin add by guozhenbin 20100727 (use Pseudo-static)
Location/
    {
If (! -E $ request_filename ){
Rewrite ^/(. *) $/index. php? $1 last;
    }
    }
# End add by guozhenbin 20100727
......

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.