Nginx rewrite pseudo static configuration parameters detailed description _nginx

Source: Internet
Author: User
Tags browser cache domian

Regular expression matches, where:

  1. * ~ to Match case sensitivity
  2. * ~* matching for case-insensitive case
  3. *!~ and!~* are case insensitive and case-insensitive mismatches, respectively

File and directory matching, where:

  1. *-F and!-f are used to determine whether a file exists
  2. *-D and!-d used to determine whether a directory exists
  3. *-E and!-e used to determine whether a file or directory exists
  4. *-X and!-x are used to determine whether a file is executable

Flag tags are:

  1. * Last equivalent to the [L] tag in Apache, which means complete rewrite
  2. * Break termination match, no longer match the following rule
  3. * REDIRECT Return 302 temporary redirect Address bar will show the address after the jump
  4. * 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)

  1. $args
  2. $content _length
  3. $content _type
  4. $document _root
  5. $document _uri
  6. $host
  7. $http _user_agent
  8. $http _cookie
  9. $limit _rate
  10. $request _body_file
  11. $request _method
  12. $remote _addr
  13. $remote _port
  14. $remote _user
  15. $request _filename
  16. $request _uri
  17. $query _string
  18. $scheme
  19. $server _protocol
  20. $server _addr
  21. $server _name
  22. $server _port
  23. $uri

