Describes rewrite Rules in Nginx.

Source: Internet
Author: User
Tags nginx load balancing

I. Regular Expression matching, where:
*~ Case-sensitive matching
*~ * Case-insensitive match
*!~ And !~ * Case-insensitive and case-insensitive
2. file and directory matching, 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.
3. The last parameter of the rewrite command is the flag. The flag is marked as follows:
1. last is equivalent to the [L] Mark in apache, indicating rewrite.
2. After the break rule is matched, the match is terminated and the subsequent rule is no longer matched.
3. redirect returns the 302 temporary redirection. the browser address will display the URL address after the jump.
4. permanent returns 301 permanent redirection. the browser address will display the URL address after the jump.


Use last and break to rewrite the URI. the address bar of the browser remains unchanged. In addition, there are minor differences between the two. The alias command must use the last tag. When using the proxy_pass command, you must use the break tag. After the Last flag is executed for this rewrite rule, it will be directed to its server {......} the tag re-initiates the request, and the break tag terminates the match after the rule is matched.
For example, if we redirect a URL like/photo/123456 to/path/to/photo/12/1234/123456.png
Rewrite "/photo/([0-9] {2}) ([0-9] {2}) ([0-9] {2 }) "/path/to/photo/$1/$1 $2/1_11_21_3.png;


Iv. Instructions related to NginxRewrite rules


1. break command
Environment: server, location, if;
This command is used to complete the current rule set and does not process the rewrite command.


2. if command
Environment: server, location
This command is used to check whether a condition is met. If the condition is met, the statement in braces is executed. The If command does not support nesting and does not support multiple conditions & |.


3. return command
Syntax: returncode;
Environment: server, location, if;
This command ends rule execution and returns the status code to the client.
Example: If the accessed URL ends with ". sh" or ". bash", status code 403 is returned.
Location ~ . * \. (Sh | bash )? $
{
Return 403;
}


4. rewrite command
Syntax: rewriteregex replacement flag
Environment: server, location, if
This command redirects the URI based on the expression or modifies the string. The command is executed according to the order in the configuration file. Note that the rewrite expression is only valid for relative paths. If you want to pair the host name, you should use the if statement, for example:
If ($ host ~ * Www \.(.*))
{
Set $ host_without_www $1;
Rewrite ^ (. *) $ http: // $ host_without_www $1 permanent;
}


5. Set command
Syntax: setvariable value; default value: none; Environment: server, location, if;
This command is used to define a variable and assign a value to the variable. The value of a variable can be the combination of text, variables, and text variables.
Example: set $ varname "hello world ";


6. Uninitialized_variable_warn command
Syntax: uninitialized_variable_warnon | off
Environment: http, server, location, if
This command is used to enable and disable the warning information for uninitialized variables. The default value is enable.

 


5. Compile an instance with Nginx Rewrite Rules
1. When the accessed file and directory do not exist, redirect to a PHP File
If (! -E $ request_filename)
{
Rewrite ^/(. *) $ index. php last;
}


2. Directory swap/123456/xxxx ==>/xxxx? Id = 123456
Rewrite ^/(\ d +)/(. +) // $2? Id = $1 last;


3. If the client uses the ie browser, redirect to the/ie directory.
If ($ http_user_agent ~ MSIE)
{
Rewrite ^ (. *) $/ie/$1 break;
}


4. prohibit access to multiple directories
Location ~ ^/(Cron | templates )/
{
Deny all;
Break;
}


5. prohibit access to files starting with/data
Location ~ ^/Data
{
Deny all;
}


6.prohibit objects whose names are .sh,.flv and suffix
Location ~ . * \. (Sh | flv | mp3) $
{
Return 403;
}


7. Set the browser cache time for some types of files
Location ~ . * \. (Gif | jpg | jpeg | png | bmp | swf) $
{
Expires 30d;
}
Location ~ . * \. (Js | css) $
{
Expires 1 h;
}


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


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


10. File anti-leeching and set expiration time
Return412 indicates the custom http status code. The default value is 403, which is used to identify the correct request for leeching.
"Rewrite ^/http://img.bkjia.net/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 days


Location ~ * ^. + \. (Jpg | jpeg | gif | png | swf | rar | zip | css | js) $ {
Valid_referers none blocked * .bkjia.com * .bkjia.net localhost 208.97.167.194;
If ($ invalid_referer ){
Rewrite ^/http://img.bkjia.net/leech.gif;
Return 412;
Break;
}
Access_log off;
Root/opt/lampp/htdocs/web;
Expires 3d;
Break;
}


11. 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;


12. 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;


13. Redirection when the file and directory do not exist:


If (! -E $ request_filename ){
Proxy_pass http: // 127.0.0.1;
}


14. Point A folder under 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;


15. Domain jump
Server
{
Listen 80;
Server_name jump.bkjia.com;
Index index.html index.htm index. php;
Root/opt/lampp/htdocs/www;
Rewrite ^/l index.htm index. php;
Root/opt/lampp/htdocs;
If ($ host ~ "Bkjia \. net "){
Rewrite ^ (.*)

 

Nginx load balancing: nginx: [emerg] cocould not build the types_hash

 

Nginx Load Balancing module ngx_http_upstream_module details

 

Nginx + Firebug allows the browser to tell you which Server Load balancer distributes requests

 

Ubuntu install Nginx php5-fpm MySQL (LNMP environment setup)

 

Nginx details: click here
Nginx: click here

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.