一、Nginx防盗链

1、编辑虚拟主机配置文件

server
{
listen 80;
server_name test.com test1.com test2.com test3.com;
index index.html index.htm index.php;
root /data/testnginx/test.com;
if ($host != 'test.com' ) {
rewrite ^/(.*)$ http://test.com/$1 permanent;
}
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;
}

access_log /tmp/nginx.log combined_realip;
}
2、测试,如图防盗链配置成功

二、访问控制

1、编辑配置文件,在原来的基础上增加如下内容:

location /admin/
{
allow 127.0.0.1;
allow 192.168.134.130;
deny all;
}

2、测试

可以发现127.0.0.1和192.168.134.141都可以访问,从日志看来源IP是127.0.0.1和192.168.134.130,是被允许的IP,

下图可以看出192.168.150.128访问时为403,由日志可知来源IP是不被允许访问的。

3、正则匹配限制

1)编辑配置文件

location ~.*(aaa|image)/.*\.php$
{
deny all;
}

2)测试

由测试可知凡是.php的文件都是被拒绝访问的。

4、user_agent限制访问

1)编辑配置文件

if ($http_user_agent ~ 'Spider/3.0|Youdao|Tomato')
{
return 403;
}

2)测试

由测试可知,当user_agent匹配到Tomato字符时就被拒绝访问,但是小写的是可以访问的。

要想大小写都被拒绝,修改配置文件,在原配置文件基础上加*。

}
if ($http_user_agent ~* 'Spider/3.0|Youdao|Tomato')
{
return 403;
}

3)再次测试

三、解析PHP

1、编辑配置文件

location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/testnginx/test.com$fastcgi_script_name;
}

2、测试

1)先不加载,发现无法解析,返回源码。

2)加载后可以解析

3、502情况

1)配置文件写错路径

fastcgi_pass unix:/tmp/php-fcgi.sock;误写为fastcgi_pass unix:/tmp/php-fgi.sock;

根据错误log提示没有发现该文件。

因为我们在php-fpm.conf中定义的路径是fastcgi_pass unix:/tmp/php-fcgi.sock

2)php-fpm.conf中监听IP和端口,而test.com.conf中监听socket.不匹配导致。

 

重启nginx ,查看是否有9000端口,测试发现报502

需要修改虚拟主机配置文件:

fastcgi_pass unix:/tmp/php-fcgi.sock;为 fastcgi_pass 127.0.0.1:9000;然后可以解析。

3)php-fpm.conf中没有定义 listen.mode = 666 或定义的权限不是666,也会报502。

 

因为当listen.mode = 666没有定义时,我们可以发现sock的权限是440,只有root和root所属组才能读写,其他用户不能读写,而sockt是以nobody身份读写的。

所以php-fpm.conf中一定要定义listen.mode = 666。

四、Nginx代理

1、编辑配置文件:  vim /usr/local/nginx/conf/vhost/proxy.conf

server
{
listen 80;
server_name www.baidu.com;

location /
{
proxy_pass http://61.135.169.121/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $Proxy_add_x_forwarded_for;
}
}
2、测试

posted on 2018-03-15 23:42  天梭  阅读(137)  评论(0编辑  收藏  举报