您的位置:首页 > 编程语言 > PHP开发

12-1 12 防盗链 访问控制 php解析 代理

2017-10-31 00:00 471 查看
12.13 Nginx防盗链

12.14 Nginx访问控制

12.15 Nginx解析php相关配置

12.16 Nginx代理

扩展

502问题汇总 http://ask.apelearn.com/question/9109

location优先级 http://blog.lishiming.net/?p=100

12.13 Nginx防盗链

用来禁止来自非本网站的资源访问请求,可以保护服务器不为别的网站请求做响应

[root@axiang-02 ~]# cd /usr/local/nginx/
[root@axiang-02 nginx]# vim conf/vhost/ccc.conf

location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$  匹配
~*表示不区分大小写,^.+表示任意字符
{
expires 7d;
valid_referers none blocked server_names  *.ccc.om ;    //定义白名单,不匹配403
if ($invalid_referer) {
return 403;
}
access_log off;
}

也可以和之前的配置结合起来,多次定义有优先级的问题要注意,参考扩展



测试

[root@axiang-02 vhost]# curl -x127.0.0.1:80 ccc.com/1.gif
asfoawnfnasxojfan
[root@axiang-02 vhost]# curl -e "http://www.baidu.com/1.txt" -x127.0.0.1:80 ccc.com/1.gif
#-e表示指定指定refer 必须是“http://~~格式”

<head><title>403 Forbidden</title></head>  403表示防盗链成功

12.14 Nginx访问控制

如果发现有来自某个固定IP,其访问请求不太像人类行为,可以通过访问控制拒绝为之服务
访问控制还可以创建只允许内网IP访问的网站资源

需求:访问/admin/目录的请求,只允许某几个IP访问,配置如下:

目录访问控制

location /kongzhi/
{
allow 127.0.0.1;
deny all;
}

mkdir kongzhi
vim kongzhi/1.php
echo “test,test”>/data/wwwroot/ccc.com/kongzhi/2.html
-t && -s reload
curl -x127.0.0.1:80 ccc.com/kongzhi/2.html -I
curl -x192.168.83.138:80 ccc.com/kongzhi/2.html -I
HTTP/1.1 403 Forbidden
[root@axiang-02 nginx]# curl -x127.0.0.1 ccc.com/kongzhi/2.html -I
curl: (7) Failed connect to 127.0.0.1:1080; 拒绝连接  //没有指定端口也不行
[root@axiang-02 nginx]# curl -x127.0.0.1:80 ccc.com/kongzhi/2.html -I
HTTP/1.1 200 OK

页面访问控制

可以匹配正则,限制含有某些字符的目录下的php文件。

根据user_agent限制

server
{
listen 80;
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/aaa.com;

location ~ .*(upload|image)/.*\.php$ /    //表示匹配包含upload或image字符的目录下的php
{
deny all;
}
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')    //表示匹配agent为Spider/3.0|YoudaoBot|Tomato的拒绝访问
{
return 403;
}
}


deny all和return 403效果一样



匹配符号~ *可以不区分大小写

12.15 Nginx解析php相关配置

之前的主配置文件中,删除service的部分含有php解析的代码。改为include后,需要重新添加到各个虚拟主机

[root@axiang-02 php-fpm]# cd /usr/local/nginx/conf/vhost/
[root@axiang-02 vhost]# ls
aaa.conf  bbb.conf  ccc.conf  ld.conf  proxy.conf  ssl.conf
[root@axiang-02 vhost]# vi aaa.conf
[root@axiang-02 vhost]# cat aaa.conf
server
{
listen 80;
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/aaa.com;

location ~ .*(upload|image)/.*\.php$
{
allow 127.0.0.1;
allow 192.168.83.1;
deny all;
}
if ($http_user_agent ~* 'Spider/3.0|YoudaoBot|Tomato')
{
return 403;
}
location ~ \.php$  //php解析核心配置
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;   //这里要指定正确
#fastcgi_pass 127.0.0.1:9000;    //也可以监听ip端口。不用来与外网交互,只在本机监听进程
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/aaa.com$fastcgi_script_name;
}
}

测试

