[toc]
12.13 Nginx防盗链
- 配置如下,可以和上面的配置结合起来
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$
{
expires 7d;
valid_referers none blocked server_names *.test.com ;
if ($invalid_referer) {
return 403;
}//定义白名单
access_log off;
}
- curl -e "http://www.test.com/1.txt" -x127.0.0.1:80 -I test.com/1.gif
12.14 Nginx访问控制
- 需求:访问/admin/目录的请求,只允许某几个IP访问,配置如下:
location /admin/
{
allow 192.168.133.1;
allow 127.0.0.1;
deny all;
}//先allow 再deny;从上到下开始匹配
可以匹配正则
location ~ .*(upload|image)/.*\.php$
{
deny all;
}
- mkdir /data/wwwroot/test.com/upload
- echo "1111" > /data/wwwroot/test.com/upload/1.php
根据user_agent限制//~* 可以忽略大小写,即匹配后边加*
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
{
return 403;
}//deny all和return 403效果一样
//禁止蜘蛛搜到自己
- curl -A "Tomato" -x127.0.0.1:80 test.com/upload/1.txt
- curl -x127.0.0.1:80 test.com/upload/1.php
- mkdir /data/wwwroot/test.com/admin/
- echo “test,test”>/data/wwwroot/test.com/admin/1.html
- -t && -s reload
- curl -x127.0.0.1:80 test.com/admin/1.html -I
- curl -x192.168.133.130:80 test.com/admin/1.html -I
12.15 Nginx解析php相关配置
- 配置如下:
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;//要和php的listen一样,php也可以是IP端口形式
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
}
12.16 Nginx代理
- cd /usr/local/nginx/conf/vhost
vim proxy.conf //加入如下内容
server
{
listen 80;
server_name ask.apelearn.com;
location /
{
proxy_pass http://121.201.9.155/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
扩展
- 502问题汇总 http://ask.apelearn.com/question/9109
- location优先级 http://blog.lishiming.net/?p=100