目录
一、Nginx防盗链
二、Nginx访问控制
三、Nginx解析php相关配置
四、Nginx代理
扩展
502问题汇总 http://ask.apelearn.com/question/9109
location优先级 http://blog.lishiming.net/?p=100
Nginx防盗链
vi /usr/local/nginx/conf/vhost/test.com.conf
配置如下,可以和上面的配置结合起来
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;
}
-t && -reload
测试
curl -e "http://www.baidu.com/1.txt" -x127.0.0.1:80 -I test.com/1.gif
Nginx访问控制
需求:访问/admin/目录的请求,只允许某几个IP访问,配置如下:
vi /usr/local/nginx/conf/vhost/test.com.conf
location /admin/
{
allow 192.168.78.128;
allow 127.0.0.1;
deny all;
}
只要匹配,就不再做其他匹配
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.31.129:80 test.com/admin/1.html -I
查看log
可以匹配正则
location ~ .*(upload|image)/.*\.php$
{
deny all;
}
(禁止解析PHP)
测试
mkdir /data/wwwroot/test.com/upload
echo "1111" > /data/wwwroot/test.com/upload/1.php
curl -x127.0.0.1:80 test.com/upload/1.php
根据user_agent限制
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato') ~*忽略大小写
{
return 403;
}
deny all和return 403效果一样
做一个隐藏的网站,禁掉蜘蛛
测试
curl -A "Tomatoasdlkfjsdlf" -x127.0.0.1:80 test.com/upload/1.txt -I
Nginx解析php相关配置
配置如下:
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
}
fastcgi_pass 用来指定php-fpm监听的地址或者socket
修改 fastcgi_pass unix:/tmp/php-fcgi.sock; 为fastcgi_pass unix:/tmp/php-cgi.sock
curl -x127.0.0.1:80 test.com/3.php
出现502错误
查看nginx_error log
tail /usr/local/nginx/logs/nginx_error.log
查看php-fpm配置文件
cat /usr/local/php-fpm/etc/php-fpm.conf
nginx和php-fpm要对应
SCRIPT_FILENAME /data/wwwroot/test.com 要和root对应起来
listen.mode = 666 不配置 默认440
重启php-fpm
/etc/init.d/php-fpm reload or /etc/init.d/php-fpm restart
php-rpm进程耗尽也会出现502
Nginx代理
cd /usr/local/nginx/conf/vhost
vim proxy.conf //加入如下内容
server
{
listen 80;
server_name ask.apelearn.com;
location /
{
proxy_pass http://223.94.95.10/;
proxy_set_header Host $host; $host就是sever name
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}