Nginx pseudo static configuration and common Rewrite pseudo static rule collections
- 2020-05-10 23:14:33
- OfStack
In nginx, pseudo-statics are used to write the rules directly in nginx.conf, and it is not necessary to open the write module (mod_rewrite) like apache to do the pseudo-statics.
nginx simply opens the nginx.conf configuration file and writes the required rules in server.
server
{
listen 80;
server_name bbs.ofstack.com;
index index.html index.htm index.php;
root /home/www/bbs;
error_page 404 /404.htm; # configuration 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;
}
# So here's pseudo-static
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 will take effect, which is very inconvenient to maintain and 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 is an example:
1. Create a new.htaccess file in the directory using the.htaccess file, such as the following 1 Discuz forum directory:
vim /var/www/html/jb51/bbs/.htaccess
2. Enter the rule inside, I enter the pseudo-static rule of Discuz here (add only the pseudo-static rule of Discuz 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 exit.
3. Modify the nginx configuration file:
vim /etc/nginx/nginx.conf
4. In the need to add pseudo-static virtual host server{}.htaccess file:
include /var/www/html/jb51/bbs/.htaccess; Note: change the path to you .htaccess File location)
wq save exit.
5. Reload the nginx configuration file:
/etc/init.d/nginx reload
Nginx common Rewrite pseudo-static rules:
Pseudo-static rule is a very heavy parameter for us to do pseudo-static. If we can understand more, we can quickly write the optimal pseudo-static code. I have sorted out some examples for you below, hoping to help you.
This log content is from the Internet and daily use experience, sorting 1 for reference in the future.
Regular expression matching, where:
* ~ Match for case sensitivity
* ~* Match for case-insensitive
* !~ and !~* Case - insensitive mismatch and case - insensitive mismatch, respectively
File and directory matching, where:
* -f and !-f To determine if a file exists
* -d and !-d To determine if a directory exists
* -e and !-e To determine if a file or directory exists
* -x and !-x To determine if the file is executable
flag Tags are:
* last The equivalent of Apache In the [L] Mark to indicate completion rewrite
* break Termination of the match , No longer matches the following rules
* redirect return 302 Temporary redirection The address bar shows the address after the jump
* permanent return 301 Permanent redirection The address bar shows the address after the jump
1 there are some available global variables, 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
Combine the example of QeePHP
ocation /{
rewrite ^(.*)/equip(d+).html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last;
}
0
Multiple directories are turned into parameters
ocation /{
rewrite ^(.*)/equip(d+).html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last;
}
1
Directory exchange
/123456/xxxx -> /xxxx?id=123456
rewrite ^/(/d+)/(.+)/ /$2?id=$1 last;
For example nginx In user use ie The use of redirect to /nginx-ie Directory:
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /nginx-ie/$1 break;
}
Directory automatically add "/"
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
Ban htaccess
ocation /{
rewrite ^(.*)/equip(d+).html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last;
}
4
Disable multiple directories
ocation /{
rewrite ^(.*)/equip(d+).html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last;
}
5
Files starting with /data are prohibited
You can disable /data/ under multilevel directories. log.txt, etc.
ocation /{
rewrite ^(.*)/equip(d+).html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last;
}
6
Prohibit a single directory
Cannot forbid.log.txt can request
ocation /{
rewrite ^(.*)/equip(d+).html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last;
}
7
Disable a single file
ocation /{
rewrite ^(.*)/equip(d+).html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last;
}
8
Set expiration times for favicon.ico and robots.txt;
This is favicon.ico for 99 days, robots.txt for 7 days and does not log 404 errors
ocation /{
rewrite ^(.*)/equip(d+).html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last;
}
9
Set the expiration time of a file; This is 600 seconds and no access logs are logged
include /home/www/bbs/bbs_nginx.conf;
0
The file is anti-hotlinking and sets the expiration time
Here, return 412 is a custom http status code with a default of 403 to help you find the correct hotlinking request
" rewrite ^/ //www.ofstack.com/jb51.gif; "To show 1 An anti-hotlinking picture
" access_log off; "Do not log access to reduce stress
" expires 3d "All documents 3 Days of browser caching
location ~* ^.+/.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {
valid_referers none blocked *.ofstack.com *.jbzj.net localhost 1.1.1.1;
if ($invalid_referer) {
rewrite ^/ //www.ofstack.com/jb51.gif;
return 412;
break;
}
access_log off;
root /opt/lampp/htdocs/web;
expires 3d;
break;
}
Only fixed ip is allowed to access the site with 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;
To enhance the seo effect, convert the files in the multilevel directory into one file
/job-123-456-789.html Point to the /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
Such as /shanghaijob/ pointing /area/shanghai/
If you change last to permanent, the browser's address bar will be /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;
So /shanghai is also accessible, but the relative links in the page are not available,
For example, the real address of./ list_1.html is /area/shanghia/ list_1.html will become/list_1.html, which cannot be accessed.
Then I can't add automatic jump
(-d $request_filename) It has a condition that is required for the real directory and mine rewrite No, so it doesn't work
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
It's easy to do when you know why. Let me jump manually
include /home/www/bbs/bbs_nginx.conf;
7
Redirection when files and directories do not exist:
include /home/www/bbs/bbs_nginx.conf;
8
The domain name to jump
server
{
listen 80;
server_name jump.c1gstudio.com;
index index.html index.htm index.php;
root /opt/lampp/htdocs/www;
rewrite ^/ //www.ofstack.com/;
access_log off;
}
Multiple domain redirection
vim /var/www/html/jb51/bbs/.htaccess
0
Level 3 domain name jump
vim /var/www/html/jb51/bbs/.htaccess
1
Domain name is mirror
vim /var/www/html/jb51/bbs/.htaccess
2
Mirror a subdirectory
vim /var/www/html/jb51/bbs/.htaccess
3
discuz ucenter home (uchome) rewrite
vim /var/www/html/jb51/bbs/.htaccess
4
Now cms is commonly used
WordPress pseudo-static rules:
vim /var/www/html/jb51/bbs/.htaccess
5
PHPCMS pseudo-static rules:
vim /var/www/html/jb51/bbs/.htaccess
6
ECSHOP pseudo-static rules:
vim /var/www/html/jb51/bbs/.htaccess
7
SHOPEX pseudo-static rules:
vim /var/www/html/jb51/bbs/.htaccess
8
SaBlog 2.0:
vim /var/www/html/jb51/bbs/.htaccess
9
Discuz 7 pseudo-static rules:
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:
vim /var/www/html/jb51/bbs/.htaccess
5
Example: pseudo-static is enabled for shopex
# 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
2