2019獨角獸企業重金招聘Python工程師標準>>>
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 ; //定義白名單,不匹配403if ($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.conflocation ~ \.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/
...