Nginx网站架构实战——08、nginx Rewrite语法详解

网友投稿 568 2023-03-09

本站部分文章、图片属于网络上可搜索到的公开信息,均用于学习和交流用途,不能代表睿象云的观点、立场或意见。我们接受网民的监督,如发现任何违法内容或侵犯了您的权益,请第一时间联系小编邮箱jiasou666@gmail.com 处理。

Nginx网站架构实战——08、nginx Rewrite语法详解

前言:

Nginx网站架构实战——01、Nginx介绍及编译安装:传送门

Nginx网站架构实战——02、Nginx信号量:传送门

Nginx网站架构实战——03、nginx虚拟主机配置:传送门

Nginx网站架构实战——04、nginx日志管理:传送门

Nginx网站架构实战——05、nginx定时任务完成日志切割:传送门

Nginx网站架构实战——06、Location详解之精准匹配:传送门

Nginx网站架构实战——07、Location之正则匹配:传送门

常用的命令

    if (条件) {} 设定条件,再进行重写    set #设置变量    return  #返回状态码    break   #跳出rewrite    rewrite #重写

if (条件) {} 设定条件,再进行重写 set #设置变量 return #返回状态码 break #跳出rewrite rewrite #重写

if语法格式

    if 空格 (条件){        重写模式    }    条件又怎么写?    1. “=”来判断相等, 用于字符串比较    2. “~” 用正则来匹配(此处的正则区分大小写)    ~* 不区分大小写的正则    3. -f -d -e来判断是否为文件,为目录,是否存在.    [root@tiejiang ~]# cd /usr/local/nginx/    [root@tiejiang nginx]# vim html/test-if.html        <html>            test if and reutrn;        </html>    [root@tiejiang nginx]# tail -n 1 logs/access.log         192.168.0.102 - - [15/May/2018:03:53:36 +0800] "GET /test-if.html HTTP/1.1" 200 35 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0"

if 空格 (条件){ 重写模式 } 条件又怎么写? 1. “=”来判断相等, 用于字符串比较 2. “~” 用正则来匹配(此处的正则区分大小写) ~* 不区分大小写的正则 3. -f -d -e来判断是否为文件,为目录,是否存在. [root@ ~]# cd /usr/local/nginx/ [root@ nginx]# vim html/test-if.html <html> test if and reutrn; </html> [root@ nginx]# tail -n 1 logs/access.log 192.168.0.102 - - [15/May/2018:03:53:36 +0800] "GET /test-if.html HTTP/1.1" 200 35 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0"

拒绝一个指定IP访问这个test-if.html页面

    [root@tiejiang ~]# cd /usr/local/nginx/    [root@tiejiang nginx]# vim conf/nginx.conf            location / {                if ($remote_addr = 192.168.0.102) {                return 403;            }                root   html;                index  index.html index.htm;            }    [root@tiejiang nginx]# ./sbin/nginx -s reload

[root@ ~]# cd /usr/local/nginx/ [root@ nginx]# vim conf/nginx.conf location / { if ($remote_addr = 192.168.0.102) { return 403; } root html; index index.html index.htm; } [root@ nginx]# ./sbin/nginx -s reload

拒绝IE浏览器打开任何页面(msie是ie的标识)

用rewrite设置404页面

首先打开一个存在的页面

然后再打开一个不存在的页面,现在展示出来我们指定的404页面。

上一篇:Nginx网站架构实战——09、编译PHP并与nginx整合
下一篇:网络告警关联分析方法(网络告警信息)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~