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

Nginx防盜鏈

爲什麼給靜態文件(如圖片,視頻等)設置防盜鏈?是由於若是不設置防盜鏈,有心人就會在本身的網站中連接到咱們的網站,用戶在訪問他的網站的時候,能夠無限制的看到咱們服務器上的圖片等信息,佔用咱們的帶寬資源,服務器資源等,而用戶並不知道實際上時瀏覽的咱們服務器上的資源。因此爲了防止這種狀況發生,咱們就給靜態的文件作防盜鏈。php

  1. 設定目錄訪問受限,配置test.com網站目錄的訪問受限,編輯虛擬主機配置文件html

    [root[@localhost](https://my.oschina.net/u/570656) ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
  2. 添加以下內容nginx

    location ~*^.+\.(gif|jpg|jpeg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|bmp|xls)$
     {
     expires	 7d;
     valid_referers none blocked server_name *.test.com;(指定referer白名單)
     if ($invalid_referer) {
     return 403;
       }
     access_log  off;
     }
  3. 測試配置文件是否有錯誤,並從新加載配置文件(不是重啓)vim

    [root[@localhost](https://my.oschina.net/u/570656) ~]# /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[@localhost](https://my.oschina.net/u/570656) ~]# /usr/local/nginx/sbin/nginx -s reload
  4. 使用curl命令測試,curl -e能夠模擬referer,可是模擬referer的格式必須是http://....瀏覽器

    [root[@localhost](https://my.oschina.net/u/570656) ~]# curl -e "http://wwww.baidu.com" -x127.0.0.1:80 test.com/1.jpg -I
     HTTP/1.1 403 Forbidden
    
     [root[@localhost](https://my.oschina.net/u/570656) ~]# curl -e "http://www.test.com" -x127.0.0.1:80 test.com/1.jpg -I
     HTTP/1.1 200 OK

上面輸出結果太多,我是簡化呈現的,能夠看到,使用非指定的referer訪問的結果是403,而使用指定的referer訪問的結果是200。服務器

Nginx訪問控制

當咱們的網站中有某一站點只是針對公司內部使用,禁止外網使用的時候可使用訪問控制來實現網絡

  1. 編輯虛擬主機配置文件curl

    [root@localhost ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
  2. 添加以下內容ide

    location /admin/
     {
             allow 127.0.0.1;   //現實生產中,該白名單地址應設置爲公司外網地址。
             deny all;
     }
  3. 使用curl命令測試,能夠看到,使用指定白名單ip能夠正常訪問,使用沒指定過的ip訪問該站點就會受到限制。php-fpm

    [root@localhost ~]# curl -x127.0.0.1:80 test.com/admin/1.jpg
     fangwen kongzhi ceshi `
     [root@localhost ~]# curl -x192.168.254.131:80 test.com/admin/1.jpg
     <html>
     <head><title>403 Forbidden</title></head>
     <body bgcolor="white">
     <center><h1>403 Forbidden</h1></center>
     <hr><center>nginx/1.15.3</center>
     </body>
     </html>

設定指定目錄下的PHP文件解析受限

  1. 編輯虛擬主機配置文件

    [root@localhost ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
  2. 添加內容

    location ~ .*(upload|image)/.*\.php$
     {
     deny all;
     }
  3. 在test.com目錄下建立一個upload目錄,並寫一個PHP文件1.php

  4. 測試配置文件是否有問題,並從新加載

    [root@localhost ~]# /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@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
  5. 使用curl測試限制解析是否成功,能夠看到返回的代碼是403,表示限制解析成功

    [root@localhost ~]# curl -x127.0.0.1:80 test.com/upload/1.php 
     <html>
     <head><title>403 Forbidden</title></head>

根據user_agent限制訪問(能夠拒絕網絡爬蟲爬走網站內容)

好比我想讓誰訪問個人網站,我就告訴他域名,若是不告訴別人域名,就說明我不想讓他知道個人站點,這須要禁止搜索引擎在網絡上爬取站點內容。能夠經過user_agent來限制。

  1. 編輯虛擬主機文件

    [root@localhost ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
  2. 添加以下內容

    if ($http_user_agent ~* 'Spider/3.0|baidu|YoudaoBot|Tomato')
     {
     return 403;
     }
  3. 測試並從新加載配置文件 ..-t ...-s reload

  4. 使用curl測試,curl -A 能夠模擬user_agent,發現返回的代碼是403,表示實驗成功。

    [root@localhost ~]# curl -A "www.baidu.com" -x127.0.0.1:80 test.com -I
     HTTP/1.1 403 Forbidden
     Server: nginx/1.15.3
     Date: Tue, 04 Sep 2018 17:57:37 GMT
     Content-Type: text/html
     Content-Length: 169
     Connection: keep-alive

nginx解析PHP的相關配置

Nginx安裝完默認是不能解析PHP文件的,須要配置一下虛擬配置文件和php-fpm.com文件作一個相應調整才能夠

  1. 編輯虛擬配置文件

    [root@localhost tmp]# vim /usr/local/nginx/conf/vhost/test.com.conf
  2. 增長內容

    location ~ \.php$
    
     {
    
     include fastcgi_params;
    
     fastcgi_pass unix:/tmp/php-fcgi.sock;	 //路徑和文件要準確,否則會報錯502
     #fastcgi_pass 127.0.0.1:9000;//這兩行配置哪一行都行,可是得跟php-fpm.conf裏面一致,不然就會報錯502
    
     fastcgi_index index.php;
    
     fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
    
     }
  3. 在/data/wwwroot/test.com/admin/下建立可解析的PHP文件data/wwwroot/test.com/upload/下建立可解析的PHP文件

    [root@localhost tmp]# vim /data/wwwroot/test.com/admin/1.php
     [root@localhost tmp]# cp /data/wwwroot/test.com/admin/1.php /data/wwwroot/test.com/upload/1.php
  4. 而後經過瀏覽器分別訪問這兩個站點下的PHP文件

  5. 經過上面實驗能夠看到,PHP解析已經正常了,而upload目錄下的不能解析是由於咱們給upload這個目錄設置了PHP解析限制。

Nginx代理

Nginx代理,好比咱們要訪問國外的某些站點,這些站點在國內的訪問速度較慢,咱們能夠經過代理的方式達到快速訪問的目的。

  1. 進入/vhost目錄

    [root@localhost tmp]# cd /usr/local/nginx/conf/vhost/
  2. 建立proxy.conf文件,並寫入代理配置

    server
     {
     listen 80;
     server_name baidu.com;
     location /
    	{
     proxy_pass	http://220.181.111.188/;
     proxy_set_header Host   $host;
     proxy_set_header X-Real-IP      $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     	}
     }
  3. 測試並從新加載配置文件

    [root@localhost 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@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload
  4. 使用curl 訪問百度,成功訪問baidu,實驗完成。

    [root@localhost vhost]# curl -x127.0.0.1:80 baidu.com/robots.txt
     User-agent: Baiduspider
     Disallow: /baidu
     Disallow: /s?
     Disallow: /ulink?
     Disallow: /link?
     Disallow: /home/news/data/
相關文章
相關標籤/搜索