[root@axiang-02 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@axiang-02 vhost]# curl -x127.0.0.1:80 aaa.com/aaa/aaa.php
this is aaa.com
[root@axiang-02 vhost]# curl -x127.0.0.1:80 aaa.com/reupload/aaa.php
<?php echo "this is aaa.com"; ?>    //做了访问控制的目录即使通过访问请求,也仍然不能解析php


sock监听错误

[root@axiang-02 vhost]# vim aaa.conf



fcgi故意写错为cgi再测试

[root@axiang-02 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@axiang-02 vhost]# curl -x127.0.0.1:80 aaa.com/aaa/aaa.php

<head><title>502 Bad Gateway</title></head>  出现502坏访问网关

查看错误日志(主配置文件里有定义位置,注意是nginx_error.log 把级别改为debug更详细)



[root@axiang-02 vhost]# vi /usr/local/nginx/conf/nginx.conf
[root@axiang-02 vhost]# tail /usr/local/nginx/logs/nginx_error.log
2017/08/09 17:40:37 [crit] 2966#0: *31 connect() to unix:/tmp/php-cgi.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: aaa.com, request: "GET HTTP://aaa.com/aaa/aaa.php HTTP/1.1", upstream: "fastcgi://unix:/tmp/php-cgi.sock:", host: "aaa.com"

看到提示,php-cgi.sock不存在,说明nginx与php-fpm需要指向正确的 sock文件进行交互

[root@axiang-02 vhost]# ls /usr/local/php-fpm/etc/php-fpm.d/
axiang.conf  www.conf
[root@axiang-02 vhost]# cat !$www.conf
cat /usr/local/php-fpm/etc/php-fpm.d/www.conf

[www]
listen = /tmp/php-fcgi.sock
#listen = 127.0.0.1:9000
listen.mode = 666


IP端口监听

改为监听IP和端口

[root@axiang-02 vhost]# vim /usr/local/php-fpm/etc/php-fpm.d/www.conf

[www]
#listen = /tmp/php-fcgi.sock
listen = 127.0.0.1:9000
listen.mode = 666

[root@axiang-02 vhost]# /usr/local/php-fpm/sbin/php-fpm -t
[root@axiang-02 vhost]# /etc/init.d/php-fpm reload
[root@axiang-02 vhost]# netstat -lntp  //查看9000端口

tcp0  0 127.0.0.1:9000  0.0.0.0:*   LISTEN  3018/php-fpm: maste

[root@axiang-02 vhost]# vi aaa.conf

location ~ \.php$
{
include fastcgi_params;
#fastcgi_pass unix:/tmp/php-fcgi.sock;
#虚拟主机配置文件中定义监听方式,sock和ip:port两种
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/aaa.com$fastcgi_script_name;
}




[root@axiang-02 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@axiang-02 vhost]# curl -x127.0.0.1:80 aaa.com/aaa/aaa.php
this is aaa.com


注意解析的根目录参数

/data/wwwroot/aaa.com$fastcgi_script_name;

注意nginx对接php-fpm监听方式

vim /usr/local/php-fpm/etc/php-fpm.d/www.conf

如果有优先级更高的php匹配,则
location ~ \.php$
中的参数不生效

比如
location ~ .*(upload|image)/.*\.php$
优先级大于
location ~ \.php$
,所以curl -x127.0.0.1:80 aaa.com/reupload/aaa.php出现php不解析<?php echo "this is aaa.com"; ?>

12.16 Nginx代理



当两边的服务器不能直接访问,或者访问速度很慢,可以通过优秀的代理服务器作为中间的访问跳板

[root@axiang-02 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;
}
}

[root@axiang-02 vhost]# /usr/local/nginx/sbin/nginx -t
[root@axiang-02 ~]# /usr/local/nginx/sbin/nginx -s reload
[root@axiang-02 ~]# curl -x127.0.0.1:80 ask.apelearn.com/robots.txt
#
# robots.txt for MiWen
#

User-agent: *

Disallow: /?/admin/
Disallow: /?/people/
Disallow: /?/question/
Disallow: /account/
Disallow: /app/
Disallow: /cache/
Disallow: /install/
Disallow: /models/
...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