nginx和cookie重定向

时间:2014-10-06 23:48:37

标签: redirect cookies nginx config

我需要一些nginx PRO来帮助我。我在我的网站上有一个秘密页面,我需要保护它免受扫描机器人和暴力破解者的攻击(实际上这是登录页面)。如果有人试图访问此页面而没有特殊的cookie,我需要将任何人重定向到404页面。如果设置了cookie,我需要这个页面非常好用。让我们看看我的配置:

server {
        listen  80;
        server_name  example.com;
        root   /var/www/example.com/;
        index  index.php;
        client_max_body_size 64M;

        location ~* /(secret\-page\.php/).*$ {
#                if ($cookie_secretauth != "123123") {
                        rewrite ^/(.*)$ /not-found;
#                }
        }

        location / {
                if (!-e $request_filename) {
                    rewrite ^/(.*)$ /index.php last;
                }
        }

        location ~* ^/(images|data|t)/.+.(js|css|png|jpg|jpeg|gif|ico)$ {
                access_log        off;
                expires max;
        }

        location ~ \.php$ {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                #.....more things
        }
}

查看注释行。当这些行被注释掉时,重定向效果很好。一旦我删除评论,它就会下载我的PHP代码,而不是重定向我!我做错了什么?我的头坏了。

感谢。

1 个答案:

答案 0 :(得分:0)

必须在secret-page.php位置添加PHP处理程序,以便将其传递给PHP。

server {
    listen  80;
    server_name  example.com;
    root   /var/www/example.com/;
    index  index.php;
    client_max_body_size 64M;

    location ~* /(secret\-page\.php/).*$ {
            if ($cookie_secretauth != "123123") {
                    rewrite ^/(.*)$ /not-found;
            }
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #.....more things
    }

    location / {
            if (!-e $request_filename) {
                rewrite ^/(.*)$ /index.php last;
            }
    }

    location ~* ^/(images|data|t)/.+.(js|css|png|jpg|jpeg|gif|ico)$ {
            access_log        off;
            expires max;
    }

    location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #.....more things
    }
}