12.13 Nginx防盗链php
12.14 Nginx访问控制html
12.15 Nginx解析php相关配置nginx
12.16 Nginx代理vim
扩展服务器
用来禁止来自非本网站的资源访问请求,能够保护服务器不为别的网站请求作响应app
[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; }
也能够和以前的配置结合起来,屡次定义有优先级的问题要注意,参考扩展curl
测试tcp
[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表示防盗链成功
若是发现有来自某个固定IP,其访问请求不太像人类行为,能够经过访问控制拒绝为之服务 访问控制还能够建立只容许内网IP访问的网站资源ide
需求:访问/admin/目录的请求,只容许某几个IP访问,配置以下:php-fpm
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
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; } }
以前的主配置文件中,删除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和端口
[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
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"; ?>当两边的服务器不能直接访问,或者访问速度很慢,能够经过优秀的代理服务器做为中间的访问跳板
[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/ ...