2014-01-31 26 views
0

我有一個困難時期嘗試一些安全添加到網站, 基本上我想,一個目錄下的項目必須:Nginx的身份驗證和重定向到HTTPS

1) be redirected to https if http 
2) be under HttpAuthBasicModule 

因爲某些原因,我可以得到AUTH的工作,但它不是重定向到https的某些URL,如的index.php,但它確實爲一些其他的文件:

/revive/www/admin/assets/images/login-welcome.gif works 
/revive/www/admin/index.php remains under port 80 

這是我的nginx的配置文件中的相關部分

location ^~ /revive/www/admin { 

    if ($server_port = 80) { 
     rewrite^https://$host$request_uri permanent; 
    } 

    auth_basic   "Restricted"; 
    auth_basic_user_file htpasswd_revive; 

    location ~ \.php$ { 
     fastcgi_split_path_info ^(.+\.php)(/.+)$; 
     fastcgi_pass 127.0.0.1:9001; 
     fastcgi_param PHP_VALUE "newrelic.appname=revive.host.com"; 
     fastcgi_index index.php; 
     include fastcgi_params; 
    } 
} 

如何確保/ revive/www/admin下的所有內容都被重定向到了端口443(如果它在端口80上調用的話)?任何幫助將不勝感激!

回答

0

原來,這似乎是工作,如果我這樣做:

location ^~ /revive/www/admin { 

    if ($server_port = 80) { 
     rewrite^https://$host$request_uri permanent; 
    } 

    auth_basic   "Restricted"; 
    auth_basic_user_file htpasswd_revive; 

    location ~ \.php$ { 

     if ($server_port = 80) { 
      rewrite^https://$host$request_uri permanent; 
     } 

     fastcgi_split_path_info ^(.+\.php)(/.+)$; 
     fastcgi_pass 127.0.0.1:9001; 
     fastcgi_param PHP_VALUE "newrelic.appname=revive.host.com"; 
     fastcgi_index index.php; 
     include fastcgi_params; 
    } 
} 
1

把你的服務器的規則只能用「聽443」,並添加以下規則波紋管:

server { 
    listen 80; 
    server_name mysite.com; 
    rewrite^https://$server_name$request_uri? permanent; 
} 
+0

不幸我不能這樣做是因爲我想重定向到https只有那個特殊的URL,其餘的網站是由清漆代理 – sathia

+0

我看不出有什麼理由不能做到這一點,你已經這樣做了,但在更糟糕的情況下方法,使用'if' –