【问题标题】:Nginx deny doesn't work for folder filesNginx 拒绝对文件夹文件不起作用
【发布时间】:2013-10-16 15:26:25
【问题描述】:

我试图限制对我的网站的访问,只允许特定的 IP,但我遇到了以下问题:当我访问 www.example.com 时,拒绝工作正常,但是当我尝试访问 www.example.com 时/index.php 它返回“拒绝访问”页面,并且 php 文件直接在浏览器中下载而无需处理。 我确实想拒绝访问网站上除我之外的所有 IP 的所有文件。我该怎么做?

这是我的配置:

server {
listen 80;
server_name example.com; 
root /var/www/example;

location / {
    index index.html index.php; ## Allow a static html file to be shown first
    try_files $uri $uri/ @handler; ## If missing pass the URI to front handler
    expires 30d; ## Assume all files are cachable
 allow my.public.ip;
 deny all;
}

location @handler { ## Common front handler
    rewrite / /index.php;
}
location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
    rewrite ^(.*.php)/ $1 last;
}

location ~ .php$ { ## Execute PHP scripts
    if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss

    expires        off; ## Do not cache dynamic content
    fastcgi_pass   127.0.0.1:9001;
    fastcgi_param  HTTPS $fastcgi_https;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params; ## See /etc/nginx/fastcgi_params
    }
}

【问题讨论】:

    标签: nginx


    【解决方案1】:

    这是因为您的拒绝/允许规则仅适用于一个位置。

    删除它并尝试:

    server {
        listen 80;
        server_name example.com; 
        root /var/www/example;
        if ($remote_addr != "YOUR.PUBLIC.IP") {return 403;}
        ...
    }
    

    由于测试在任何特定位置块之外,它将适用于所有情况。

    还要注意IF 在这里并不邪恶,因为它只是“返回”。

    【讨论】:

    • 有趣的建议。谢谢!现在有 2 个问题: 1. 如果我有 IP 列表,我应该怎么做? 2. 如果我只想阻止包含 php 文件的特定文件夹,最好的方法是什么?
    【解决方案2】:

    好的,所以我找到了解决方案。 Nginx 处理最精确的正则表达式,在这种情况下是 php 文件的正则表达式。要使配置正常工作,必须在 / location 规则中定义除@handler 之外的所有其他位置(您不能置于任何规则之下 - 只能作为 root 用户)

    server {
    listen 80;
    server_name example.com; 
    root /var/www/example;
    
        location / {
        index index.html index.php; ## Allow a static html file to be shown first
        try_files $uri $uri/ @handler; ## If missing pass the URI to front handler
        expires 30d; ## Assume all files are cachable
        allow my.public.ip;
        deny all;
    
        location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
            rewrite ^(.*.php)/ $1 last;
        }
    
        location ~ .php$ { ## Execute PHP scripts
            if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss
    
            expires        off; ## Do not cache dynamic content
            fastcgi_pass   127.0.0.1:9001;
            fastcgi_param  HTTPS $fastcgi_https;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params; ## See /etc/nginx/fastcgi_params
            }
    }
    
        location @handler { ## Common front handler
            rewrite / /index.php;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-14
      • 1970-01-01
      • 1970-01-01
      • 2016-01-01
      • 2016-03-06
      • 2012-11-19
      • 1970-01-01
      • 2013-06-05
      • 1970-01-01
      相关资源
      最近更新 更多