Http://www.cnblogs.com/analyzer/articles/1377684.html
]
Standard turn from: http://blog.c1gstudio.com/archives/434
Recommended reference Address:
Mailing List ARChives Official discussion area
Http://marc.info/?l=nginx
Nginx Common Application Technology Guide [Nginx Tips]
Http://bbs.linuxtone.org/thread-1685-1-1.html
This log content from the Internet and weekday use experience, collation for future reference.
regular expression matching, where:
- * ~ For case-sensitive matching
- * ~* for case-insensitive matching
- *!~ and!~* are case insensitive and case insensitive
file and directory matching, where:
- *-F and!-f to determine if a file exists
- *-D and!-d to determine if a directory exists
- *-E and!-e to determine if a file or directory exists
- *-X and!-x to determine if the file is executable
flag flags are:
- * Last equivalent to the [L] mark in Apache, indicating completion of rewrite
- * Break terminates the match and no longer matches the following rule
- * REDIRECT Returns 302 the temporary redirect Address bar displays the address after the jump
- * Permanent return 301 Permanent redirect Address bar will show the address after the jump
Some of the available global variables can be used as conditional judgments (to be complete)
- $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
examples 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 catalogs into parameters
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;
- }
Catalog Swap
/123456/xxxx-/xxxx?id=123456
- Rewrite ^/(/d+)/(. +)//$2?id=$1 last;
For example, the following setting Nginx is redirected to the/nginx-ie directory using IE's use of the user:
- if ($http _user_agent ~ MSIE) {
- Rewrite ^ (. *) $/nginx-ie/$1 break;
- }
Catalog automatically Add "/"
- if (-D $request _filename) {
- Rewrite ^/(. *) ([^/]) $/HTTP $host/$1$2/permanent;
- }
prohibit htaccess
- Location ~//.ht {
- Deny all;
- }
Disallow multiple directories
- Location ~ ^/(cron|templates)/{
- Deny all;
- Break
- }
prohibit files that start with/data
Can prohibit/data/under the multilevel directory. Log.txt and other requests;
- Location ~ ^/data {
- Deny all;
- }
prohibit a single directory
Cannot forbid. Log.txt can request
- location/searchword/cron/{
- Deny all;
- }
Prohibit individual files
- Location ~/data/sql/data.sql {
- Deny all;
- }
set expiration time for Favicon.ico and robots.txt;
Here is favicon.ico for 99 days, robots.txt for 7 days does not log 404 error logs
- Location ~ (favicon.ico) {
- Log_not_found off;
- Expires 99d;
- Break
- }
- Location ~ (robots.txt) {
- Log_not_found off;
- Expires 7d;
- Break
- }
sets the expiration time of a file, which is 600 seconds, and does not log access logs
- 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 with a default of 403, which makes it easy to find the correct hotlinking request.
"Rewrite ^/http://leech.c1gstudio.com/leech.gif;" Show a picture of the anti-theft chain
"Access_log off;" Reduce stress by not logging access logs
"Expires 3d" All files 3-day browser cache
- 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 allow fixed IP access to the website, plus a password
- 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 from a multilevel directory to a file to enhance SEO results
/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 under the root directory to a level 2 directory
such as/Shanghaijob/point to/area/Shanghai/
If you change last to permanent, then the browser address bar is/location/shanghai/
- Rewrite ^/([0-9a-z]+) job/(. *) $/area/$1/$2 last;
One of the problems with the above example is that access to/shanghai will not match
- 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,
If the real address of the./list_1.html is/area/shanghia/list_1.html will become/list_1.html, which leads to inaccessible.
Well, I'm not going to add the auto jump.
(-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 after the good, let me manually jump
- Rewrite ^/([0-9a-z]+) job$/$1job/permanent;
- Rewrite ^/([0-9a-z]+) job/(. *) $/area/$1/$2 last;
Redirects when files and directories 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.c1gstudio.com/;
- Access_log off;
- }
multi-domain steering
- 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;
- }
three-level domain jump
- if ($http _host ~* "^ (. *)/.i/.c1gstudio/.com$") {
- Rewrite ^ (. *) http://top.yingjiesheng.com$1;
- Break
- }
the domain name Mirror
- 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 for the mirror
- 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 separately for a discuz section
- 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
- 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;
- }
Nginx rewrite parameters and examples