Nginx 的 rewrite 规则

本文将介绍通过 nginx 实现 url 重定向,然后介绍重定向的一些简易规则,供大家学习。

nginx 的 ngx_http_rewrite_module 模块提供了 URL 重定向的功能,rewrite 模块支持 URL 重定向,支持 if 条件判断,(if中不支持else),支持通过正则表达式进行 URL 路径匹配,因此该模块需要 pcre 的支持。

rewrite 是实现 URL 重写的关键指令,根据 regex(正则表达式)部分内容,重定向到 replacement,结尾是flag标记。

语法规则

rewrite    <regex>    <replacement>    [flag];
关键字        正则              替代内容         flag标记

rewrite 作用范围为 server, location, if。其中:

  • rewrite: 关键词,指令

  • regex: 正则表达式,匹配 url 路径

  • replacement: 替换内容,重定向的新 url 路径

  • flag: rewrite 支持的 flag 标记。可选择的 flag 如下:

    • last 重写完成后停止对当前 URL 在当前 location 中后续的其它重写操作,而后对新的 URL 启动新一轮重写检查;提前重启新一轮循环,不建议在 location 中使用

    • break 重写完成后停止对当前 URL 在当前 location 中后续的其它重写操作,而后直接跳转至重写规则配置块之后的其它配置;结束循环,建议在 location 中使用

    • redirect 临时重定向,重写完成后以临时重定向方式直接返回重写后生成的新 URL 给客户端,由客户端重新发起请求;使用相对路径,或者 https://  或 https://  开头,状态码:302

    • permanent 重写完成后以永久重定向方式直接返回重写后生成的新 URL 给客户端,由客户端重新发起请求,状态码: 301

注意:如果在同一级配置块中存在多个rewrite规则,那么会自下而下逐个检查;被某条件规则替换完成后,会重新一轮的替换检查;rewrite 隐含有循环机制,但不超过 10 次;如果超过,提示 500 响应码,[flag] 所表示的标志位用于控制此循环机制;如果在同一级配置块中存在多个 rewrite 规则,那么会自下而下逐个检查;被某条件规则替换完成后,会重新一轮的替换检查,如果 flag 标识设置不当会导致死循环匹配,导致出错。

Nginx 防盗链配置

nginx 防盗链由 ngx_http_referer_module 模块支持,用来阻止 Referer 首部无有效值的请求访问,可防止盗链。

作用范围:server, location

语 法:valid_referers  none | blocked | server_names | string…

参 数:

  • none:请求标头中缺少 Referer 字段

  • blocked:Referer 字段出现在请求标头中,但其值已被防火墙或代理服务器删除;这些值是不以 https://  或 https://  开头的字符串

  • server_names:Referer 请求头字段包含一个服务器名称

实例:配置图片防盗链

# 需要防盗链资源的文件类型
location ~* .(gif|jpg|png|swf|flv)$ {
    # 这是可以盗链的域名或IP地址,一般情况可以把google,baidu,sogou,soso,bing,feedsky,zhuaxia,photozero等域名放进来
    valid_referers none blocked  hxstrive.com;
    if ($invalid_referer) {
        # 这样设置能够防盗链,不断地 302 重定向很多次,可能会加重服务器的负担,所以不建议这么做,除非有单独的图片服务器支持
        # 如果有人非法盗链资源,则返回一张防盗链的图片
        # rewrite ^/ https://www.hxstrive.com/picture/images/details-image-1.jpg;
        # 或者返回 403 错误代码
        return 403;
    } 
}

Nginx 重定向实例

将 www.myweb.com/connect 跳转到 connect.myweb.com

rewrite ^/connect$ https://connect.myweb.com permanent;
rewrite ^/connect/(.*)$ https://connect.myweb.com/$1 permanent;

将 connect.myweb.com 301 跳转到 www.myweb.com/connect/

if ($host = "connect.myweb.com"){
    rewrite ^/(.*)$ https://www.myweb.com/connect/$1 permanent;
}

myweb.com 跳转到 www.myweb.com

if ($host != 'www.myweb.com' ) {
    rewrite ^/(.*)$ https://www.myweb.com/$1 permanent;
}

www.myweb.com/category/123.html 跳转为 category/?cd=123

rewrite "/category/(.*).html$" /category/?cd=$1 last;

www.myweb.com/admin/ 下跳转为 www.myweb.com/admin/index.php?s=

if (!-e $request_filename){
    rewrite ^/admin/(.*)$ /admin/index.php?s=/$1 last;
}

在后面添加 /index.php?s=

if (!-e $request_filename){
    rewrite ^/(.*)$ /index.php?s=/$1 last;
}

www.myweb.com/xinwen/123.html  等xinwen下面数字+html的链接跳转为404

rewrite ^/xinwen/([0-9]+).html$ /404.html last;

https://www.myweb.com/news/radaier.html 301跳转 https://www.myweb.com/strategy/

rewrite ^/news/radaier.html https://www.myweb.com/strategy/ permanent;

重定向 链接为404页面

rewrite https://www.myweb.com/123/456.php /404.html last;

禁止htaccess

location ~//.ht {
    deny all;
}

可以禁止/data/下多级目录下.log.txt等请求;

location ~ ^/data {
    deny all;
}

禁止单个文件

location ~ /www/log/123.log {
    deny all;
}

https://www.myweb.com/news/activies/2014-08-26/123.html 跳转为 https://www.myweb.com/news/activies/123.html

rewrite ^/news/activies/2014-([0-9]+)-([0-9]+)/(.*)$ https://www.myweb.com/news/activies/$3 permanent;

nginx 多条件重定向 rewrite

如果需要打开带有play的链接就跳转到play,不过/admin/play这个不能跳转

if ($request_filename ~ (.*)/play){ set $payvar '1';}
if ($request_filename ~ (.*)/admin){ set $payvar '0';}
if ($payvar ~ '1'){
    rewrite ^/ https://play.myweb.com/ break;
}

https://www.myweb.com/?gid=6 跳转为https://www.myweb.com/123.html

if ($request_uri ~ "/?gid=6"){return  https://www.myweb.com/123.html;}
天赋如同自然花木,要用学习来修剪。 —— 培根
0 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号