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/ ...