nginx rewrite pseudo-static configuration parameters and usage examples with regular usage instructions
Regular expression matching, where:
* ~ is case-sensitive matching
* ~* is case-insensitive matching
* !~ and !~* are case-sensitive mismatches respectively And case-insensitive mismatch
File and directory match, where:
* -f and !-f are used to judge whether a file exists
* -d and !-d are used to judge whether a directory exists
* -e and !-e are used to judge Whether there is a file or directory
* -x and !-x are used to judge whether the file is executable or not. The
flags are:
* last is equivalent to the [L] mark in Apache, indicating that rewrite is completed
* break terminates the match, no longer matches the following rules
* redirect Returns 302 The temporary redirection address bar will display the redirected address
* permanent Returns 301 The permanent redirection address bar will display the redirected address
Some available global variables are, 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
结合QeePHP的例子
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;
多目录转成参数
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;
}
目录对换
/123456/xxxx -> /xxxx?id=123456
rewrite ^/(\d+)/(.+)/ /$2?id=$1 last;
例如下面设定nginx在用户使用ie的使用重定向到/nginx-ie目录下:
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /nginx-ie/$1 break;
}
目录自动加“/”
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
禁止htaccess
location ~/\.ht {
deny all;
}
禁止多个目录
location ~ ^/(cron|templates)/ {
deny all;
break;
}
禁止以/data开头的文件
可以禁止/data/下多级目录下.log.txt等请求;
location ~ ^/data {
deny all;
}
forbid a single directory
not forbid. log.txt can request
location /searchword/cron/ {
deny all;
}
forbid a single file
location ~ /data/sql/data.sql {
deny all;
}
for favicon.ico and robots .txt sets the expiration time;
here is favicon.ico is 99 days, robots.txt is 7 days and does not record 404 error log
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; here is 600 seconds, and access logs are not recorded
location ^~ /html/scripts/loadhead_1.js {
access_log off;
root /opt/lampp /htdocs/web;
expires 600;
break;
}
File anti-hotlinking and set expiration time
here return 412 is a custom http status code, the default is 403, which is convenient to find the correct hotlinking request
"rewrite ^/ http://leech.c1gstudio.com/ leech.gif;" shows a picture of anti-leech
"access_log off;" does not record access logs, reducing pressure
"expires 3d" 3-day browser cache for all files
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 ; 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 the file in the multi-level directory into a file to enhance the seo effect
/job-123-456-789.html points to /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 the second-level directory,
such as /shanghaijob/ points to /area/shanghai/
If you change last to permanent, then the browser address bar shows /location/shanghai/
rewrite ^/ ([0-9a-z]+)job/(.*)$ /area/$1/$2 last;
The problem with the above example is that accessing /shanghai will not match
rewrite ^/([0-9a-z] +)job$ /area/$1/ last;
rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;
this way /shanghai can also be accessed, but in the page The relative link of the can't be used,
For example, the real address of ./list_1.html is /area /shanghia/list_1.html will become /list_1.html, which will lead to inaccessibility.
Then I can't add automatic jump
(-d $request_filename) It has a condition that it must be a real directory, and my rewrite is not, so it has no effect
if (-d $request_filename){
rewrite ^/(.* )([^/])$ http://$host/$1$2/ permanent;
} Once
you know the reason, it will be easy, let me jump manually
rewrite ^/([0-9a-z]+)job$ /$1job/ permanent;
rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last;
Redirect when files and directories do not exist:
if (!-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 ^/ http://www.c1gstudio.com/;
access_log off;
}
multi-domain forwarding 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;
} The
third-level domain name jumps
if ($http_host ~* "^(.* )\.i\.c1gstudio\.com$") {
rewrite ^(.*) http://top.yingjiesheng.com$1;
break;
}
domain name mirror
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;
}
某个子目录作镜向
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;
Detailed description of nginx rewrite pseudo-static configuration parameters
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=326952715&siteId=291194637
Recommended
Ranking