nginx rewrite pseudo static configuration parameters and usage examples
- 2020-05-06 12:14:21
- OfStack
Regular expression matching, where:
* ~ matches
for case sensitivity * ~* matches
for case insensitivity *! ~ and! ~* for case sensitive mismatches and case insensitive mismatches
, respectively File and directory match, where:
* - f and! -f is used to determine the existence of file
* - d and! -d is used to determine if the 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 or not flag:
* last is equivalent to the [L] flag in Apache, indicating completion rewrite
* break terminates the match and no longer matches the following rule
* redirect returns to 302 the temporary redirect address bar displays the address
after the jump * permanent returns the 301 permanent redirect address bar to show the address
after the jump Some of the available global variables are available as conditional judgments (to be completed)
Combine QeePHP with
Multiple directories to parameter
Directory swap
For example, set nginx below to redirect to/nginx-ie when the user USES ie:
Directory automatically adds "/"
Ban htaccess
Multiple directories
are disabled
starting with /data is prohibited Requests under /data/ multilevel directory. Es85en.txt, etc.
Single directory
is disabled log.txt can request
Single file
is disabled
Set expiration time for favicon.ico and robots.txt;
Es110en.ico for 99 days, robots.txt for 7 days, no 404 error log
Set the expiration time of a file; Here it is 600 seconds and no access log
is recorded
File anti-hotlinking and set the expiration time
return 412 here is a custom http status code, default is 403, to find the correct hotlinking request
"rewrite ^ / http: / / leech c1gstudio. com/leech gif;" Displays an anti-hotlinking image
"access_log off;" No access logs to reduce stress
"expires 3d" browser cache of all files for 3 days
Only fixed ip is allowed to visit the website with the password
Turn files in multiple directories into one file to enhance the seo effect
Point 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/
One problem with the above example is that
will not match when accessing /shanghai
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 until it cannot be accessed.
Then I also can't add auto jump (-d $request_filename) it has a condition that it must be a real directory, and my rewrite is not, so there is no effect
It's easy to do after you know the reason. Let me jump
manually
Redirection when files and directories do not exist:
Domain name jump
Multi-domain switch to
Level 3 domain name jump
Domain mirror to
Mirror a subdirectory to
Separate the domain name
for a section of discuz
discuz ucenter heads rewrite optimized
jspace rewrite
* ~ matches
for case sensitivity * ~* matches
for case insensitivity *! ~ and! ~* for case sensitive mismatches and case insensitive mismatches
, respectively File and directory match, where:
* - f and! -f is used to determine the existence of file
* - d and! -d is used to determine if the 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 or not flag:
* last is equivalent to the [L] flag in Apache, indicating completion rewrite
* break terminates the match and no longer matches the following rule
* redirect returns to 302 the temporary redirect address bar displays the address
after the jump * permanent returns the 301 permanent redirect address bar to show the address
after the jump Some of the available global variables are available as conditional judgments (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 QeePHP with
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;
Multiple directories to parameter
abc.domian.com/sort/2 => abc.domian.com/index.php?act=sort&name=abc&id=2
if ($host ~* (.*)\.domain\.com) {
set $sub_name $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, set nginx below to redirect to/nginx-ie when the user USES ie:
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /nginx-ie/$1 break;
}
Directory automatically adds "/"
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
Ban htaccess
location ~/\.ht {
deny all;
}
Multiple directories
are disabled
location ~ ^/(cron|templates)/ {
deny all;
break;
}
starting with /data is prohibited Requests under /data/ multilevel directory. Es85en.txt, etc.
location ~ ^/data {
deny all;
}
Single directory
is disabled log.txt can request
location /searchword/cron/ {
deny all;
}
Single file
is disabled
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;
0
Set expiration time for favicon.ico and robots.txt;
Es110en.ico for 99 days, robots.txt for 7 days, no 404 error log
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;
1
Set the expiration time of a file; Here 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-hotlinking and set the expiration time
return 412 here is a custom http status code, default is 403, to find the correct hotlinking request
"rewrite ^ / http: / / leech c1gstudio. com/leech gif;" Displays an anti-hotlinking image
"access_log off;" No access logs to reduce stress
"expires 3d" browser cache of all files for 3 days
location ~* ^.+\.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {
valid_referers none blocked *.c1gstudio.com *.c1gstudio.net localhost 208.97.167.194;
if ($invalid_referer) {
rewrite ^/ http://leech.c1gstudio.com/leech.gif;
return 412;
break;
}
access_log off;
root /opt/lampp/htdocs/web;
expires 3d;
break;
}
Only fixed ip is allowed to visit the website with the password
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;
4
Turn files in multiple directories into one file to enhance the seo effect
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;
5
Point 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/
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;
6
One problem with the above example is that
will not match when accessing /shanghai
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;
7
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 until it cannot be accessed.
Then I also can't add auto jump (-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's easy to do after you know the reason. Let me jump
manually
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;
9
Redirection when files and directories do not exist:
abc.domian.com/sort/2 => abc.domian.com/index.php?act=sort&name=abc&id=2
if ($host ~* (.*)\.domain\.com) {
set $sub_name $1;
rewrite ^/sort\/(\d+)\/?$ /index.php?act=sort&cid=$sub_name&id=$1 last;
}
0
Domain name jump
server
{
listen 80;
server_name jump.c1gstudio.com;
index index.html index.htm index.php;
root /opt/lampp/htdocs/www;
rewrite ^/ http://www.c1gstudio.com/;
access_log off;
}
Multi-domain switch to
server_name www.c1gstudio.com www.c1gstudio.net;
index index.html index.htm index.php;
root /opt/lampp/htdocs;
if ($host ~ "c1gstudio\.net") {
rewrite ^(.*) http://www.c1gstudio.com$1 permanent;
}
Level 3 domain name jump
if ($http_host ~* "^(.*)\.i\.c1gstudio\.com$") {
rewrite ^(.*) http://top.yingjiesheng.com$1;
break;
}
Domain mirror to
server
{
listen 80;
server_name mirror.c1gstudio.com;
index index.html index.htm index.php;
root /opt/lampp/htdocs/www;
rewrite ^/(.*) http://www.c1gstudio.com/$1 last;
access_log off;
}
Mirror a subdirectory to
location ^~ /zhaopinhui {
rewrite ^.+ http://zph.c1gstudio.com/ 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;
Separate the domain name
for a section of discuz
server_name bbs.c1gstudio.com news.c1gstudio.com;
location = / {
if ($http_host ~ news\.c1gstudio.com$) {
rewrite ^.+ http://news.c1gstudio.com/forum-831-1.html last;
break;
}
}
discuz ucenter heads rewrite optimized
abc.domian.com/sort/2 => abc.domian.com/index.php?act=sort&name=abc&id=2
if ($host ~* (.*)\.domain\.com) {
set $sub_name $1;
rewrite ^/sort\/(\d+)\/?$ /index.php?act=sort&cid=$sub_name&id=$1 last;
}
7
jspace rewrite
abc.domian.com/sort/2 => abc.domian.com/index.php?act=sort&name=abc&id=2
if ($host ~* (.*)\.domain\.com) {
set $sub_name $1;
rewrite ^/sort\/(\d+)\/?$ /index.php?act=sort&cid=$sub_name&id=$1 last;
}
8