1. 程式人生 > >四十九、Nginx防盜鏈、Nginx訪問控制、Nginx解析PHP相關配置、Nginx代理

四十九、Nginx防盜鏈、Nginx訪問控制、Nginx解析PHP相關配置、Nginx代理

Nginx防盜鏈 Nginx訪問控制 Nginx解析PHP相關配置 Nginx代理

四十九、Nginx防盜鏈、Nginx訪問控制、Nginx解析PHP相關配置、Nginx代理

一、Nginx防盜鏈

必須和“不記錄日誌和過期時間”結合在一起,因為它們同時用到了location。

# vim /usr/local/nginx/conf/vhost/test.com.conf

location ~* ^.+\.(gif|jpg|png|bmp|swf|jpeg|flv|rar|zip|doc|pdf|gz|bz2|xls)$

{

expires 7d; 過期時間

valid_referers none blocked server_names

*.test.com;

//定義白名單的referer。

if ($invalid_referer) {

//如果訪問的不是白名單的,就會反饋403。

return 403;

}

access_log off; 訪問日誌不記錄

}

# /usr/local/nginx/sbin/nginx -t

# /usr/local/nginx/sbin/nginx -s reload

測試:

# curl -x 127.0.0.1:80 test.com/1.gif -I

HTTP/1.1 200 OK

# curl -x 127.0.0.1:80 test.com/1.jpg -I

HTTP/1.1 200 OK

# curl -e "http://www.baidu.com/1.txt" -x 127.0.0.1:80 test.com/1.jpg -I

HTTP/1.1 403 Forbidden

# curl -e "http://www.test.com/1.txt" -x 127.0.0.1:80 test.com/1.jpg -I

HTTP/1.1 200 OK


referer是test.com時就不會拒絕訪問,說明防盜鏈配置成功了。

核心配置就這三行:

valid_referers none blocked server_names *.test.com;

if ($invalid_referer) {

return 403; //這裏也可以寫deny all。


二、Nginx訪問控制

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

# vim /usr/local/nginx/conf/vhost/test.com.conf

location /admin/

{

allow 192.168.93.130;

allow 127.0.0.1;

deny all;

}

沒有像Apache那樣的先allow,再deny這個順序一說。

Nginx的是只要匹配到這個IP這條規則,就停止了,就不再繼續匹配其他規則了,也就不會被deny,所以最終的結果是allow。

所以三條規則中,來源IP只有一條會生效。


針對正則進行匹配:

# vim /usr/local/nginx/conf/vhost/test.com.conf

location ~.*(upload|image)/.*\.php$

{

deny all;

}

# /usr/local/nginx/sbin/nginx -t

# /usr/local/nginx/sbin/nginx -s reload

測試:

# mkdir /data/wwwroot/test.com/upload

# echo "11111" > /data/wwwroot/test.com/upload/1.php

# echo "11111" > /data/wwwroot/test.com/upload/1.txt

# curl -x 127.0.0.1:80 test.com/upload/1.php -I

HTTP/1.1 403 Forbidden //直接被拒絕了

# curl -x 127.0.0.1:80 test.com/upload/1.txt -I

HTTP/1.1 200 OK

# cat /tmp/test.com.log

127.0.0.1 - [27/Apr/2018:04:59:16 +0800] test.com "/upload/1.php" 403 "-" "curl/7.29.0"

127.0.0.1 - [27/Apr/2018:05:03:43 +0800] test.com "/upload/1.txt" 200 "-" "curl/7.29.0"

txt可以訪問,php禁止了。


根據user_agent限制:

# vim /usr/local/nginx/conf/vhost/test.com.conf

if ($http_user_agent ~* 'Spider/3.0|Youdaobot|Tomato')

{

return 403; //deny all和return 403效果一樣

}

# /usr/local/nginx/sbin/nginx -t

# /usr/local/nginx/sbin/nginx -s reload

$http_user_agent ~*:匹配符後面加個*號就可以忽略大小寫。

# curl -A "tomato" -x 127.0.0.1:80 test.com/upload/1.txt -I

HTTP/1.1 403 Forbidden

# curl -A "Tomato" -x 127.0.0.1:80 test.com/upload/1.txt -I

HTTP/1.1 403 Forbidden


三、Nginx解析PHP相關配置

# vim /usr/local/nginx/conf/vhost/test.com.conf

location ~ \.php$

{

include fastcgi_params;

fastcgi_pass unix:/tmp/php-fcgi.sock;

//當這裏寫的另外的路徑,就會顯示502,因為找不到socket。

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;

}

# /usr/local/nginx/sbin/nginx -t

# /usr/local/nginx/sbin/nginx -s reload

# vim /data/wwwroot/test.com/2.php

<?php

phpinfo();

# curl -x 127.0.0.1:80 test.com/2.php

沒有重新加載配置文件之前訪問出來的是源代碼,加載之後才能解析出來,訪問看到的是內容。


當出現狀態碼502的時候:要檢查這個地方,Nginx和php-fpm所配置的地址是否對應;fastcgi_pass unix:/tmp/php-fcgi.sock;

(一)首先,看一下錯誤日誌,先看這個文件有沒有(/tmp/php-cgi.sock),如果沒有,然後查看php-fpm.conf文件查看sock是什麽,再對應Nginx這邊的fastcgi_pass,要對應,不然就會502。

(二)要監聽端口

知道監聽的是IP和端口,在配置文件中就要做更改

將fastcgi_pass unix:/tmp/php-fcgi.sock;改為:

fastcgi_pass 127.0.0.1:9000;

不然就會502。

# vim /usr/local/php-fpm/etc/php-fpm.conf

listen.mode = 666 讀寫的權限:666


四、Nginx代理

技術分享圖片技術分享圖片

# cd /usr/local/nginx/conf/vhost/

[root@MRX vhost]# vim proxy.conf

server

{

listen 80;

server_name ask.apelearn.com;

location /

{

proxy_pass http://121.201.9.155/; //web服務器地址

proxy_set_header Host $host; //$Host就是server_name

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

}

# /usr/local/nginx/sbin/nginx -t

# /usr/local/nginx/sbin/nginx -s reload

# curl -x 127.0.0.1:80 ask.apelearn.com/robots.txt

此時代理服務器就是我的虛擬機,web服務器就是ask.apelearn.com這個論壇


四十九、Nginx防盜鏈、Nginx訪問控制、Nginx解析PHP相關配置、Nginx代理