作为程序员一定要保持良好的睡眠,才能好编程

nginx Rewrite重写规则及目录限制参数说明

发布时间:2019-06-10

已经配置好nginx了

今天我们来看看如何设置nginx下的url重写

好,我们来看看如何设置吧:



Rewrite只能放在server{},location{},if{}中,并且只能对域名后边的除去传递的参数外的字符串起作用


一、正则表达式匹配,

    *~ 区分大小写

    *~* 不区分大小写

二、文件及目录匹配

    -f和!-f用来判断文件是否存在

     -d和!-d用来判断目录是否存在

    -e和!-e用来判断文件或目录是否存在

    -x和!-x用来判断文件是否可以执行

三、rewrite指令的最后一项参数为flag标记,此flag的标记有哪几项?

    1、last 相当于apache里面的[L]标记,表示rewrite。

    2、break 本条规则匹配完成后,终止匹配,不再匹配后面的规则。

    3、redirect 返回302临时重定向,浏览器地址会显示跳转后的url地址

    4、permanent 返回301永久重定向,浏览器地址会显示跳转后的url地址


用last和break实现URI重写,浏览器地址栏不变。而且两者有细微差别,使用alias指令必须用last标记;使用proxy_pass指令时,需要使用break标记。Last标记在本条rewrite规则执行完毕后,会对其所在server{......}标签重新发起请求,而break标记则在本条规则匹配完成后,终止匹配。



<?php 

    phpinfo();

?>



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;
}


rewrite /search/([^/])/([\d]+) /search.php?key=$1&page=$2 last; 这条语句匹配不能适用 / 开头 也就是第一个参数可以是除/以外的任意字符 中文

#rewrite "/category/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)" /category.php?categories_name=$1&categories_code=$2 last;

rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
rewrite ^([^\.]*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3 last;
rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
rewrite ^([^\.]*)/blog-([0-9]+)-([0-9]+)\.html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3 last;
rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/index.php?action=$2&value=$3 last;
rewrite ^([^\.]*)/([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ $1/plugin.php?id=$2:$3 last;
#if (!-e $request_filename) {
#	return 404;
#}



四.NginxRewrite 规则相关指令



1.break指令

使用环境:server,location,if;

该指令的作用是完成当前的规则集,不再处理rewrite指令。



2.if指令

使用环境:server,location

该指令用于检查一个条件是否符合,如果条件符合,则执行大括号内的语句。If指令不支持嵌套,不支持多个条件&&和||处理。




3.return指令

语法:returncode ;

使用环境:server,location,if;

该指令用于结束规则的执行并返回状态码给客户端。

示例:如果访问的URL以".sh"或".bash"结尾,则返回403状态码

location ~ .*\.(sh|bash)?$

{

return 403;

}



4.rewrite 指令

语法:rewriteregex replacement flag

使用环境:server,location,if

该指令根据表达式来重定向URI,或者修改字符串。指令根据配置文件中的顺序来执行。注意重写表达式只对相对路径有效。如果你想配对主机名,你应该使用if语句,示例如下:

if( $host ~* www\.(.*) )

{

set $host_without_www $1;

rewrite ^(.*)$  http://$host_without_www$1permanent;

}



5.Set指令

语法:setvariable value ; 默认值:none; 使用环境:server,location,if;

该指令用于定义一个变量,并给变量赋值。变量的值可以为文本、变量以及文本变量的联合。

示例:set$varname "hello world";



6.Uninitialized_variable_warn指令

语法:uninitialized_variable_warnon|off

使用环境:http,server,location,if

该指令用于开启和关闭未初始化变量的警告信息,默认值为开启。


 



五.Nginx的Rewrite规则编写实例

1.当访问的文件和目录不存在时,重定向到某个php文件

if( !-e $request_filename )

{

rewrite ^/(.*)$ index.php last;

}



2.目录对换 /123456/xxxx  ====>  /xxxx?id=123456

rewrite ^/(\d+)/(.+)/  /$2?id=$1 last;



3.如果客户端使用的是IE浏览器,则重定向到/ie目录下

if( $http_user_agent  ~ MSIE)

{

rewrite ^(.*)$ /ie/$1 break;

}



4.禁止访问多个目录

location ~ ^/(cron|templates)/

{

deny all;

break;

}



5.禁止访问以/data开头的文件

location ~ ^/data

{

deny all;

}



6.禁止访问以.sh,.flv,.mp3为文件后缀名的文件

location ~ .*\.(sh|flv|mp3)$

{

return 403;

}



7.设置某些类型文件的浏览器缓存时间

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

{

expires 30d;

}

location ~ .*\.(js|css)$

{

expires 1h;

}



8.给favicon.ico和robots.txt设置过期时间;

这里为favicon.ico为99天,robots.txt为7天并不记录404错误日志

location ~(favicon.ico) {

log_not_found off;

expires 99d;

break;

}

location ~(robots.txt) {

log_not_found off;

expires 7d;

break;

}



9.设定某个文件的过期时间;这里为600秒,并不记录访问日志

location ^~ /html/scripts/loadhead_1.js {

access_log  off;

root /opt/lampp/htdocs/web;

expires 600;

break;

}



10.文件反盗链并设置过期时间

这里的return412 为自定义的http状态码,默认为403,方便找出正确的盗链的请求

“rewrite ^/ http://img.linuxidc.net/leech.gif;”显示一张防盗链图片

“access_log off;”不记录访问日志,减轻压力

“expires 3d”所有文件3天的浏览器缓存



location ~*^.+\.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {

valid_referers none blocked *.linuxidc.com*.linuxidc.net localhost 208.97.167.194;

if ($invalid_referer) {

rewrite ^/ http://img.linuxidc.net/leech.gif;

return 412;

break;

}

access_log  off;

root /opt/lampp/htdocs/web;

expires 3d;

break;

}



11.只允许固定ip访问网站,并加上密码



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;



12将多级目录下的文件转成一个文件,增强seo效果

/job-123-456-789.html 指向/job/123/456/789.html



rewrite^/job-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /job/$1/$2/jobshow_$3.html last;



13.文件和目录不存在的时候重定向:



if (!-e $request_filename) {

proxy_pass http://127.0.0.1;

}



14.将根目录下某个文件夹指向2级目录

如/shanghaijob/ 指向 /area/shanghai/

如果你将last改成permanent,那么浏览器地址栏显是/location/shanghai/

rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2last;

上面例子有个问题是访问/shanghai时将不会匹配

rewrite ^/([0-9a-z]+)job$ /area/$1/ last;

rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2last;

这样/shanghai 也可以访问了,但页面中的相对链接无法使用,

如./list_1.html真实地址是/area/shanghia/list_1.html会变成/list_1.html,导至无法访问。

那我加上自动跳转也是不行咯

(-d $request_filename)它有个条件是必需为真实目录,而我的rewrite不是的,所以没有效果

if (-d $request_filename){

rewrite ^/(.*)([^/])$ http://$host/$1$2/permanent;

}

知道原因后就好办了,让我手动跳转吧

rewrite ^/([0-9a-z]+)job$ /$1job/permanent;

rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2last;



15.域名跳转

server

{

listen      80;

server_name  jump.linuxidc.com;

index index.html index.htm index.php;

root  /opt/lampp/htdocs/www;

rewrite ^/ http://www.linuxidc.com/;

access_log  off;

}



16.多域名转向

server_name  www.linuxidc.comwww.linuxidc.net;

index index.html index.htm index.php;

root  /opt/lampp/htdocs;

if ($host ~ "linuxidc\.net") {

rewrite ^(.*) http://www.linuxidc.com$1permanent;

}



六.nginx全局变量

arg_PARAMETER    #这个变量包含GET请求中,如果有变量PARAMETER时的值。

args                    #这个变量等于请求行中(GET请求)的参数,如:foo=123&bar=blahblah;

binary_remote_addr #二进制的客户地址。

body_bytes_sent    #响应时送出的body字节数数量。即使连接中断,这个数据也是精确的。

content_length    #请求头中的Content-length字段。

content_type      #请求头中的Content-Type字段。

cookie_COOKIE    #cookie COOKIE变量的值

document_root    #当前请求在root指令中指定的值。

document_uri      #与uri相同。

host                #请求主机头字段,否则为服务器名称。

hostname          #Set to themachine’s hostname as returned by gethostname

http_HEADER

is_args              #如果有args参数,这个变量等于”?”,否则等于”",空值。

http_user_agent    #客户端agent信息

http_cookie          #客户端cookie信息

limit_rate            #这个变量可以限制连接速率。

query_string          #与args相同。

request_body_file  #客户端请求主体信息的临时文件名。

request_method    #客户端请求的动作,通常为GET或POST。

remote_addr          #客户端的IP地址。

remote_port          #客户端的端口。

remote_user          #已经经过Auth Basic Module验证的用户名。

request_completion #如果请求结束,设置为OK. 当请求未结束或如果该请求不是请求链串的最后一个时,为空(Empty)。

request_method    #GET或POST

request_filename  #当前请求的文件路径,由root或alias指令与URI请求生成。

request_uri          #包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。不能修改。

scheme                #HTTP方法(如http,https)。

server_protocol      #请求使用的协议,通常是HTTP/1.0或HTTP/1.1。

server_addr          #服务器地址,在完成一次系统调用后可以确定这个值。

server_name        #服务器名称。

server_port          #请求到达服务器的端口号。



七.Apache和Nginx规则的对应关系


Apache的RewriteCond对应Nginx的if

Apache的RewriteRule对应Nginx的rewrite

Apache的[R]对应Nginx的redirect

Apache的[P]对应Nginx的last

Apache的[R,L]对应Nginx的redirect

Apache的[P,L]对应Nginx的last

Apache的[PT,L]对应Nginx的last



例如:允许指定的域名访问本站,其他的域名一律转向www.linuxidc.net

  Apache:

RewriteCond %{HTTP_HOST} !^(.*?)\.aaa\.com$[NC]

RewriteCond %{HTTP_HOST} !^localhost$ 

RewriteCond %{HTTP_HOST}!^192\.168\.0\.(.*?)$

RewriteRule ^/(.*)$ http://www.linuxidc.net[R,L]



  Nginx:

if( $host ~* ^(.*)\.aaa\.com$ )

{

set $allowHost ‘1’;

}

if( $host ~* ^localhost )

{

set $allowHost ‘1’;

}

if( $host ~* ^192\.168\.1\.(.*?)$ )

{

set $allowHost ‘1’;

}

if( $allowHost !~ ‘1’ )

{

rewrite ^/(.*)$ http://www.linuxidc.netredirect ;

}




七.常用正则

. : 匹配除换行符以外的任意字符
? : 重复0次或1次
+ : 重复1次或更多次
* : 重复0次或更多次
\d :匹配数字
^ : 匹配字符串的开始
$ : 匹配字符串的介绍
{n} : 重复n次
{n,} : 重复n次或更多次
[c] : 匹配单个字符c
[a-z] : 匹配a-z小写字母的任意一个

小括号()之间匹配的内容,可以在后面通过$1来引用,$2表示的是前面第二个()里的内容。正则里面容易让人困惑的是\转义特殊字符。

匹配Rewrite的一些实例:


http {
    # 定义image日志格式
    log_format imagelog '[$time_local] ' $image_file ' ' $image_type ' ' $body_bytes_sent ' ' $status;
    # 开启重写日志
    rewrite_log on;
 
    server {
        root /home/www;
 
        location / {
                # 重写规则信息
                error_log logs/rewrite.log notice; 
                # 注意这里要用‘’单引号引起来,避免{}
                rewrite '^/images/([a-z]{2})/([a-z0-9]{5})/(.*)\.(png|jpg|gif)$' /data?file=$3.$4;
                # 注意不能在上面这条规则后面加上“last”参数,否则下面的set指令不会执行
                set $image_file $3;
                set $image_type $4;
        }
 
        location /data {
                # 指定针对图片的日志格式,来分析图片类型和大小
                access_log logs/images.log mian;
                root /data/images;
                # 应用前面定义的变量。判断首先文件在不在,不在再判断目录在不在,如果还不在就跳转到最后一个url里
                try_files /$arg_file /image404.html;
        }
        location = /image404.html {
                # 图片不存在返回特定的信息
                return 404 "image not found\n";
        }
}

对形如/images/ef/uh7b3/test.png的请求,重写到/data?file=test.png,于是匹配到location /data,先看/data/images/test.png文件存不存在,如果存在则正常响应,如果不存在则重写tryfiles到新的image404 location,直接返回404状态码。


if ($http_user_agent ~ MSIE) {
  rewrite ^(.*)$ /msie/$1 break;
} //如果UA包含"MSIE",rewrite请求到/msid/目录下
  
if ($http_cookie ~* "id=([^;]+)(?:;|$)") {
  set $id $1;
 } //如果cookie匹配正则,设置变量$id等于正则引用部分
  
if ($request_method = POST) {
  return 405;
} //如果提交方法为POST,则返回状态405(Method not allowed)。return不能返回301,302
  
if ($slow) {
  limit_rate 10k;
} //限速,$slow可以通过 set 指令设置
  
if (!-f $request_filename){
  break;
  proxy_pass http://127.0.0.1; 
} //如果请求的文件名不存在,则反向代理到localhost 。这里的break也是停止rewrite检查
  
if ($args ~ post=140){
  rewrite ^ http://mysite.com/ permanent;
} //如果query string中包含"post=140",永久重定向到mysite.com


if指令中,可以使用全局变量,这些变量有:


$args: #这个变量等于请求行中的参数,同$query_string

$content_length: 请求头中的Content-length字段。

$content_type: 请求头中的Content-Type字段。

$document_root: 当前请求在root指令中指定的值。

$host: 请求主机头字段,否则为服务器名称。

$http_user_agent: 客户端agent信息

$http_cookie: 客户端cookie信息

$limit_rate: 这个变量可以限制连接速率。

$request_method: 客户端请求的动作,通常为GET或POST。

$remote_addr: 客户端的IP地址。

$remote_port: 客户端的端口。

$remote_user: 已经经过Auth Basic Module验证的用户名。

$request_filename: 当前请求的文件路径,由root或alias指令与URI请求生成。

$scheme: HTTP协议(如http,https)。

$server_protocol: 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。

$server_addr: 服务器地址,在完成一次系统调用后可以确定这个值。

$server_name: 服务器名称。

$server_port: 请求到达服务器的端口号。

$request_uri: 包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。

$uri: 不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。

$document_uri: 与$uri相同。




使用return跳转


我们有时需要在Nginx上使用rewrite做301地址跳转,比如以下规则:

 

rewrite ^ $scheme://www.mysite.com$request_uri permanent;

当访问任意url都会301永久定向到www.mysite.com的url上。这个写法没错,但是因为使用了rewrite的正则匹配,会损耗一部分资源,nginx官网不推荐这样写。我们可以使用return同样可以实现301跳转,简单实用,看实例:


301永久定向到新域名

 

server {

  listen 80;

  listen 443 ssl;

  server_name www.old-name.com old-name.com;

  return 301 $scheme://www.new-name.com;

}

以上代码实现了老域名301跳转到新域名上,假如网站要更换新域名的话使用此方法做301跳转。


不带www的域名301跳转到带www的域名



server {

  listen 80;

  listen 443 ssl;

  server_name mysite.com;

  return 301 $scheme://www.mysite.com$request_uri;

}

http站点301跳转到https站点


server {

  listen 80;

  server_name www.mysite.com;

  return 301 https://www.mysite.com$request_uri;

}

以上就是有关Nginx的rewrite重写与重定向的介绍,觉得有用就请练习多遍,收藏好此文不迷路。也希望大家多多支持脚本之家。


http://www.nginx.cn/


https://blog.csdn.net/xy2204/article/details/47749405

本篇文章来源于 Linux公社网站(www.linuxidc.com)  原文链接:https://www.linuxidc.com/Linux/2014-01/95493.htm