Nginx rewrite settings and Examples

Source: Internet
Author: User

With its excellent concurrency performance, nginx is gradually replacing Apache as the first choice for Web servers. However, nginx currently has very little Chinese information and requires everyone to make contributions.

Below I will introduce nginx rewrite Module Settings and examples of WordPress and discuz. Nginx rewrite rules are much simpler and more flexible than Apache, as can be seen from the following introduction.

First, nginx can use if for condition matching. The syntax rules are similar to C, for example:

If ($ http_user_agent ~ MSIE ){
Rewrite ^ (. *) $/MSIE/$1 break;
}

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

For example:

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

Second, the rewrite rules of nginx are almost identical to those of Apache. The difference is the final flag. For example:

Rewrite ^/feed/$ last http://feed.shunz.net;

Flag labels include:

* Last is equivalent to the [l] Mark in Apache, indicating that rewrite is completed and subsequent rules are not matched.
* Break is similar to last.
* Redirect returns 302 temporary redirection
* Permanent returns 301 permanent redirection

WordPress redirection rules:

If (! -E $ request_filename ){
Rewrite ^/(index | atom | RSD) \. xml $ the http://feed.shunz.net last;
Rewrite ^ ([_ 0-9a-za-z-] + )? (/WP-. *) $2 last;
Rewrite ^ ([_ 0-9a-za-z-] + )? (/. * \. Php) $2 last;
Rewrite ^/index. php last;
}

Discuz! Redirection rules:

If (! -F $ request_filename ){
Rewrite ^/archiver/(FID | tid)-[\ W \-] + \. html) $/archiver/index. php? $1 last;
Rewrite ^/Forum-([0-9] +)-([0-9] +) \. html $/Forumdisplay. php? FID = $1 & page = $2 last;
Rewrite ^/thread-([0-9] +)-([0-9] +)-([0-9] +) \. html $/viewthread. php? Tid = $1 & extra = Page % 3d $3 & page = $2 last;
Rewrite ^/space-(username | UID)-(. +) \. html $/space. php? $1 = $2 last;
Rewrite ^/Tag-(. +) \. html $/Tag. php? Name = $1 last;
}

With its excellent concurrency performance, nginx is gradually replacing Apache as the first choice for Web servers. However, nginx currently has very little Chinese information and requires everyone to make contributions.

Below I will introduce nginx rewrite Module Settings and examples of WordPress and discuz. Nginx rewrite rules are much simpler and more flexible than Apache, as can be seen from the following introduction.

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.