Nginx pseudo static configuration and common Rewrite pseudo static rule collection
- 2020-05-07 20:56:47
- OfStack
In nginx, pseudo-static is used to write rules directly in nginx.conf, and it is not necessary to open the write module (mod_rewrite) like apache to do pseudo-static.
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 not very convenient 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 is an example:
1. Create a new.htaccess file in the directory where the.htaccess file is used, such as the following Discuz forum directory:
vim /var/www/html/jb51/bbs/.htaccess
2. To enter the rule, I enter Discuz's pseudo-static rule here (here I add Discuz's pseudo-static rule only) :
# 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 nginx profile:
vim /etc/nginx/nginx.conf
4. Introduces.htaccess file: in server{} where a pseudo-static virtual host needs to be added
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 rule:
Pseudo-static rule is that we do pseudo-static 1 very heavy parameter, if we can understand more can quickly write the optimal pseudo-static code, the following to give you a few examples, I hope to help you.
The contents of this log are from the Internet and the daily use experience.
Regular expression matching, where:
* ~ Match for case sensitivity
* ~* Match for case insensitivity
* !~ 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 whether 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 rule
* 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 for 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
combines QeePHP with
ocation /{
rewrite ^(.*)/equip(d+).html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last;
}
0
multi-directory is converted to the parameter
ocation /{
rewrite ^(.*)/equip(d+).html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last;
}
1
directory swap
/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 adds "/"
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
prohibits htaccess
ocation /{
rewrite ^(.*)/equip(d+).html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last;
}
4
prohibits multiple directories
ocation /{
rewrite ^(.*)/equip(d+).html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last;
}
5
prohibits files starting with /data
can disable /data/ under multiple levels of directory.
ocation /{
rewrite ^(.*)/equip(d+).html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last;
}
6
prohibits a single directory
cannot be disabled.log.txt can request
ocation /{
rewrite ^(.*)/equip(d+).html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last;
}
7
prohibits a single file
ocation /{
rewrite ^(.*)/equip(d+).html$ $1/index.php?m=content&c=index&a=lists&catid=$2 last;
}
8
sets expiration times for favicon.ico and robots.txt;
: favicon.ico: 99 days. robots: txt: 7 days
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 kept
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 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 website, 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;
enhances seo by turning files in multiple directories into a single 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;
points a folder in the root directory to the level 2 directory
For example, /shanghaijob/ points to /area/shanghai/
If you change last to permanent, the browser's address bar is /location/shanghai/
rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;
The 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./ list_1.html the real address is /area/shanghia/ list_1.html will become/list_1.html, which cannot be accessed.
Then I also can't add automatic jump
(-d $request_filename) It has a condition that is required for the real directory and mine rewrite No, it doesn't work
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
It's easy to do after you know the reason, 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
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.ofstack.com/;
access_log off;
}
multi-domain to
vim /var/www/html/jb51/bbs/.htaccess
0
level 3 domain name jump
vim /var/www/html/jb51/bbs/.htaccess
1
domain mirrors
vim /var/www/html/jb51/bbs/.htaccess
2
Mirror in a subdirectory
vim /var/www/html/jb51/bbs/.htaccess
3
discuz ucenter home (uchome) rewrite
vim /var/www/html/jb51/bbs/.htaccess
4
cms is now commonly substituted
WordPress pseudo-static rule:
vim /var/www/html/jb51/bbs/.htaccess
5
PHPCMS pseudo-static rule:
vim /var/www/html/jb51/bbs/.htaccess
6
ECSHOP pseudo-static rule:
vim /var/www/html/jb51/bbs/.htaccess
7
SHOPEX pseudo-static rule:
vim /var/www/html/jb51/bbs/.htaccess
8
SaBlog 2.0:
vim /var/www/html/jb51/bbs/.htaccess
9
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:
vim /var/www/html/jb51/bbs/.htaccess
5
instance: shopex enables pseudo-static
# 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