nginx访问控制、rewrite应用 一、访问控制 在这里依然还是以default2.conf虚拟主机为例,配置文件位置default2.conf 1、允许某个ip访问 ,需要在default2.conf配置配文件中添加,具体如下图:
规则如下: allow 127.0.0.1; allow 192.168.21.97; deny all; 只允许127.0.0.1和192.168.21.97来访问,其他的全部拒绝
退出保存 1)检查配置文件 2)重置配置文件 3)测试 允许ip测试 [iyunv@mysql ~]# curl -x192.168.122.10:80 http://www.guhantai.cn/1.jpg-I
HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Mon, 22 Jun
2015 15:21:05 GMT
Content-Type:
image/jpeg
Content-Length: 0
Last-Modified: Mon,
22 Jun 2015 01:16:22 GMT
Connection:
keep-alive
ETag:
"558761e6-0"
Expires: Thu, 02 Jul
2015 15:21:05 GMT
Cache-Control:
max-age=864000
Accept-Ranges: bytes
禁止本地IP地址访问
规则如下: deny 127.0.0.1; allow all;
1)保存退出 2)检查配置文件 /usr/local/nginx/sbin/nginx -t 3)重置配置文件 [iyunv@mysql ~]# /usr/local/nginx/sbin/nginx -s reload 4)测试 使用127.0.0.1来个访问 [iyunv@mysql ~]# curl -x127.0.0.1:80 http://www.hello.com/1.jpg
<html>
<head><title>403 Forbidden</title></head> #提示被禁止
<body
bgcolor="white">
<center><h1>403
Forbidden</h1></center>
<hr><center>nginx/1.6.2</center>
</body>
</html>
[iyunv@mysql ~]# curl -x192.168.122.10:80 http://www.hello.com/1.jpg-I
HTTP/1.1 200 OK #访问是OK的
3、某个目录下限制IP访问,这个的作用主要就是是针对一些特定的目录来限制访问,依然以default2.conf虚拟主机配置为例 只允许192.168.61.0/24这个网段对w目录访问,其他的全部拒绝,配置文件位置:/usr/local/nginx/conf/vhosts/default2.conf 如图:
规则写法如下: location /w/ { allow 192.168.61.0/24; #这里的IP段就是允许的IP段 deny all; location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name; } } 一定要加上location php,不然是不会去解析php的
退出保存 检查配置文件 [iyunv@mysql w]# /usr/local/nginx/sbin/nginx -t
重启nginx /etc/init.d/nginx restart 4、在nginx的配置文件nginx.conf中加入: include deny.ip;
重启一下nginx的服务:/usr/local/nginx/sbin/nginx reload就可以生效了。
deny.ip 的格式中也可以用deny all; 如果你想实现这样的应用,除了几个IP外,其他全部拒绝, 那需要你在deny.ip 中这样写 allow 1.1.1.1; allow 1.1.1.2; deny all;
5、有时候会根据目录来限制php解析,这里依然还是以/usr/local/nginx/conf/vhosts/default2.conf 虚拟主机为例的
location ~ .*(diy|template|attachments|forumdata|attachment|image)/.*\.php$ #括号里面的表示或者的意思,只要是括号里面的,不管哪一个都禁止 { deny all; }
这个的意思就是说访问这些目录后的php全部不解析
添加在这个位置,如下图:
退出保存
检查配置文件 /usr/local/nginx/sbin/nginx -t
重启nginx /etc/init.d/nginx restart 测试访问www.hello.com后面带image目录下后缀为.php的文件时,提示拒绝访问 [iyunv@mysql ~]# curl -x192.168.21.97:80 http://www.hello.com/wer/image/23.php <html> <head><title>403 Forbidden</title></head> <body bgcolor="white"> <center><h1>403 Forbidden</h1></center> <hr><center>nginx/1.6.2</center> </body> </html>
6、使用 user_agent 控制客户端访问 ,就是可以限制某些浏览器来访问,只需要将浏览器的标示写入到规则里面就可以,都是在nginx虚拟主机配置文件中设置的 代码如下: if ($http_user_agent ~ 'bingbot/2.0|MJ12bot/v1.4.2|Spider/3.0|YoudaoBot|Tomato|Gecko/20100315'){ return 403; }
location ~ \.php$ { include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name; }
二、nginx的rewrite应用 1、rewrite作用 重写URL,或者修改字符串。需要注意的是重写URL只对相对路径有效,如果想要对主机名,要使用if语句 2、伪静态rewrite规则,一下这规则可以作为一个标准的模板,需要的时候直接复制粘贴就可以,当然这个也是写在虚nginx虚拟主机的配置文件中的
伪静态rewrite规则 rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 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 ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/index.php?action=$2&value=$3 last;
|