nginx 配置中的 rewrite

本文详细介绍了Nginx配置中的rewrite规则,包括flag的使用,如last、break、redirect和permanent,以及如何根据不同的条件进行重写。讨论了last与break的区别,并列举了可用于判断的表达式和全局变量,如$args、$request_uri等。同时,提供了多个实际配置示例,涵盖目录转换、重定向、防盗链、过期时间设置、访问控制以及域名转向等应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

语法
rewrite regex replacement flag

flag有如下:
- last
- break 中止 rewrite, 不再继续匹配
- redirect 返回临时重定向的 HTTP 状态302
- permanet 返回永久重定向的 HTTP 状态301

last 和 break 的不同:
break 是终止当前location 的 rewrite 检测, 且不再进行 location 匹配;
last是终止当前location的rewrite检测,但会继续重试location匹配并处理区块中的rewrite规则

  1. 下面是可以用来判断的表达式:
    -f!-f 判断是否存在文件
    -d!-d 判断是否存在目录
    -e!-e 判断是否存在文件或目录
    -x!-x 判断文件是否可执行

  2. 下面是可以用作判断的全局变量

    • $args 等于请求行中的参数
    • $content_length 请求头中的Content-length 字段
    • $content_type 请求头中的Content-Type 字段
    • $document_root 当前请求在root 指令中指定的值
    • $host 请求主机头字段, 否则为服务器名称
    • $http_user_agent 客户端agent 信息
    • $http_cookie 客户端cookie 信息
    • $limit_rate 这个变量可以限制连接速率
    • $request_body_file 客户端请求主题信息的临时文件名
    • $request_method #客户端请求的动作,通常为GET或POST。
    • $remote_addr #客户端的IP地址。
    • $remote_port #客户端的端口。
    • $remote_user #已经经过Auth Basic Module验证的用户名。
    • $request_filename #当前请求的文件路径,由root或alias指令与URI请求生成。
    • $query_string #与$args相同。
    • $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相同

例:

http://localhost:88/test1/test2/test.php

$host: localhost
$server_post: 88
$request_uri: http://localhost:88/test1/test2/test.php
$document_uri: /test1/test2/test.php
$document_root: /usr/share/nginx/html (在nginx.conf里配置的)
$request_filename: /usr/share/nginx/html/test1/test2/test.php (在nginx.conf里配置的)

详例:
多目录转成参数
abc.domain.com/sort/2 => abc.domain.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;
// rewrite ^/\/(\d+)\/(\w+)\/? /$2?id=$1 last;

如果使用IE浏览器, 则重定向到/nginx-ie 目录下

 if ($http_user_agent ~ MSIE) {
     rewrite ^(.*)$ /nginx-ie/$1 break;
 }

目录自动加 /

1.     if (-d $request_filename){
2.     rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
3.     }

禁止htaccess

1.     location ~/\.ht {
2.              deny all;
3.          }

禁止多个目录

1.     location ~ ^/(cron|templates)/ {
2.              deny all;
3.     break;
4.          }

禁止以/data开头的文件

可以禁止/data/下多级目录下.log.txt等请求;
1.     location ~ ^/data {
2.              deny all;
3.          }

禁止单个目录

不能禁止.log.txt能请求
1.     location /searchword/cron/ {
2.              deny all;
3.          }

禁止单个文件

1.     location ~ /data/sql/data.sql {
2.              deny all;
3.          }

给favicon.ico和robots.txt设置过期时间;
这里为favicon.ico为99 天,robots.txt为7天并不记录404错误日志

1.     location ~(favicon.ico) {
2.                      log_not_found off;
3.     expires 99d;
4.     break;
5.          }
6.      
7.          location ~(robots.txt) {
8.                      log_not_found off;
9.     expires 7d;
10. break;
11.      }

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

1.     location ^~ /html/scripts/loadhead_1.js {
2.                      access_log   off;
3.                      root /opt/lampp/htdocs/web;
4.     expires 600;
5.     break;
6.            }

文件反盗链并设置过期时间
这里的return 412 为自定义的http状态码,默认为403,方便找出正确的盗链的请求
“rewrite ^/ http://leech.c1gstudio.com/leech.gif;”显示一张防盗链图片
“access_log off;”不记录访问日志,减轻压力
“expires 3d”所有文件3天的浏览器缓存

1.     location ~* ^.+\.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ {
2.     valid_referers none blocked *.c1gstudio.com *.c1gstudio.net localhost 208.97.167.194;
3.     if ($invalid_referer) {
4.         rewrite ^/ http://leech.c1gstudio.com/leech.gif;
5.         return 412;
6.         break;
7.     }
8.                      access_log   off;
9.                      root /opt/lampp/htdocs/web;
10. expires 3d;
11. break;
12.      }

只充许固定ip访问网站,并加上密码

1.     root  /opt/htdocs/www;
2.     allow   208.97.167.194;
3.     allow   222.33.1.2;
4.     allow   231.152.49.4;
5.     deny    all;
6.     auth_basic "C1G_ADMIN";
7.     auth_basic_user_file htpasswd;

将多级目录下的文件转成一个文件,增强seo效果
/job-123-456-789.html 指向 /job/123/456/789.html

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

将根目录下某个文件夹指向2级目录
/shanghaijob/ 指向 /area/shanghai/
如果你将last改成permanent,那么浏览器地址栏显是 /location/shanghai/

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

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

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

这样/shanghai 也可以访问了,但页面中的相对链接无法使用,
./list_1.html真实地址是/area /shanghia/list_1.html会变成/list_1.html,导至无法访问。
那我加上自动跳转也是不行咯
(-d $request_filename)它有个条件是必需为真实目录,而我的rewrite不是的,所以没有效果

1.     if (-d $request_filename){
2.     rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
3.     }

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

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

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

1.     if (!-e $request_filename) {
2.     proxy_pass http://127.0.0.1;
3.     }

域名跳转

1.     server
2.          {
3.                  listen       80;
4.                  server_name  jump.c1gstudio.com;
5.                  index index.html index.htm index.php;
6.                  root  /opt/lampp/htdocs/www;
7.                  rewrite ^/ http://www.c1gstudio.com/;
8.                  access_log  off;
9.          }

多域名转向

1.     server_name  www.c1gstudio.com www.c1gstudio.net;
2.                  index index.html index.htm index.php;
3.                  root  /opt/lampp/htdocs;
4.     if ($host ~ "c1gstudio\.net") {
5.     rewrite ^(.*) http://www.c1gstudio.com$1 permanent;
6.     }

三级域名跳转

1.     if ($http_host ~* "^(.*)\.i\.c1gstudio\.com$") {
2.     rewrite ^(.*) http://top.yingjiesheng.com$1;
3.     break;
4.     }

域名镜向

1.     server
2.          {
3.                  listen       80;
4.                  server_name  mirror.c1gstudio.com;
5.                  index index.html index.htm index.php;
6.                  root  /opt/lampp/htdocs/www;
7.                  rewrite ^/(.*) http://www.c1gstudio.com/$1 last;
8.                  access_log  off;
9.          }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值