一、访问控制
限制只让某个ip访问:
allow 192.168.1.100;
deny all;
限制只有本地地址可以访问,白名单;
allow 127.0.0.1;
deny all;
拒绝本地访问,黑名单:
deny 127.0.0.1;
allow all;
deny all 直接拒绝所有,下面的allow就不生效了。
1
2
3
4
5
6
7
8
9
10
|
[root@localhost vhosts]
# vi default.conf
server
{
listen 80 default_server;
server_name localhost;
index index.html index.htm index.php;
root
/usr/local/nginx/html
;
deny all;
allow 2.2.2.2;
}
|
1
2
3
4
5
6
7
|
[root@localhost vhosts]
# curl -x127.0.0.1:80 192.168.20.30/index.html -I
HTTP
/1
.1 403 Forbidden
Server: nginx
/1
.6.2
Date: Fri, 15 May 2015 08:46:05 GMT
Content-Type: text
/html
Content-Length: 168
Connection: keep-alive
|
禁止某个IP或者IP段访问站点的设置方法:
1、首先建立下面的配置文件放在nginx的conf目录下面,命名为deny.ip
1
2
3
4
|
# cat /usr/local/nginx/conf/deny.ip
deny 192.168.20.10;
deny 192.168.20.11;
deny 192.168.10.0
/24
;
|
2、在nginx的配置文件nginx.conf中加入:include deny.ip;
3、重启一下nginx的服务:/usr/local/nginx/sbin/nginx -s reload 就可以生效了。
deny.ip 的格式中也可以用deny all; 如果你想实现这样的应用,除了几个IP外,其他全部拒绝,
allow 1.1.1.1;
allow 1.1.1.2;
deny all;
针对目录限制php解析:
location ~ .*(diy|template|attachments|forumdata|attachment|image/.*\.php$ {
deny all;
}
根据 user_agent 控制客户端访问
location / {
if ($http_user_agent ~ 'bingbot/2.0|MJ12bot/v1.4.2|Spider/3.0|YoudaoBot|Tomato|Gecko/20100315'){
return 403;
}
}
实验:限定IE7.0 和 curl 不能访问;IE8.0可以正常打开;
location / {
if ($http_user_agent ~ 'MSIE 7.0|curl'){
return 403;
}
}
curl -A 代表浏览器标识agent;模拟浏览器标识;测试包含bingbot/2.0,MSIE 7.0,curl的返回值为403;
1
2
3
4
5
6
7
|
[root@localhost vhosts]
# curl -x127.0.0.1:80 -A "aabingbot/2.0ss" www.111.com -I
HTTP
/1
.1 403 Forbidden
Server: nginx
/1
.6.2
Date: Fri, 15 May 2015 21:09:35 GMT
Content-Type: text
/html
Content-Length: 168
Connection: keep-alive
|
1
2
3
4
5
6
7
|
[root@localhost vhosts]
# curl -x127.0.0.1:80 -A "MSIE 7.0aa" www.111.com -I
HTTP
/1
.1 403 Forbidden
Server: nginx
/1
.6.2
Date: Fri, 15 May 2015 21:13:50 GMT
Content-Type: text
/html
Content-Length: 570
Connection: keep-alive
|
1
2
3
4
5
6
7
|
[root@localhost vhosts]
# curl -x127.0.0.1:80 192.168.20.30/index.html -I
HTTP
/1
.1 403 Forbidden
Server: nginx
/1
.6.2
Date: Fri, 15 May 2015 09:15:27 GMT
Content-Type: text
/html
Content-Length: 168
Connection: keep-alive
|
二、nginx的rewrite应用
现在有这样的的需求,访问 www.abc.com 请求到 www.abc.com/abc/
在nginx虚拟主机配置文件中加入 :
if ($document_uri !~ 'abc')
{
rewrite ^/(.*)$ http://www.abc.com/abc/$1 permanent;
}
而不是单独加一句rewrite ^/(.*)$ http://www.abc.com/abc/$1 permanent;
如果只加rewrite 规则,而不限定条件,那么会造成死循环。
会访问到http://www.abc.com/abc/abc/abc/abc/....
实验测试,只加一行rewrite规则,redirect 302 临时重定向;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[root@localhost vhosts]
# cat 111.conf
server
{
listen 80;
server_name www.aaa.com aaa.com;
index index.html index.htm index.php;
root
/data/www2
;
rewrite ^/(.*)$
/aaa/
$1 redirect;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:
/tmp/php-fcgi-www2
.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
/data/www2
$fastcgi_script_name;
}
}
|
使用curl测试跳转到http://www.aaa.com/aaa/asdfasdfa;
1
2
3
4
5
6
7
8
9
|
[root@localhost vhosts]
# curl -x127.0.0.1:80 www.aaa.com/asdfasdfa -I
HTTP
/1
.1 302 Moved Temporarily
Server: nginx
/1
.6.2
Date: Fri, 15 May 2015 09:54:10 GMT
Content-Type: text
/html
Content-Length: 160
Location:
http:
//www
.aaa.com
/aaa/asdfasdfa
Connection: keep-alive
|
浏览器输入http://www.aaa.com/asdfasdfa 出现死循环网址;
加入if判断,域名不匹配aaa的时候跳转到aaa地址下;浏览器访问跳转正确,出现404错误是我们做实验没有这个目录;
if ($document_uri !~ 'aaa')
{
rewrite ^/(.*)$ /aaa/$1 redirect;
三、nginx代理配置
/conf/vhosts/目录下,新建一个proxy.conf 写入下面的内容:
1
2
3
4
5
6
7
8
9
10
11
|
[root@localhost vhosts]
# cat proxy.conf
server {
listen 80;
server_name
location / {
proxy_pass http:
//180
.97.33.108/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
|
代理baidu.com,proxy_pass填写baidu的ip地址;
#curl -x192.168.20.30:80 www.baidu.com 使用本地解析baidu.com 就可以访问百度的主页;
dig命令查看baidu的别名和对应的ip地址;
#dig www.baidu.com
;; ANSWER SECTION:
www.baidu.com. 1065 IN CNAME www.a.shifen.com.
www.a.shifen.com. 183 IN A 180.97.33.108
www.a.shifen.com. 183 IN A 180.97.33.107
如果代理的机器有多台,可以实现负载均衡。bbb为自定义的内容;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
upstream bbb
{
server 180.97.33.108:80;
server 180.97.33.107:80;
}
server {
listen 80;
server_name
location / {
proxy_pass http:
//bbb/
;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
|
本文转自 模范生 51CTO博客,原文链接:http://blog.51cto.com/mofansheng/1652905,如需转载请自行联系原作者