阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Nginx Rewrite正则表达式实例简述

171次阅读
没有评论

共计 3847 个字符,预计需要花费 10 分钟才能阅读完成。

简单整理了下 Nginx 的 URL Rewrite 基本指令,今天谈谈 Nginx Rewrite 的 location 正则表达式。

1.Nginx Rewrite 基本标记(flags)

last 相当于 Apache 里的 [L] 标记,表示完成 rewrite

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

redirect 返回 302 临时重定向 地址栏会显示跳转后的地址

permanent 返回 301 永久重定向 地址栏会显示跳转后的地址

2、正则表达式:

 1)变量名,错误的值包括:空字符串“”,或者任何以 0 开始的字符串。

(2)变量比较可以使用“=”和“!=”(等于和不等于)运算符

(3)正则表达式模式匹配可以使用“~”和“~*”符号

 

(4)~  为区分大小写匹配

(5)~* 为不区分大小写匹配

文件以及目录匹配:

(6)!~ 和!~* 分别为区分大小写不匹配及不区分大小写不匹配

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

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

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

(10)- x 和!- x 用来判断文件是否可执行:

3、案例:

3.1)需要将网站以 https 形式访问

 

server {

    listen  80; 

    server_name www.xxx.com; 

    rewrite ^(.*)$  https://$host$1 permanent; 

小提示:百度是通过 index.html 刷新网页,更巧妙一些。

1
2
3 <html> 
<meta http-equiv=”refresh” content=”0;url=https://baidu.com/”> 
</html>

 3.2)Nginx Redirect 将所有 xxx.com 与 abc.xxx.com 域名全部自跳 www.xxx.com

server {
listen 80;
server_name xxx.com abc.xxx.com;
index index.html index.php;
root /var/InfiNET/web/;
if ($http_host !~ “^www\.xxx\.com$”) {
rewrite ^(.*) [url]http://www.xxx.com$1 redirect;
}
……………………
}

3.3)如果虚拟站点只允许 https 访问时,用 http 访问时 nginx 会报出 497 错误码,用户习惯用 http 访问,后面通过 497 状态码让它自动跳到 443 端口 

 server {
    listen      x.x.x.x:443;  #ssl 端口 
    listen      x.x.x.x:80; 
    server_name  xxx.com; 
    ssl                  on; 
    #指定 PEM 格式的证书文件 
    ssl_certificate      /xxx/xxx.pem; 
    #指定 PEM 格式的私钥文件 
    ssl_certificate_key  /xx/xxx.key; 
     
    #让 http 请求重定向到 https 请求 
    error_page 497  https://$host$uri?$args; 
}

3.4)location 匹配查询资源

eg:

示例 1:

location = / {
# matches the query / only.
# 只匹配 / 查询。
}

匹配任何查询,因为所有请求都已 /  开头。但是正则表达式规则和长的块规则将被优先和查询匹配

示例 2:

location ^~ /images/ {
# matches any query beginning with /images/ and halts searching,
# so regular expressions will not be checked.
# 匹配任何已 /images/ 开头的任何查询并且停止搜索。任何正则表达式将不会被测试。

示例 3:

location ~* \.(gif|jpg|jpeg)$ {
# matches any request ending in gif, jpg, or jpeg. However, all
# requests to the /images/ directory will be handled by
}

3.4.1)匹配任何已 gif、jpg 或 jpeg 结尾的请求。

location ~ .(jsp|jspx|do)?$ {
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}

3.4.2)匹配.php 代理到后端
1
2
3
4
5 location ~ .*.PHP?$ {
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index  index.php;
        include        fcgi.conf;
    }

3.5)Nginx exprires 缓存

location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ {
expires      30d;
}
 
location ~ .*\.(js|css)?$ {
expires      12h;
}
}
# 根据文件类型
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
root /html/web/bbs;
expires 1d;
break;
}
}
# 根据目录类型
location ~ ^/(images|Javascript|js|css|flash|media|static)/ {
root /html/web;
expires 30d;
}

3.6)nginx 防盗链

#Preventing hot linking of images and other file types
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {
valid_referers none blocked server_names *.chinarenservice.com http://localhost baidu.com;
if ($invalid_referer) {
rewrite ^/ [img]http://www.xxx.com/images/default/logo.gif[/img];
# return 403;
}
}

3.7)Nginx 禁止访问下载某类型的文件

3.7.1)Nginx 下禁止访问 *.txt 文件,配置方法如下. 代码:

location ~* \.(txt|doc)$ {
if (-f $request_filename) {
root /html/test;
break;
}
}

3.7.2)禁止访问某个目录

location ~ ^/(tomcat)/ {
deny all;
}

3.7.3)禁止下载以点开头的文件:如 .freeke;.dat;.exe

location ~ /\..+ {
deny all;
}

下面关于 Nginx 的文章您也可能喜欢,不妨参考下:

CentOS 7.2 下编译安装 PHP7.0.10+MySQL5.7.14+Nginx1.10.1  http://www.linuxidc.com/Linux/2016-09/134804.htm

搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

Linux 下编译安装 Nginx 1.8.1 及配置 http://www.linuxidc.com/Linux/2017-02/140495.htm

CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

Nginx 的 500,502,504 错误解决方法 http://www.linuxidc.com/Linux/2015-03/115507.htm

CentOS 7 编译安装 Nginx1.10.2 脚本启动失败解决思路 http://www.linuxidc.com/Linux/2017-01/139794.htm

Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-02/140774.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计3847字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中