Nginx pseudo static rewrite regular resource rollup _nginx

Source: Internet
Author: User
Tags comments browser cache domian

This station a server originally is the Windows system, uses the Isapi_rewrite to carry on the URL rewriting, has one rule is

Rewriterule ^/(. { 6}) (\d{3}) (. +)/php/http://www.xxx.com/qq$2.apk [Nc,l,nu]

In the middle of the use of {6} refers to the characters before 6 times, and then Ping Mobile Linux system below, using Nginx rewrite to rewrite the URL results loaded Nginx when prompted to error

Copy Code code as follows:

Rewrite ^/(. { 6}) (\d{3}) (. +)/php/http://www.xxx.com/qq$2.apk break;

Looking for a long time resources finally in Ken's article found a solution

Wrap the first half of the rule in double quotes in English to be used correctly

such as: Rewrite "^/(. { 6}) (\d{3}) (. +)/php/"http://www.xxx.com/qq$2.apk break;

You can parse it properly.

By the way, the Nginx support regular type of resources to complete the whole, to facilitate the next use;

nginx rewrite pseudo-static configuration parameters and examples attached with regular use instructions

Regular expression matching:

~ to match case sensitivity
~* for case-insensitive matching
!~ 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 matches the following rule
REDIRECT Return 302 The temporary REDIRECT Address bar displays the address after the jump
Permanent return 301 The Permanent redirect Address bar displays the address after the jump

$args This variable is equal to the parameter in the request line
$content _length equals the value of the "content_length" of the request line.
$content _type equivalent to the value of the "Content_Type" of the requested header
$document _root equivalent to the value specified by the root directive of the current request
$document _uri and $uri.
$host the value specified by the "host" line in the header or the name of the server to which the request arrives (no host row)
$limit _rate allow restricted connection rates
$request _method is equivalent to request method, usually "get" or "POST"
$remote _ADDR Client IP
$remote _port Client Port
$remote _user equivalent to user name, by Ngx_http_auth_basic_module certification
$request _filename The path name of the currently requested file, combined by root or alias and URI request
$request _body_file
$request _uri The complete initial URI containing the parameter
$query _string and $args.
$server _protocol is equivalent to the request agreement, using "http/1.0" or "http/1.1"
$server the IP of the server to which _ADDR request arrives, the value of this variable is generally obtained to make system calls. In order to avoid system calls, it is necessary to indicate the IP in the Listen directive and use the bind parameter.
$server the name of the server to which the _name request arrives
$server the port number of the server to which the _port request arrives
The $uri is equivalent to the URI in the current request, and can be different from the initial value, such as internal redirection or the use of the index

A combination of PHP examples

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

ABC.DOMIAN.COM/SORT/2 => abc.domian.com/index.php?act=sort&name=abc&id=2

Copy Code code as follows:

if ($host ~* (. *) \.domain\.com) {
Set $sub _name $;
Rewrite ^/sort\/(\d+) \/?$/index.php?act=sort&cid= $sub _name&id=$1 last;
}

Directory swap

/123456/xxxx->/xxxx?id=123456

Copy Code code as follows:

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:

Copy Code code as follows:

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 ^/http://leech.c1gstudio.com/leech.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 *.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 fixed IP access to the 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

/job-123-456-789.html Point to/job/123/456/789.html

Copy Code code as follows:

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.
(-D $request _filename) It has a condition that is required for the real directory, and my rewrite is not, so no effect

Copy Code code as follows:

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.c1gstudio.com/;
Access_log off;
}

Multi-domain Direction

Copy Code code as follows:

server_name http://www.c1gstudio.com/http://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;
}

Level three domain jump

Copy Code code as follows:

if ($http _host ~* "^ (. *) \.i\.c1gstudio\.com$") {
Rewrite ^ (. *) http://top.yingjiesheng.com$1/;
Break
}

Domain name Mirror to

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.c1gstudio.com/$1 last;
Access_log off;
}

A subdirectory is used as a mirror to

Copy Code code as follows:

Location ^~/zhaopinhui {
Rewrite ^.+ http://zph.c1gstudio.com/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

Copy Code code as follows:

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\=$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

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

Discuz ucenter Avatar Rewrite optimization

Copy Code code as follows:

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

Copy Code code as follows:

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

Nginx description of the regular formula

The ^~ identifier is followed by a string. Nginx will stop the matching of regular expressions after this string match (the result of the matching of the location instruction is preferred), such as: Location ^~/images/, you want to do some special work on the/images/directory, such as increase expires head, anti-theft chain, etc., but you want to except this catalog picture of all the pictures only to increase the expires head operation, this operation may be used to another location, such as: Location ~* \. (gif|jpg|jpeg) $, so, if there is a request/images/1.jpg,nginx how to decide which location to do in the operation? The result depends on the identifier ^~, if you write this: location/images/, so nginx will match 1.jpg to location ~* \. (gif|jpg|jpeg) $ In this location, this is not the result you need, and after adding the ^~ identifier, it stops searching for other location with regular after matching the/images/string.
= An exact lookup address, such as location =/It will only match the request with URI/, and if the request is/index.html, it will look for another location and will not match this, and of course can write two location,location =/ and location/, so/index.html will match to the latter, and if your site has a large number of requests, you can use this method to speed up the response of the request.

@ is named for a location, that is, a custom location, which cannot be accessed by the outside world, and can only be used for nginx-generated child requests, mainly for Error_page and Try_files.

~ to match case sensitive.
~* case-insensitive matching (matching Firefox's regular matching Firefox).
!~ doesn't match.
!~* doesn't match.

. Match any character except the line feed
\w matches letters or numbers or underscores or Chinese characters
\s matches any whitespace character
\d Matching numbers
\b Match the start or end of a word
^ Start of Match string
$ end of Match string

* Repeat 0 or more times
+ Repeat one or more times
? Repeat 0 times or once
{n} repeat n times
{N,} repeat n or more times
{N,m} repeats n to M times
*? Repeat any time, but try to repeat as little as possible
+? Repeat 1 or more times, but repeat as little as possible
?? Repeat 0 or 1 times, but repeat as little as possible
{n,m}? Repeat N to M times, but repeat as little as possible
{N,}? Repeat more than n times, but repeat as little as possible

\w matches any character that is not a letter, number, underline, Chinese character
\s matches any character that is not whitespace
\d matches any number of non-numeric characters
\b Match is not the beginning or end of a word
[^x] matches any character other than X
[^aeiou] matches any character other than the aeiou of these letters

Capture (exp) match exp, and capture text into an automatically named group
(? <name>exp) matches exp and captures the text in a group named name, or it can be written as (?) Name ' exp ')
(?: EXP) matches exp, does not capture matching text, and does not assign group numbers to this group
0 Wide assertion (? =exp) matches the position of the exp front
(? <=exp) matches the position of the exp rear
(?! EXP) match the position that follows not exp
(? <!exp) matches a position not previously exp
Comments (? #comment) This type of grouping does not have any effect on the processing of regular expressions and is used to provide comments for people to read

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.