如何在http到nginx docker elastic beanstalk中将http重定向到https

问题描述:

我的django应用程序托管在docker elastic beanstalk中,它使用nginx。对于SSL,我使用的是aws证书。
要将http重定向到https,我在docker容器中尝试使用了nginxx_forwarded_proto,但是我收到了502错误。这是nginx配置:

I've django application hosted in docker elastic beanstalk, which uses nginx. For SSL i'm using aws certificate. To redirect http to https i tried " x_forwarded_proto " with trhe nginx inside the docker container but i'm getting a 502 error. here's the nginx config:

server {

listen      80 default_server;

server_name www.example.com; 

access_log /home/docker/logs/nginx-access.log;
error_log /home/docker/logs/nginx-error.log;


if ($host !~* ^(www.example.com|example.com)$ ) {
    return 444;
}

if ( $http_x_forwarded_proto != 'https' ) {
return 301 https://$host$request_uri;
}

location / {
    uwsgi_pass unix:/var/sockets/api.sock;
    include    /home/docker/server/uwsgi_params; #
  }  
}

任何人都可以提出更好的解决方案。

Can anyone suggest a better solution for it.

找到一个解决方案,只需添加

Found a solution for it, just add

if ( $http_x_forwarded_proto != 'https' ) {
return 301 https://$host$request_uri;
}

到eb实例的nginx配置。

to the nginx configuration of the eb instance.