2011-01-31 44 views
15

我在Nginx後面運行FastCGI,並且需要檢測何時通過HTTPS訪問url。但是,我的Django Web應用程序總是報告連接是HTTP(request.is_secure()== False)。但是,SSL設置正確,並且我已通過SSL檢查器驗證了我的https://網址是否安全。NGINX後面的FastCGI應用程序無法檢測到使用HTTPS安全連接

我該如何讓Django正確檢測請求來自HTTPS url?

我的Nginx的設置是:

http { 
    include  mime.types; 
    default_type application/octet-stream; 

    sendfile  on; 
    keepalive_timeout 65; 

    gzip on; 

    server { 
     listen  80; 
     listen  443 default ssl; 
     ssl_certificate /home/webapp/ssl.crt 
     ssl_certificate_key /home/webapp/ssl.key 

     server_name myapp.com; 
     access_log /home/webapp/access.log 
     error_log /home/webapp/error.log 

     root /home/mywebapp; 

     location/{ 
       # host and port to fastcgi server      
      fastcgi_pass 127.0.0.1:8801; 
      fastcgi_param PATH_INFO $fastcgi_script_name; 
      fastcgi_param REQUEST_METHOD $request_method; 
      fastcgi_param QUERY_STRING $query_string; 
      fastcgi_param SERVER_NAME $server_name; 
      fastcgi_param SERVER_PORT $server_port; 
      fastcgi_param SERVER_PROTOCOL $server_protocol; 
      fastcgi_param CONTENT_TYPE $content_type; 
      fastcgi_param CONTENT_LENGTH $content_length; 
      fastcgi_pass_header Authorization; 
      fastcgi_intercept_errors off; 
     } 
    } 
} 

我開始Django的FastCGI進程:

python /home/webapp/manage.py runfcgi method=threaded host=127.0.0.1 port=8801 pidfile=/home/webapp/fastcgi.pid 

回答

13

確保nginx的是在443

+0

啊,謝謝,那做的伎倆。有沒有辦法只爲端口443設置這個參數?也就是說,無需複製整個配置塊? – 2011-01-31 19:07:42

+0

如下所示...您應該將變量設置爲「關閉」80端口。 – 2014-01-04 16:15:00

30

感謝發送fastcgi_param HTTPS on用於連接的雄二答案。我已經更新了我的服務器塊有條件注入HTTPS或HTTPS關閉,這取決於$ SERVER_PORT:

{

server { 
    listen  80; 
    listen  443 default ssl; 

    if ($server_port = 443) { set $https on; } 
    if ($server_port = 80) { set $https off; } 

    ssl_certificate /home/webapp/ssl.crt 
    ssl_certificate_key /home/webapp/ssl.key 

    server_name myapp.com; 
    access_log /home/webapp/access.log 
    error_log /home/webapp/error.log 

    root /home/mywebapp; 

    location/{ 
      # host and port to fastcgi server      
     fastcgi_pass 127.0.0.1:8801; 
     fastcgi_param PATH_INFO $fastcgi_script_name; 
     fastcgi_param REQUEST_METHOD $request_method; 
     fastcgi_param QUERY_STRING $query_string; 
     fastcgi_param SERVER_NAME $server_name; 
     fastcgi_param SERVER_PORT $server_port; 
     fastcgi_param SERVER_PROTOCOL $server_protocol; 
     fastcgi_param CONTENT_TYPE $content_type; 
     fastcgi_param CONTENT_LENGTH $content_length; 
     fastcgi_pass_header Authorization; 
     fastcgi_intercept_errors off; 

     fastcgi_param HTTPS $https; 
    } 
} 

}

相關問題