Nginx rewrite parameters and examples

Source: Internet
Author: User
Tags domian

Original address: 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, organize a convenient reference for the future.

Regular expression matching, where: * ~ for Case Sensitivity * ~* matching case insensitive *!~ and!~* for case-insensitive mismatches and case-insensitive mismatches respectively

File and directory matching, where: * F and!-f are used to determine whether the existence of files *-D and!-d are used to determine whether the existence of directories *-E and!-E is used to determine whether a file or directory *-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 the rewrite * break termination match, no longer matches the following rule * Redirect Return 302 temporary redirect Address bar will show after the jump address * permanent return 301 Forever Long redirect Address bar will show the address after the jump

Some of the global variables available can be used as conditional judgments (to be filled) $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

Combine qeephp example 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 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 rewrite ^/(\d+)/(. +)//$2?id=$1 last;

For example, the following settings are nginx in the user using IE using the redirect to/nginx-ie directory: if ($http _user_agent ~ msie) {Rewrite ^ (. *) $/nginx-ie/$1 break;}

The directory automatically adds "/" if (-D $request _filename) {rewrite ^/(. *) ([^/]) $ http://$host/$1$2/permanent;}

Prohibit htaccess location ~/\.ht {deny all; }

Prohibit multiple directories location ~ ^/(cron|templates)/{deny all; }

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

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.