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

时间:2016-03-18 07:38:44

标签: php zend-framework nginx

我需要在我的网站中限制管理模块。不幸的是,它并不总是有效。我正在从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的问题。我在php应用程序中使用Zend Framework。

2 个答案:

答案 0 :(得分:0)

所有以.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将它们卸载到每个位置。

有关详细信息,请参阅this document

答案 1 :(得分:0)

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