2014-10-06 144 views
0

我需要一些nginx PRO来帮助我。我在我的网站上有一个秘密页面,我需要保护它免受扫描机器人和暴力破解者(实际上这是登录页面)。如果有人试图访问此页面而没有特殊的cookie,我需要将任何人重定向到404页面。如果cookie设置在上面,我需要这个页面才能很好地工作。让我看看我的配置:nginx和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代码,而不是重定向我!我做错了什么?我的头坏了。

谢谢。

回答

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 
    } 
} 
+0

有比较大的块..为什么我需要重复这个块?看,有一个块重写index.php和位置〜\ .php后正常执行。 – Epsiloncool 2014-10-07 09:28:04