使用 nginx 限制对管理模块的访问并不总是有效


Restricting access to admin module with nginx not always working

我需要限制我网站的管理模块。不幸的是,它并不总是有效。我正在从 apache 迁移到 nginx,我的 nginx 配置是。

server {
    listen 80;
    server_name example.com;
    charset utf-8;
    index index.php;
    root /websites/example/www/;
    location /admin {
            allow 192.168.3.137;
            deny all;
            try_files $uri /index.php;
    }
    location / {
            try_files $uri /index.php;
    }
location ~* '.php$ {

        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param APPLICATION_ENV dev;
        include fastcgi_params;
    }

当我尝试在Firefox中打开 http://example.com/admin/时,我被重定向到我的主页。如果 url 没有尾部斜杠 http://example.com/admin,则会打开正确的管理员登录名。在这种情况下,访问日志只有"/"(主页)的记录。我对Chrome和IE没有问题。我使用Zend Framework来开发php应用程序。

所有

.php 结尾的文件都由 location ~* '.php$ 块处理,这有效地绕过了location /块中的访问规则。

一种方法可能是使用嵌套位置块并复制 PHP 块。例如:

location / {
    try_files $uri /index.php;
}
location ~* '.php$ {
    include fastcgi_params;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param APPLICATION_ENV dev;
}
location ^~ /admin {
    allow 192.168.3.137;
    deny all;
    try_files $uri /admin/index.php;
    location ~* '.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param APPLICATION_ENV dev;
    }
}

^~修饰符可确保location块处理以 /admin 开头的所有 URI。allow/deny规则由嵌套location块继承。

若要管理重复的代码,可能需要将 fastcgi 指令卸载到单独的文件中,并将它们include到每个位置。

有关详细信息,请参阅此文档。

感谢您的回复。我删除了FF饼干和缓存,问题解决了。