Nginx pseudo-static configuration and a collection of common rewrite pseudo-static rules _nginx

Source: Internet
Author: User
Tags browser cache nginx server domian

Nginx in the use of pseudo static is directly in the nginx.conf write rules, do not need to be like Apache to open the Write module (mod_rewrite) in order to perform pseudo static.

Nginx only need to open the nginx.conf configuration file, write the required rules in the server.

Copy Code code as follows:

Server
{
listen       80;
server_name  bbs.jb51.net;
Index index.html index.htm index.php;
root /home/www/bbs;

error_page  404                                              /404.htm;        #配置404错误页面
Location ~. *. ( PHP|PHP5)? $
{
#fastcgi_pass   unix:/tmp/php-cgi.sock;
fastcgi_pass  127.0.0.1:9000;
Fastcgi_index index.php;
include fcgi.conf;
}

#下面就是伪静态了

Location/{
Rewrite ^ (. *)/equip (d+). html$ $1/index.php?m=content&c=index&a =lists&catid=$2 last;
}
access_log  access_log   off;
}

Then restart the Nginx server pseudo static is effective, this maintenance is very inconvenient we can write it in the external file such as bbs_nginx.conf

Create the bbs_nginx.conf file in the/home/www/bbs directory and write the following code:

Copy Code code as follows:

Ocation/{
Rewrite ^ (. *)/equip (d+). html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last;
}

Then add the following code after the above code:

Copy Code code as follows:

include/home/www/bbs/bbs_nginx.conf;

This enables bbs_nginx.conf pseudo static rules in the root directory of the site to be managed separately.

Here is an example:

1. Create a new. htaccess file in the directory using the. htaccess file, such as the following Discuz forum directory:

Copy Code code as follows:

Vim/var/www/html/jb51/bbs/.htaccess

2. Enter the rules here, I enter discuz pseudo static rules here (add only pseudo static rules for Discuz):

Copy Code code as follows:

# nginx Rewrite rule
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;
# End Nginx Rewrite rule

Wq Save exit.

3. Modify Nginx configuration file:

Copy Code code as follows:

Vim/etc/nginx/nginx.conf

4. Introduce the. htaccess file in the server{} that requires the addition of pseudo static virtual hosts:

Copy Code code as follows:

include/var/www/html/jb51/bbs/.htaccess; (Remark: Change the path to the exact location of your. htaccess file)

Wq Save exit.

5. Reload the Nginx configuration file:

Copy Code code as follows:

/etc/init.d/nginx Reload

Nginx Common rewrite pseudo static rules:

Pseudo-static rules are we do pseudo static a very heavy parameter, if we can understand the more we can quickly write the best pseudo static code, the following for you to organize some examples, hoping to help you.

This log content from the Internet and weekday use experience, organize a convenient reference for the future.
Regular expression matches, where:

Copy Code code as follows:

* ~ to Match case sensitivity
* ~* matching for case-insensitive case
*!~ and!~* are case insensitive and case-insensitive mismatches, respectively
File and directory matching, where:
*-F and!-f are used to determine whether a file exists
*-D and!-d used to determine whether a directory exists
*-E and!-e used to determine whether a file or directory exists
*-X and!-x are used to determine whether a file is executable
Flag tags are:
* Last equivalent to the [L] tag in Apache, which means complete rewrite
* Break termination match, no longer match the following rule
* REDIRECT Return 302 temporary redirect Address bar will show the address after the jump
* Permanent return 301 Permanent redirect Address bar will show the address after the jump

Some of the global variables available can be used as conditional judgments (pending completion)

Copy Code code 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

Combined with the qeephp example

Copy Code code 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

Multi-Directory conversion parameters

Copy Code code 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 $;
Rewrite ^/sort//(/d+)//?$/index.php?act=sort&cid= $sub _name&id=$1 last;
}

Directory swap

Copy Code code as follows:

/123456/xxxx->/xxxx?id=123456
Rewrite ^/(/d+)/(. +)//$2?id=$1 last;
For example, the following settings Nginx are redirected to the/nginx-ie directory using the user's use of IE:
if ($http _user_agent ~ msie) {
Rewrite ^ (. *) $/nginx-ie/$1 break;
}

Directory automatically add "/"

Copy Code code as follows:

if (-D $request _filename) {
Rewrite ^/(. *) ([^/]) $ http://$host/$1$2/permanent;
}

Prohibit htaccess

Copy Code code as follows:

Location ~//.ht {
Deny all;
}

Prohibit multiple directories

Copy Code code as follows:

Location ~ ^/(cron|templates)/{
Deny all;
Break
}

Prohibit files that start with/data
Can prohibit/data/under the multi-level directory. Log.txt and other requests;

Copy Code code as follows:

Location ~ ^/data {
Deny all;
}

Prohibit a single directory
cannot be forbidden. Log.txt can request

Copy Code code as follows:

location/searchword/cron/{
Deny all;
}

Prohibit individual files

Copy Code code as follows:

Location ~/data/sql/data.sql {
Deny all;
}

Set expiration time for Favicon.ico and robots.txt;
Here for Favicon.ico 99 days, robots.txt 7 days does not record 404 error log

Copy Code code as follows:

Location ~ (favicon.ico) {
Log_not_found off;
Expires 99d;
Break
}
Location ~ (robots.txt) {
Log_not_found off;
Expires 7d;
Break
}

Sets the expiration time for a file; This is 600 seconds, and the access log is not logged

Copy Code code as follows:

Location ^~/html/scripts/loadhead_1.js {
Access_log off;
Root/opt/lampp/htdocs/web;
Expires 600;
Break
}

File anti-hotlinking and set expiration time
The return 412 here is a custom HTTP status code, the default is 403, to easily find the right hotlinking request

Copy Code code as follows:

"Rewrite ^/yun_qi_img/jb51.gif;" Display an anti-theft chain picture
"Access_log off;" Do not log access logs to reduce stress
"Expires 3d" browser cache for all files 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 allow fixed IP access to the Web site, plus password

Copy Code code 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;

turn the files in a multilevel directory into a file to enhance the SEO effect

Copy Code code as follows:

/job-123-456-789.html Point 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 level 2 directory

such as/shanghaijob/point to/area/shanghai/
If you change the last to permanent, then the browser address bar is/location/shanghai/

Copy Code code as follows:

Rewrite ^/([0-9a-z]+) job/(. *) $/area/$1/$2 last;

The problem with the example above is that it will not match when accessing the/shanghai

Copy Code code as follows:

Rewrite ^/([0-9a-z]+) job$/area/$1/last;
Rewrite ^/([0-9a-z]+) job/(. *) $/area/$1/$2 last;

This/shanghai can also be accessed, but the relative links in the page are not available.
such as./list_1.html real address is/area/shanghia/list_1.html will become/list_1.html, lead to inaccessible.
Then I'll go with the automatic jump.

Copy Code code as follows:

(-D $request _filename) It has a condition that is required for the real directory, and my rewrite is not, so no effect
if (-D $request _filename) {
Rewrite ^/(. *) ([^/]) $ http://$host/$1$2/permanent;
}

Know the reason to do it, let me manually jump it

Copy Code code as follows:

Rewrite ^/([0-9a-z]+) job$/$1job/permanent;
Rewrite ^/([0-9a-z]+) job/(. *) $/area/$1/$2 last;

redirect When files and directories do not exist:

Copy Code code as follows:

if (!-e $request _filename) {
Proxy_pass http://127.0.0.1;
}

Domain Jump

Copy Code code 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.jb51.net/;
Access_log off;
}

Multi-domain Direction

Copy Code code 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.jb51.net$1 permanent;
}

Level three domain jump

Copy Code code as follows:

if ($http _host ~* "^ (. *)/.i/.c1gstudio/.com$") {
Rewrite ^ (. *) http://www.jb51.net$1;
Break
}

Domain Name Mirroring

Copy Code code as follows:

Server
{
Listen 80;
server_name mirror.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 for mirroring

Copy Code code as follows:

Location ^~/php {
Rewrite ^.+ http://www.jb51.net/last;
Break
}

Discuz Ucenter Home (uchome) rewrite

Copy Code code as follows:



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;


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 the domain name separately for discuz


server_name bbs.jb51.net news.jb51.net;


Location =/{


if ($http _host ~ news/.jb51.net$) {


Rewrite ^.+ http://news.jb51.net/forum-831-1.html last;


Break


}


}


Discuz ucenter Avatar Rewrite optimization


Location ^~/ucenter {


Location ~. */.php?$


{


#fastcgi_pass Unix:/tmp/php-cgi.sock;


Fastcgi_pass 127.0.0.1:9000;


Fastcgi_index index.php;


Include fcgi.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 rewrite


Location ~. */.php?$


{


#fastcgi_pass Unix:/tmp/php-cgi.sock;


Fastcgi_pass 127.0.0.1:9000;


Fastcgi_index index.php;


Include fcgi.conf;


}


Location ~* ^/index.php/


{


Rewrite ^/index.php/(. *)/index.php?$1 break;


Fastcgi_pass 127.0.0.1:9000;


Fastcgi_index index.php;


Include fcgi.conf;


}


Attached now common CMS replacement

WordPress pseudo Static rule:

Copy Code code as follows:

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 pseudo static rule:

Copy Code code as follows:

Location/{
# # #以下为PHPCMS pseudo-static rewrite law
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 law
Rewrite ^ (. *)-htm-(. *) $ $1.php?$2 last;
Rewrite ^ (. *)/simple/([a-z0-9\_]+\.html) $ $1/simple/index.php?$2 last;
}

Ecshop pseudo static rule:

Copy Code code as follows:



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 pseudo static rule:

Copy Code code as follows:

Location/{
if (!-e $request _filename) {
Rewrite ^/(. +\. ( html|xml|json|htm|php|jsp|asp|shtml)) $/index.php?$1 last;
}
}

Sablog 2.0:

Copy Code code as follows:



# Archive with only the month


Rewrite "^/date/([0-9]{6})/? ([0-9]+)/?$ "/index.php?action=article&setdate=$1&page=$2 last;


# no category page


Rewrite ^/page/([0-9]+)?/?$/index.php?action=article&page=$1 last;


# classification


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, Advanced raid


Rewrite ^/(archives|search|article|links)/?$/index.php?action=$1 last;


# full criticism, tag list, List of references 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 File


Rewrite Sitemap.xml sitemap.php last;


# Self-defined links


Rewrite ^ (. *)/([0-9a-za-z\-\_]+)/? ([0-9]+)/?$ $1/index.php?action=show&alias=$2&page=$3 last;


Discuz 7 pseudo-static rules:

Copy Code code as follows:

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:

Copy Code code as follows:

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;
}
}

instance: Shopex enable pseudo static

Copy Code code as follows:

......

Location/

{

if (!-e $request _filename) {

Rewrite ^/(. *) $/index.php?$1 last;

}

}

......

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.