How to implement SSL (http to https) with Flask, Flask-SocketIO and nginx

My application uses Flask-Socketio, Flask and nginx. I read in the post that all HTTP-HTTPS processing should be done at the Web server level, not the Application Server level. I have used the attribute rewrite

to redirect all HTTP requests as HTTPS requests. This works successfully with static pages. However, when I try to load dynamic content, I get an error 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.

.

Further I also get this error 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.

My nginx.conf file looks like this

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;
    }
}

      

Please, help. Should checkbox-SocketIO also contain the certificate and key paths?

+3


source to share


1 answer


Try the following:

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 *;
}

      



But HTTPS overload is preferable, the proxy_pass http: // directive is more efficient , it helps Nginx get a response from the backend as soon as possible and close the connection. The only requirement is to have a backend (which is listening on port 9901) to serve HTTP.

0


source







All Articles