Combined with the qeephp example

  1. if (!-d $request _filename) {
  2. Rewrite ^/([a-z-a-z]+)/([a-z-a-z]+)/? (. *) $/index.php?namespace=user&controller=$1&action=$2&$3 last;
  3. Rewrite ^/([a-z-a-z]+)/?$/index.php?namespace=user&controller=$1 last;
  4. Break

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

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

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

  1. 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:

  1. if ($http _user_agent ~ msie) {
  2. Rewrite ^ (. *) $/nginx-ie/$1 break;
  3. }

Directory automatically add "/"

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

Prohibit htaccess

  1. Location ~/\.ht {
  2. Deny all;
  3. }

Prohibit multiple directories

  1. Location ~ ^/(cron|templates)/{
  2. Deny all;
  3. Break
  4. }

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

  1. Location ~ ^/data {
  2. Deny all;
  3. }

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

  1. location/searchword/cron/{
  2. Deny all;
  3. }

Prohibit individual files

  1. Location ~/data/sql/data.sql {
  2. Deny all;
  3. }

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

  1. Location ~ (favicon.ico) {
  2. Log_not_found off;
  3. Expires 99d;
  4. Break
  5. }
  6. Location ~ (robots.txt) {
  7. Log_not_found off;
  8. Expires 7d;
  9. Break
  10. }

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

  1. Location ^~/html/scripts/loadhead_1.js {
  2. Access_log off;
  3. Root/opt/lampp/htdocs/web;
  4. Expires 600;
  5. Break
  6. }

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
"Rewrite ^/yun_qi_img/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

  1. Location ~* ^.+\. (JPG|JPEG|GIF|PNG|SWF|RAR|ZIP|CSS|JS) $ {
  2. Valid_referers none blocked *.c1gstudio.com *.c1gstudio.net localhost 208.97.167.194;
  3. if ($invalid _referer) {
  4. Rewrite ^/yun_qi_img/leech.gif;
  5. return 412;
  6. Break
  7. }
  8. Access_log off;
  9. Root/opt/lampp/htdocs/web;
  10. Expires 3d;
  11. Break
  12. }

Only fixed IP access to the site, plus password

  1. root/opt/htdocs/www;
  2. Allow 208.97.167.194;
  3. Allow 222.33.1.2;
  4. Allow 231.152.49.4;
  5. Deny all;
  6. Auth_basic "C1g_admin";
  7. 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

  1. 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/

  1. 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

  1. Rewrite ^/([0-9a-z]+) job$/area/$1/last;
  2. 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

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

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

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

redirect When files and directories do not exist:

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

Domain Jump

  1. Server
  2. {
  3. Listen 80;
  4. server_name jump.c1gstudio.com;
  5. Index index.html index.htm index.php;
  6. root/opt/lampp/htdocs/www;
  7. Rewrite ^/http://www.c1gstudio.com/;
  8. Access_log off;
  9. }

Multi-domain Direction

  1. server_name www.c1gstudio.com www.c1gstudio.net;
  2. Index index.html index.htm index.php;
  3. Root/opt/lampp/htdocs;
  4. if ($host ~ "C1gstudio\.net") {
  5. Rewrite ^ (. *) http://www.c1gstudio.com$1 permanent;
  6. }

Level three domain jump

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

Domain name Mirror to

  1. Server
  2. {
  3. Listen 80;
  4. server_name mirror.c1gstudio.com;
  5. Index index.html index.htm index.php;
  6. root/opt/lampp/htdocs/www;
  7. Rewrite ^/(. *) http://www.c1gstudio.com/$1 last;
  8. Access_log off;
  9. }

A subdirectory is used as a mirror to

  1. Location ^~/zhaopinhui {
  2. Rewrite ^.+ http://zph.c1gstudio.com/last;
  3. Break
  4. }

Discuz Ucenter Home (uchome) rewrite

  1. Rewrite ^/(space|network)-(. +) \.html$/$1.php?rewrite=$2 last;
  2. Rewrite ^/(space|network) \.html$/$1.php last;
  3. Rewrite ^/([0-9]+) $/space.php?uid=$1 last;

Discuz 7 Rewrite

  1. Rewrite ^ (. *)/archiver/((Fid|tid)-[\w\-]+\.html) $ $1/archiver/index.php?$2 last;
  2. Rewrite ^ (. *)/forum-([0-9]+)-([0-9]+) \.html$ $1/forumdisplay.php?fid=$2&page=$3;
  3. Rewrite ^ (. *)/thread-([0-9]+)-([0-9]+) – ([0-9]+) \.html$ $1/viewthread.php?tid=$2&extra=page\%3d$4&page=$3 Last
  4. Rewrite ^ (. *)/profile-(USERNAME|UID)-(. +) \.html$ $1/viewpro.php?$2=$3 last;
  5. Rewrite ^ (. *)/space-(USERNAME|UID)-(. +) \.html$ $1/space.php?$2=$3 last;
  6. Rewrite ^ (. *)/tag-(. +) \.html$ $1/tag.php?name=$2 last;

Configure the domain name separately for discuz

  1. server_name bbs.c1gstudio.com news.c1gstudio.com;
  2. Location =/{
  3. if ($http _host ~ news\.c1gstudio.com$) {
  4. Rewrite ^.+ http://news.c1gstudio.com/forum-831-1.html last;
  5. Break
  6. }
  7. }

Discuz ucenter Avatar Rewrite optimization

  1. location ^~/ucenter {
  2. location ~. *\.php?$
  3. {
  4. #fastcgi_p Ass Unix:/tmp/php-cgi.sock;
  5. fastcgi_pass 127.0.0.1:9000;
  6. Fastcgi_index index.php;
  7. include fcgi.conf;
  8. }
  9. location/ucenter/data/avatar {
  10. log_not_found off;
  11. Access_log off;
  12. Location ~/(. *) _big\.jpg$ {
  13. error_page 404/ucenter/images/noavatar_big.gif;
  14. }
  15. location ~/(. *) _middle\.jpg$ {
  16. error_page 404/ucenter/images/noavatar_middle.gif;
  17. }
  18. location ~/(. *) _small\.jpg$ {
  19. error_page 404/ucenter/images/noavatar_small.gif;
  20. }
  21. expires;
  22. break;
  23. }
  24. }

Jspace rewrite

  1. Location ~. *\.php?$
  2. {
  3. #fastcgi_pass Unix:/tmp/php-cgi.sock;
  4. Fastcgi_pass 127.0.0.1:9000;
  5. Fastcgi_index index.php;
  6. Include fcgi.conf;
  7. }
  8. Location ~* ^/index.php/
  9. {
  10. Rewrite ^/index.php/(. *)/index.php?$1 break;
  11. Fastcgi_pass 127.0.0.1:9000;
  12. Fastcgi_index index.php;
  13. Include fcgi.conf;
  14. }

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.