2014-10-06 131 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