LNMP架構(四)之Nginx防盜鏈,訪問控制,解析php,代理

Nginx防盜鏈 配置以下,能夠和上面的配置結合起來php

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  abc.com;    //定義白名單,不在白名單則返回403。
    if ($invalid_referer) {
        return 403;
    }
    access_log off;
}

即這個白名單爲referer,就是訪問當前網址的上一個網址。html

修改完記得nginx

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

輸入圖片說明

Nginx訪問控制vim

需求:訪問/admin/目錄的請求,只容許某幾個IP訪問,配置以下:服務器

location /admin/
{
    allow 192.168.137.1;
    allow 127.0.0.1;
    deny all;
}

mkdir /data/wwwroot/test.com/admin/
echo 「test,test」>/data/wwwroot/test.com/admin/1.html

curl -x127.0.0.1:80 test.com/admin/1.html -I
curl -x192.168.133.130:80 test.com/admin/1.html -I

輸入圖片說明

能夠匹配正則,限制站點目錄下訪問php。curl

location ~ .*(abc|image)/.*\.php$   //定義匹配abc或者image目錄下以php結尾的都拒絕。
{
        deny all;
}

輸入圖片說明

根據user_agent限制socket

if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')        //嚴格匹配,若是要忽略大小寫,在~後加*
{
      return 403;
}

deny all和return 403效果同樣ide

輸入圖片說明

Nginx解析php相關配置php-fpm

配置以下:url

location ~ \.php$
    {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/php-fcgi.sock;          //這裏的文件路徑要和php-fpm配置文件裏頭的同樣,若是php-fpm監聽的是127.0.0.1:9000,那這裏也一樣要改爲監聽127.0.0.1:9000。
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
    }

fastcgi_pass 用來指定php-fpm監聽的地址或者socket,要是這裏寫錯會出現502的報錯。

輸入圖片說明

Nginx代理

cd /usr/local/nginx/conf/vhost
vim proxy.conf //加入以下內容
server
{
    listen 80;
    server_name ask.apelearn.com;        //定義服務器的域名
    location /
    {
        proxy_pass      http://121.201.9.155/;       //定義服務器地址
        proxy_set_header 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

輸入圖片說明

相關文章
相關標籤/搜索