92.Nginx配置:防盜鏈、訪問控制、解析PHP以及代理

一、Nginx防盜鏈

防盜鏈是指一個網站的資源(圖片或附件)未經允許在其它網站提供瀏覽和下載,尤其熱門資源的盜鏈,對網站帶寬的消耗非常大,設置防盜鏈以節省資源。

1、修改虛擬主機配置文件

[root@sdwaqw vhost]# vim linuxtest.conf

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

   location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$
   {
    expires 7d;
    valid_referers none blocked server_names  *.linuxtest.com ;#   定義referer白名單   
    if ($invalid_referer) {
        return 403;#    if函數的意思是:如果不是白名單內的域名,返回值:403
    }
#   location /#     { #       auth_basic         "Auth";#       auth_basic_user_file /usr/local/nginx/conf/htpasswd;#     }
   access_log /tmp/linuxtest.log combined_realip;
}#使用access_log指定日誌存儲路徑和使用的日誌格式名字

2、測試

[root@sdwaqw vhost]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@sdwaqw vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@sdwaqw vhost]# echo "這是防盜鏈jpg測試!" > /data/wwwroot/linuxtest/test.jpg
[root@sdwaqw vhost]# curl -x127.0.0.1:80 linuxtest.com/test.jpg -I
HTTP/1.1 200 OKServer: nginx/1.12.2Date: Thu, 15 Mar 2018 14:33:07 GMTContent-Type: image/jpegContent-Length: 28Last-Modified: Thu, 15 Mar 2018 14:32:45 GMTConnection: keep-aliveETag: "5aaa840d-1c"Expires: Thu, 22 Mar 2018 14:33:07 GMTCache-Control: max-age=604800Accept-Ranges: bytes

[root@sdwaqw vhost]# curl -x127.0.0.1:80 -e "http://www.com" linuxtest.com/test.jpg -I     //-e選項自定義referer
HTTP/1.1 403 ForbiddenServer: nginx/1.12.2Date: Thu, 15 Mar 2018 14:33:28 GMTContent-Type: text/htmlContent-Length: 169Connection: keep-alive

二、訪問控制

訪問控制即限制指定的IP才能訪問指定的目錄

1、修改虛擬主機配置文件

[root@sdwaqw vhost]# vim linuxtest.conf //添加如下內容

location /admin/
{
allow 192.168.242.128;
allow 127.0.0.1;
deny all;# 設置IP白名單
}

2、測試

[root@sdwaqw vhost]# mkdir /data/wwwroot/linuxtest/admin
[root@sdwaqw vhost]# echo “test,test”>/data/wwwroot/linuxtest/admin/1.html
[root@sdwaqw vhost]# curl -x127.0.0.1:80 linuxtest.com/admin/1.html
“test,test”
[root@sdwaqw vhost]# curl -x192.168.242.128:80 linuxtest.com/admin/1.html
“test,test”

3、訪問控制-正則

location ~ .(abc|image)/..php$
{
deny all;
}

4、訪問控制-代理

if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
{
return 403;
}

三、Nginx解析PHP

修改虛擬主機配置文件
[root@sdwaqw vhost]# vim linuxtest.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;
}

四、Nginx代理

Nginx代理是一種反向代理。反向代理(Reverse Proxy)方式是指以代理服務器來接受Internet上的連接請求,然後將請求轉發給內部網絡上的服務器;並將從服務器上得到的結果返回給Internet上請求連接的客戶端,此時代理服務器對外就表現爲一個服務器。
graph LR
用戶–>代理服務器
代理服務器–>用戶
代理服務器–>web服務器
web服務器–>代理服務器
92.Nginx配置:防盜鏈、訪問控制、解析PHP以及代理

1、更改配置文件

[root@sdwaqw vhost]# vim proxy.conf

server
{
listen 80;
server_name ask.apelearn.com;# 定義域名(一般和被代理ip的域名保持一致)
location /
{
proxy_pass http://47.91.145.78/;# 指定被代理(被訪問)的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;
}
}

2、測試

[root@sdwaqw vhost]# vim proxy.conf
[root@sdwaqw vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@sdwaqw vhost]# curl -x127.0.0.1:80 ask.apelearn.com -I //同通過代理
HTTP/1.1 200 OKServer: nginx/1.12.2Date: Thu, 15 Mar 2018 15:44:25 GMTContent-Type: text/htmlConnection: keep-aliveVary: Accept-EncodingX-Powered-By: PHP/5.3.29P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"Set-Cookie: ape__Session=k44g3eklsert1fgbjhl061l4f4; path=/; domain=.apelearn.comExpires: Thu, 19 Nov 1981 08:52:00 GMTCache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0Pragma: no-cache

[root@sdwaqw vhost]# curl ask.apelearn.com -I //直接連
HTTP/1.1 200 OKServer: nginx/1.8.0Date: Thu, 15 Mar 2018 15:46:06 GMTContent-Type: text/htmlConnection: keep-aliveVary: Accept-EncodingX-Powered-By: PHP/5.3.29P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"Set-Cookie: ape__Session=ium8s3hsrjh4ulf6qbrjpdcme2; path=/; domain=.apelearn.comExpires: Thu, 19 Nov 1981 08:52:00 GMTCache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0Pragma: no-cache

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章