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.