2014-11-06 91 views
3

我的應用程序使用Flask-Socketio,Flask和nginx。我在一篇文章中讀到,所有HTTP到HTTPS的處理都必須在Web Server級別完成,而不是在Application Server級別完成。我使用rewrite屬性將所有HTTP請求重定向爲HTTPS請求。這與靜態頁面成功工作。但是,當我嘗試加載動態內容時,出現錯誤,提示The page at 'https://localhost/myLoc' was loaded over HTTPS, but displayed insecure content from 'http://localhost/myLoc/more/paths?t=1390397': this content should also be loaded over HTTPS.如何使用Flask,Flask-SocketIO和nginx實現SSL(http to https)

而且我得到這個錯誤也XMLHttpRequest cannot load http://localhost/myLoc/more/paths?t=1390397. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://localhost' is therefore not allowed access.

我的nginx.conf文件看起來像這樣

server { 
    server { 
    listen  80; 
    server_name _; 
    rewrite^https://$host$request_uri? permanent; 
} 

server { 
    gzip on; 
    ssl  on; 
    listen 443 ssl; 

    server_name  *.mydomain.com; 

    ssl_certificate /path/to/nginx/ssl/nginx.crt; 
    ssl_certificate_key /path/to/nginx/ssl/nginx.key; 

    location /myLoc { 
      proxy_pass http://localhost:9001/myLoc; 
      proxy_redirect off; 
      proxy_buffering off; 

      proxy_set_header Host $host; 
      proxy_set_header X-Real-IP $remote_addr; 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    } 
} 

請幫助。 Flask-SocketIO是否也必須包含證書和密鑰的路徑?

+0

您正在加載第三方內容,如jQuery? – dirn 2014-11-06 15:24:37

+0

你如何引用你的內容?它看起來應該是相對的(沒有域名,例如'/ myLoc/more/paths?t = 1390397),你可能會在'http:// localhost/myLoc/more/paths?t = 1390397'顯式獲取資源')或協議相關(沒有方案,例如'// localhost/myLoc/more/paths?t = 1390397');理想情況下,應該使用'url_for'來代替硬編碼。您也可能需要在您的位置塊中設置「X-Forwaded-Proto」標頭併爲[此答案]添加「ProxyFix」中間件(https://stackoverflow.com/questions/23347387/x-forwarded-proto-和瓶)。 – jonafato 2014-11-06 18:02:40

+0

@dim:是的,我們正在使用JavaScript和jQuery – 2014-11-11 05:48:57

回答

0

試試這個:

location /myLoc { 
     proxy_pass https://localhost:9001/myLoc; 
     proxy_redirect off; 
     proxy_buffering off; 
     proxy_set_header Host $host; 
     proxy_set_header X-Real-IP $remote_addr; 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     add_header Access-Control-Allow-Origin *; 
} 

但HTTPS卸載是比較喜歡的方式,proxy_pass HTTP://指令更好, 它有助於Nginx的,以儘早和密切的聯繫得到後端響應。唯一的要求是讓後端(監聽端口9901)服務於HTTP。

+0

這不起作用。我早些時候嘗試過。 – 2014-11-11 03:17:32

+0

直到您提供日誌後才能使用 – Anatoly 2014-11-11 10:41:53

+0

請提供日誌? – Anatoly 2014-12-07 08:20:16