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

[toc]php

12.13 Nginx防盜鏈

  1. 配置以下,能夠和上面的配置結合起來
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;
}
  1. curl -e "http://www.test.com/1.txt" -x127.0.0.1:80 -I test.com/1.gif

12.14 Nginx訪問控制

  1. 需求:訪問/admin/目錄的請求,只容許某幾個IP訪問,配置以下:
location /admin/
{
    allow 192.168.133.1;
    allow 127.0.0.1;
    deny all;
}//先allow 再deny;從上到下開始匹配
能夠匹配正則
location ~ .*(upload|image)/.*\.php$
{
        deny all;
}
  1. mkdir /data/wwwroot/test.com/upload
  2. echo "1111" > /data/wwwroot/test.com/upload/1.php
根據user_agent限制//~* 能夠忽略大小寫,即匹配後邊加*
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
{
      return 403;
}//deny all和return 403效果同樣
//禁止蜘蛛搜到本身
  1. curl -A "Tomato" -x127.0.0.1:80 test.com/upload/1.txt
  2. curl -x127.0.0.1:80 test.com/upload/1.php
  3. mkdir /data/wwwroot/test.com/admin/
  4. echo 「test,test」>/data/wwwroot/test.com/admin/1.html
  5. -t && -s reload
  6. curl -x127.0.0.1:80 test.com/admin/1.html -I
  7. curl -x192.168.133.130:80 test.com/admin/1.html -I

12.15 Nginx解析php相關配置

  1. 配置以下:
location ~ \.php$
    {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/php-fcgi.sock;//要和php的listen同樣,php也能夠是IP端口形式
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
    }

12.16 Nginx代理

  1. 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;
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

擴展

  1. 502問題彙總 http://ask.apelearn.com/question/9109
  2. location優先級 http://blog.lishiming.net/?p=100
相關文章
相關標籤/搜索