天天看點

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防盜鍊

設定目錄通路受限:

 1. 配置test.com網站目錄的防盜鍊,編輯虛拟主機配置檔案:

[root@hao-01 ~]# 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 ;

   if ($invalid_referer) {

       return 403;

   }

   access_log off;

[root@hao-01 ~]# /usr/local/nginx/sbin/nginx -t

3. 重新加載nginx配置檔案(非重新開機!):

[root@hao-01 ~]# /usr/local/nginx/sbin/nginx -s reload

4. 檢視test.com網站目錄下的靜态檔案:

[root@hao-01 ~]# ls /data/wwwroot/test.com/

5. curl測試,通過百度網站通路test.com網站下的靜态檔案,403受限:

6. curl測試,通過test.com網站通路test.com網站下的靜态檔案,200通:

1. 配置testt.com網站通路控制,編輯虛拟主機配置檔案:

   location /hao/

   {

       allow 127.0.0.1;

       allow 127.168.211.128;

       deny all;

<a href="https://s3.51cto.com/wyfs02/M02/9E/7D/wKiom1mRn-7jISM2AAAn1fx3_sE835.png" target="_blank"></a>

2. 檢測nginx配置檔案是否有錯?

4. 在test.com網站目錄下,建立一個目錄hao:

[root@hao-01 ~]# mkdir /data/wwwroot/test.com/hao/

5. ...hao/目錄下建立1.html檔案,并追加内容:

[root@hao-01 ~]# echo “test,test”&gt;/data/wwwroot/test.com/hao/1.html

7. curl通過白名單192.168.211.128ip通路test.com網站下hao目錄下的1.html檔案: [root@hao-01 ~]# curl -x192.168.211.128:80 test.com/hao/1.html -I

<a href="https://s2.51cto.com/wyfs02/M02/9E/6C/wKioL1mRoBHhIN7qAAANxz5V3t8335.png" target="_blank"></a>

[root@hao-01 ~]# dhclient ens37

10. 檢視ens37新網卡ip:

設定指定目錄下的php檔案解析受限:

1. 配置test.com網站php檔案的防盜鍊,編輯虛拟主機配置檔案:

增加内容:

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

deny all;

}

<a href="https://s5.51cto.com/wyfs02/M00/9E/6C/wKioL1mRoNSx9O2wAAA1hDZKz8g792.png" target="_blank"></a>

2. 在test.com網站目錄下,建立一個目錄upload/:

[root@hao-01 ~]# mkdir /data/wwwroot/test.com/upload/

3. ...hao/目錄下建立1.php檔案,并追加内容:

[root@hao-01 ~]# echo “111”&gt;/data/wwwroot/test.com/upload/1.php

4. 檢測nginx配置檔案是否有錯?

5. 重新加載nginx配置檔案(非重新開機!):

6. curl通路test.com網站下upload目錄下1.php檔案 403受限:

[root@hao-01 ~]# curl -x192.168.211.132:80 test.com/upload/1.php -I

根據user_agent限制:

1. 編輯test.com虛拟主機配置檔案:

增加内容(deny all和return 403效果一樣):

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

return 403;

<a href="https://s1.51cto.com/wyfs02/M00/9E/7E/wKiom1mRoV6xT9yGAAApI9EmQJM541.png" target="_blank"></a>

4. curl 指定user_agent為tomatoalsdkflsd,通路test.com網站 403受限

12.15 Nginx解析php相關配置

增加内容(解析php相關内容):

(注意這行很重要,fcgi.sock保證路徑存在:astcgi_pass unix:/tmp/php-fcgi.sock;

路徑不對,通路錯誤會報502)

location ~ \.php$

include fastcgi_params;

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

fastcgi_index index.php;

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

<a href="https://s3.51cto.com/wyfs02/M01/9E/7E/wKiom1mRoaKR2zftAABBAnNlZ0s122.png" target="_blank"></a>

2. 在test.com網站目錄下建立1.php檔案,并填寫如下内容:

[root@hao-01 ~]# vim /data/wwwroot/test.com/1.php

添加内容(php相關配置)

&lt;?php

phpinfo();

3. curl 通路test.com網站下的1.php檔案:

[root@hao-01 ~]# curl -x127.0.0.1:80 test.com/1.php

12.16 Nginx代理

1. 進入...vhost目錄下:

[root@hao-01 ~]# cd /usr/local/nginx/conf/vhost

2. 建立proxy.conf檔案,并寫入代理配置:

[root@hao-01 vhost]# vim /usr/local/nginx/conf/vhost/proxy.conf

server

   listen 80;

   server_name baidu.com;

   location /

       proxy_pass      http://111.13.101.208/;

       proxy_set_header Host   $host;

       proxy_set_header X-Real-IP      $remote_addr;

       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

<a href="https://s1.51cto.com/wyfs02/M00/9E/6C/wKioL1mRocKw03c8AAAvhNgjn9Q803.png" target="_blank"></a>

3. curl通路遠端baidu.com/robots.txt

[root@hao-01 vhost]# curl -x127.0.0.1:80 baidu.com/robots.txt

<a href="https://s2.51cto.com/wyfs02/M00/9E/6C/wKioL1mRojTDJe27AAAW5ji_Iqo911.png" target="_blank"></a>

本文轉自 主内安詳 51CTO部落格,原文連結:http://blog.51cto.com/zhuneianxiang/1956200,如需轉載請自行聯系原作者