Nginx pseudo-static configuration and commonly used Rewrite pseudo-static rules collection
The use of pseudo-static in nginx is to write rules directly in nginx.conf, and it is not necessary to open the write module (mod_rewrite) like apache to carry out pseudo-static.
nginx just needs to open the nginx.conf configuration file and write the required rules in the server.
server { listen 80; server_name bbs.jb51.net; index index.html index.htm index.php; root /home/www/bbs; error_page 404 /404.htm; 404 error page location ~.*.(php|php5)? $ { #fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fcgi.conf; location /{rewrite ^(.*)/equip(d+).html$$1/index.php? m=content&c=index&a=lists&catid=$2 last; } access_log access_log off; }
Then restart the nginx server and the pseudo-static takes effect. This is not easy to maintain. We can write it in an external file like bbs_nginx.conf
Create the bbs_nginx.conf file in the /home/www/bbs directory and write the following code:
ocation /{ rewrite ^(.*)/equip(d+).html$ $1/index.php? m=content&c=index&a=lists&catid=$2 last; }
Then add the following code after the above code:
include /home/www/bbs/bbs_nginx.conf;
In this way, bbs_nginx.conf pseudo-static rules in the root directory of the website can be managed separately.
Here's an example:
1. Create a.htaccess file in the directory that uses the.htaccess file, for example, the following Discuz forum directory:
vim /var/www/html/jb51/bbs/.htaccess
2. Enter rules inside, I enter pseudo-static rules of Discuz here (only pseudo-static rules of Discuz are added here) :
# nginx rewrite rule 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; # end nginx rewrite rule
wq Save and exit.
3. Modify the nginx configuration file:
vim /etc/nginx/nginx.conf
4. Import the.htaccess file to server{} where you want to add a pseudo-static virtual host:
include /var/www/html/jb51/bbs/.htaccess; (Note: Change the path to the exact location of your.htaccess file)
wq Save and exit.
5. Reload the nginx configuration file:
/etc/init.d/nginx reload
Nginx often Rewrite pseudo-static rules
Pseudo-static rules are a very heavy parameter that we do pseudo-static, if we can understand more we can quickly write the best pseudo-static code, the following to sort out some examples, I hope to help you.
The content of this log is from the Internet and daily use experience, collated for future reference. Regular expression matching, where:
* ~ for case sensitive matching * ~* for case insensitive matching *! ~ And! ~* Indicates case-insensitive and case-insensitive file and directory matching, respectively, where: * -f and! -f is used to check whether files * -d and! -d Indicates whether directories * -e and! Exist. -e is used to determine whether files or directories exist. * -x and! -x is used to determine whether the file is executable. The flag flag is as follows: * last is equivalent to Apache's [L] tag, Indicates complete rewrite * break terminates matching and no longer matches subsequent rules * redirect Returns 302 Temporary Redirection the address after the jump * permanent returns 301 Permanent Redirection The address after the jump
Some available global variables are available, which can be used as conditional judgment (to be completed).
$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
Combined with the 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;
Multidirectory conversion parameter
abc.domian.com/sort/2 => abc.domian.com/index.php?act=sort&name=abc&id=2 if ($host ~* (.*)/.domain/.com) { set $sub_name The $1; 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, if you want to redirect nginx to the/nginx-IE directory: if ($http_user_agent ~ MSIE) {rewrite ^(.*)$/nginx-ie/$1 break; }
Directory automatically adds /
if (-d $request_filename){ rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent; }
Disable htaccess
location ~//.ht { deny all; }
Disable multiple directories
location ~ ^/(cron|templates)/ { deny all; break; }
Files that start with /data are prohibited
You can disable requests such as.log.txt in multilevel directories under /data/.
location ~ ^/data { deny all; }
Disable single directory
.log.txt can be requested
location /searchword/cron/ { deny all; }
Disable individual file
location ~ /data/sql/data.sql { deny all; }
Set expiration dates for favicon.ico and robots.txt; In this case, favicon.ico is 99 days,robots.txt is 7 days and 404 error logs are not recorded
location ~(favicon.ico) { log_not_found off; expires 99d; break; } location ~(robots.txt) { log_not_found off; expires 7d; break; }
Set the expiration time of a file; In this case, it is 600 seconds, and no access log is recorded
location ^~ /html/scripts/loadhead_1.js { access_log off; root /opt/lampp/htdocs/web; expires 600; break; }
File anti-theft chain and set expiration time
return 412 here is a custom http status code, the default is 403, easy to find the correct pirate request
“rewrite ^/ //www.jb51.net/jb51.gif;” Display a picture of the anti-theft chain access_log off; No access logs are recorded, Reduces pressure "expires 3d" 3-day browser cache for all files location ~* ^.+/.(jpg|jpeg|gif|png|swf|rar|zip|css|js)${valid_referers none blocked *.jb51.net *.jbzj.net localhost 1.1.1.1; if ($invalid_referer) { rewrite ^/ //www.jb51.net/jb51.gif; return 412; break; } access_log off; root /opt/lampp/htdocs/web; expires 3d; break; }
Only allow fixed ip to access the website, and add 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 in multilevel directories into one file to enhance seo effects
/ job - 123-456-789. The HTML point/job / 123/456/789 HTML rewrite ^ / job - ([0-9] +) - ([0-9] +) - ([0-9] +) /. HTML $/ job / $1 / $2 / jobshow_ $3. HTML last;
Point a folder in the root directory to a level 2 directory
For example, /shanghaijob/ points to /area/shanghai/ If you change last to permanent, the browser address bar will display /location/shanghai/
rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;
One problem with the above example is that it will not match when accessing /shanghai
rewrite ^/([0-9a-z]+)job$ /area/$1/ last;
rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;
In this way, /shanghai can also be accessed, but the relative links in the page cannot be used. For example, the real address of./list_1.html is /area/shanghia/list_1.html, which will become /list_1.html and cannot be accessed. Then I can't add automatic redirection
(-d $request_filename) It has a condition that it must be a real directory, and my rewrite is not, So there is no effect if (-d $request_filename) {rewrite ^ / (. *) ([^ /]) $http://$host/$1$2/ permanent; }
It'll be easy once you know why. Let me jump manually
rewrite ^/([0-9a-z]+)job$ /$1job/ permanent; rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;
Redirect files and directories when they do not exist:
if (! -e $request_filename) {proxy_pass http://127.0.0.1; -e $request_filename) {proxy_pass http://127.0.0.1; }
Domain name jump
server { listen 80; server_name jump.c1gstudio.com; index index.html index.htm index.php; root /opt/lampp/htdocs/www; rewrite ^/ //www.jb51.net/; access_log off; }
Multiple domain switching
server_name www.c1gstudio.com www.c1gstudio.net; index index.html index.htm index.php; root /opt/lampp/htdocs; if ($host ~ “c1gstudio/.net”) { rewrite ^(.*) //www.jb51.net$1 permanent; }
Three-level domain jump
if ($http_host ~* “^(.*)/.i/.c1gstudio/.com$”) { rewrite ^(.*) //www.jb51.net$1; break; }
Domain name mirroring
server { listen 80; server_name mirror.c1gstudio.com; index index.html index.htm index.php; root /opt/lampp/htdocs/www; rewrite ^/(.*) //www.jb51.net/$1 last; access_log off; }
A subdirectory is mirrored
location ^~ /php { rewrite ^.+ https://www.jb51.net/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 domain name server_name bbs.jb51.net news.jb51.net separately for a section of discuz; location = / { if ($http_host ~ news/.jb51.net$) { rewrite ^.+ http://news.jb51.net/forum-831-1.html last; break; }} discuz ucenter profile picture rewrite Optimize 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; }
Attached are commonly used cms replacements
WordPress pseudo-static rules:
location / { index index.html index.php; if (-f $request_filename/index.html){ rewrite (.*) $1/index.html break; } if (-f $request_filename/index.php){ rewrite (.*) $1/index.php; } if (! -f $request_filename){ rewrite (.*) /index.php; } } rewrite /wp-admin$ $scheme://$host$uri/ permanent;
PHPCMS pseudo-static rules:
Below the location / {# # # for PHPCMS pseudo static rewrite rules rewrite ^ (. *) show - ([0-9] +) - ([0-9] +) \. HTML $$1 / show. PHP? itemid=$2&page=$3; rewrite ^(.*)list-([0-9]+)-([0-9]+)\.html$ $1/list.php? catid=$2&page=$3; rewrite ^(.*)show-([0-9]+)\.html$ $1/show.php? specialid=$2; # # # # below PHPWind pseudo static rewrite rules rewrite ^ (. *) - HTM - (. *) $$1. PHP? $2 last; rewrite ^(.*)/simple/([a-z0-9\_]+\.html)$ $1/simple/index.php? $2 last; }
ECSHOP pseudo-static rules:
if (! -e $request_filename) { rewrite "^/index\.html" /index.php last; rewrite "^/category$" /index.php last; rewrite "^/feed-c([0-9]+)\.xml$” /feed.php? cat=$1 last; rewrite “^/feed-b([0-9]+)\.xml$” /feed.php? brand=$1 last; rewrite “^/feed\.xml$” /feed.php last; rewrite “^/category-([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^-]*)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$” /category.php? id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5&page=$6&sort=$7&order=$8 last; rewrite “^/category-([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^-]*)(.*)\.html$” /category.php? id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5 last; rewrite “^/category-([0-9]+)-b([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$” /category.php? id=$1&brand=$2&page=$3&sort=$4&order=$5 last; rewrite “^/category-([0-9]+)-b([0-9]+)-([0-9]+)(.*)\.html$” /category.php? id=$1&brand=$2&page=$3 last; rewrite “^/category-([0-9]+)-b([0-9]+)(.*)\.html$” /category.php? id=$1&brand=$2 last; rewrite “^/category-([0-9]+)(.*)\.html$” /category.php? id=$1 last; rewrite “^/goods-([0-9]+)(.*)\.html” /goods.php? id=$1 last; rewrite “^/article_cat-([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$” /article_cat.php? id=$1&page=$2&sort=$3&order=$4 last; rewrite “^/article_cat-([0-9]+)-([0-9]+)(.*)\.html$” /article_cat.php? id=$1&page=$2 last; rewrite “^/article_cat-([0-9]+)(.*)\.html$” /article_cat.php? id=$1 last; rewrite “^/article-([0-9]+)(.*)\.html$” /article.php? id=$1 last; rewrite “^/brand-([0-9]+)-c([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)\.html” /brand.php? id=$1&cat=$2&page=$3&sort=$4&order=$5 last; rewrite “^/brand-([0-9]+)-c([0-9]+)-([0-9]+)(.*)\.html” /brand.php? id=$1&cat=$2&page=$3 last; rewrite “^/brand-([0-9]+)-c([0-9]+)(.*)\.html” /brand.php? id=$1&cat=$2 last; rewrite “^/brand-([0-9]+)(.*)\.html” /brand.php? id=$1 last; rewrite “^/tag-(.*)\.html” /search.php? keywords=$1 last; rewrite “^/snatch-([0-9]+)\.html$” /snatch.php? id=$1 last; rewrite “^/group_buy-([0-9]+)\.html$” /group_buy.php? act=view&id=$1 last; rewrite “^/auction-([0-9]+)\.html$” /auction.php? act=view&id=$1 last; rewrite “^/exchange-id([0-9]+)(.*)\.html$” /exchange.php? id=$1&act=view last; rewrite “^/exchange-([0-9]+)-min([0-9]+)-max([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$” /exchange.php? cat_id=$1&integral_min=$2&integral_max=$3&page=$4&sort=$5&order=$6 last; rewrite ^/exchange-([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$” /exchange.php? cat_id=$1&page=$2&sort=$3&order=$4 last; rewrite “^/exchange-([0-9]+)-([0-9]+)(.*)\.html$” /exchange.php? cat_id=$1&page=$2 last; rewrite “^/exchange-([0-9]+)(.*)\.html$” /exchange.php? cat_id=$1 last; }
SHOPEX pseudo-static rules:
location / { if (! -e $request_filename) { rewrite ^/(.+\.(html|xml|json|htm|php|jsp|asp|shtml))$ /index.php? $1 last; }}
SaBlog 2.0:
# Month only archive rewrite "^/date/([0-9]{6})/? ([0-9] +)? /? $" /index.php? action=article&setdate=$1&page=$2 last; rewrite ^/page/([0-9]+)? /? $ /index.php? action=article&page=$1 last; rewrite ^/category/([0-9]+)/? ([0-9] +)? /? $ /index.php? action=article&cid=$1&page=$2 last; rewrite ^/category/([^/]+)/? ([0-9] +)? /? $ /index.php? action=article&curl=$1&page=$2 last; rewrite ^/(archives|search|article|links)/? $ /index.php? action=$1 last; rewrite ^/(comments|tagslist|trackbacks|article)/? ([0-9] +)? /? $ /index.php? action=$1&page=$2 last; # tags rewrite ^/tag/([^/]+)/? ([0-9] +)? /? $ /index.php? action=article&item=$1&page=$2 last; rewrite ^/archives/([0-9]+)/? ([0-9] +)? /? $ /index.php? action=show&id=$1&page=$2 last; # RSS rewrite ^/rss/([0-9]+)? /? $ /rss.php? cid=$1 last; rewrite ^/rss/([^/]+)/? $ /rss.php? url=$1 last; rewrite ^/uid/([0-9]+)/? ([0-9] +)? /? $ /index.php? action=article&uid=$1&page=$2 last; rewrite ^/user/([^/]+)/? ([0-9] +)? /? $ /index.php? action=article&user=$1&page=$2 last; # Map file rewrite sitemap.xml sitemap.php last; # Custom link rewrite ^(.*)/([0-9a-zA-Z\-\_]+)/? ([0-9] +)? /? $ $1/index.php? action=show&alias=$2&page=$3 last;
Discuz 7 Pseudo static rule:
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;
Typecho:
location / { index index.html index.php; if (-f $request_filename/index.html){ rewrite (.*) $1/index.html break; } if (-f $request_filename/index.php){ rewrite (.*) $1/index.php; } if (!-f $request_filename){ rewrite (.*) /index.php; } }
Example: shopex enables pseudo-static
... location / { if (! -e $request_filename) { rewrite ^/(.*)$ /index.php? $1 last; }}...
To this article on Nginx pseudo-static configuration and commonly used Rewrite pseudo-static rules collection article is introduced to this, more related Nginx pseudo-static configuration and commonly used Rewrite pseudo-static rules content please search the previous articles of the script house or continue to browse the following related articles hope that everyone will support the script house in the future!
- nginx rewrite pseudo-static configuration parameters and use examples
- Example of using rewrite to rewrite urls under the Nginx server to implement pseudo-static
- nginx rewrite method to implement URL jump
- Explain location and rewrite usage summary in nginx in detail
- Explain nginx rewrite and location based on url parameter
- nginx pseudo-static Rewrite regular resource summary
- Detail nginx configuration location summary and rewrite rule writing
- nginx rewrite pseudo-static configuration parameters Description
- Explain common nginx rewrite rewriting rules in detail
- nginx rewrite parameter parsing
Related article
-
Describes the Nginx server setup and basic configuration
This article mainly introduces the Nginx server construction and basic configuration details,Nginx is an event-driven high-performance server, need friends can refer to the next2015-09-09 -
Nginx automatically jumps from http to https
This article mainly introduces Nginx to achieve http automatically jump to https, the article through the example code is very detailed, for everyone's study or work has a certain reference learning value, the need of friends below with the small series to learn it2023-01-01 -
How to configure FastCGI optimization in Nginx
This article mainly introduces how to configure and optimize Nginx FastCGI, Xiaobian feel very good, now share with you, also give you a reference. Let's take a look2018-08-08 -
This article mainly introduces the relevant information about Nginx + Tomcat to achieve the separation of request dynamic data and request static resources. The article introduces it in great detail through the example code, which has certain reference value for everyone's study or work. The friends who need it will learn together with the small series below2018-07-07
-
Explain in detail how nginx+php executes requests
This article mainly introduces a detailed explanation of the working principle of nginx+php execution request, Xiaobian feel very good, now share with you, but also give you a reference. Let's take a look2019-02-02 -
This section describes how to configure the maximum number of Nginx connections
This article mainly introduces the Nginx maximum connection number configuration method in detail, the example code in the article is very detailed, has a certain reference value, interested partners can refer to it2022-07-07 -
Example code for IP access diversion through Nginx reverse proxy
This article mainly introduces the example code of IP access diversion through Nginx reverse proxy, Xiaobian feel very good, now share with you, but also give you a reference. Let's take a look2017-11-11 -
An article to thoroughly understand Nginx's.conf file path configuration
Nginx. conf file is the main configuration file of Nginx, which contains the global configuration of Nginx and the configuration of each virtual host. This article mainly introduces the relevant information about nginx.conf file path configuration2023-12-12 -
Nginx prevents malicious resolution by domain names
This article mainly introduces the relevant information of Nginx to prevent malicious resolution of domain names, I hope this article can help you, let you understand this part of the content, need friends can refer to2017-10-10 -
Common Nginx configuration errors
For many people, configuring Nginx+PHP is nothing more than searching for a tutorial and then copying and pasting it. Sounds like there is no problem, but in fact, a lot of information on the network itself is in disrepair, full of loopholes, if you do not seek to understand, blindly copy and paste, sooner or later one day will pay for this2014-03-03
Latest comments