1. 程式人生 > >nginx防盜鏈、訪問控制、PHP解析、服務器代理

nginx防盜鏈、訪問控制、PHP解析、服務器代理

nginx防盜鏈、訪問控制、PHP解析、

12.13 Nginx防盜鏈

因為該配置也使用location板塊,所以本節可結合日誌管理一起配置:

[root@centos-01linux ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
……
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 ;
#定義referer白名單
if ($invalid_referer) {
return 403;

#if函數的意思是:如果不是白名單內的域名,返回值:403
}
access_log off;
}
……

[root@centos-01linux ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos-01linux ~]# /usr/local/nginx/sbin/nginx -s reload

說明: “location ~ ^.+”在此0“

”的作用是後面匹配的內容不區分大小寫。

檢測

[root@centos-01linux ~]# curl -e "http://www.baidu.com/1.txt" -x127.0.0.1:80 -I test.com/baidu.png
HTTP/1.1 403 Forbidden
Server: nginx/1.12.1
Date: Mon, 14 Aug 2017 06:22:36 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
即,使用非白名單內的referer進行訪問,被拒絕!!!

12.14 Nginx訪問控制

需求:訪問/admin/目錄的請求,只允許幾個指定IP通過,配置如下:

[root@centos-01linux ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
……
location /admin/
{
allow 192.168.8.132;
allow 127.0.0.1;
deny all;
#設置IP白名單
}
……

[root@centos-01linux ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos-01linux ~]# /usr/local/nginx/sbin/nginx -s reload
創建上面指定的目錄:

[root@centos-01linux ~]# mkdir /data/wwwroot/test.com/admin

[root@centos-01linux ~]# echo “test,test”>/data/wwwroot/test.com/admin/1.html
測試

[root@centos-01linux ~]# curl -x127.0.0.1:80 test.com/admin/1.html
“test,test”

[root@centos-01linux ~]# curl -x192.168.8.132:80 test.com/admin/1.html
“test,test”
訪問控制——正則匹配

location ~ .(abc|image)/..php$
{
deny all;
}
訪問控制——user_agent限制

if ($http_user_agent ~ ‘Spider/3.0|YoudaoBot|Tomato‘)
{
return 403;
}
說明: deny all和return 403效果一樣

12.15 Nginx解析PHP相關配置

核心配置:

vim /usr/local/nginx/conf/vhost/test.com.conf
……
location ~ .php$
{
include fastcgi_params;
#fastcgi_pass 127.0.0.1:9000
fastcgi_pass unix:/tmp/php-fcgi.sock;
##fastcgi_pass兩種監聽格式,但是要保證Nginx和php-fpm中格式一致
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
}
……
註: 在此註意兩點,fastcgi_pass有兩種格式,但是無論使用哪種格式都有保證Nginx和php-fpm中格式一致,否則會報錯502;fastcgi _param SCRIPT _FILENAME所在行的路徑要和root路徑一致!

12.16 Nginx代理

Nginx代理是一種反向代理。反向代理(Reverse Proxy)方式是指以代理服務器來接受Internet上的連接請求,然後將請求轉發給內部網絡上的服務器;並將從服務器上得到的結果返回給Internet上請求連接的客戶端,此時代理服務器對外就表現為一個服務器。

工作原理

Nginx代理是在一臺代理服務器中自定義一個域名,該域名指向一個IP,然後將用戶的請求通過這臺代理服務器訪問指定的IP所對應的web服務器。

graph LR
用戶-->代理服務器
代理服務器-->用戶
代理服務器-->web服務器
web服務器-->代理服務器
進入虛擬主機目錄:

[root@centos-01linux ~]# cd /usr/local/nginx/conf/vhost/
創建代理服務器

[root@centos-01linux vhost]# vim proxy.conf
server
{
listen 80;
server_name ask.apelearn.com;
#定義域名(一般和被代理ip的域名保持一致)
location /
{
proxy_pass http://121.201.9.155/;
#指定被代理(被訪問)的IP(web服務器IP)
proxy_set_header Host $host;
#$host指的是代理服務器的servername(也是被代理IP的域名)
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
說明: 因為該虛擬主機只用作代理服務器,不需要訪問本地文件,所以不需要設置根目錄。

檢測

設置代理前

[root@centos-01linux vhost]# curl -x127.0.0.1:80 ask.apelearn.com/robots.txt
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>
設置代理後

[root@centos-01linux vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos-01linux vhost]# /usr/local/nginx/sbin/nginx -s reload

[root@centos-01linux vhost]# 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/

nginx防盜鏈、訪問控制、PHP解析、服務器代理