how to redirect http to https in nginx docker elastic beanstalk

给你一囗甜甜゛ 提交于 2020-01-15 04:13:31

问题


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.


回答1:


Found a solution for it, just add

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

to the nginx configuration of the eb instance.




回答2:


This is really an nginx question (add the proper tag).

Your config seems complicated. Start from this one instead. This is what I'm using to redirect http port 80 traffic to TLS/SSL port 443 traffic.

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

server {
    listen 80;
    server_name www.example.com;

    return 301 https://$host$request_uri;
}

server {
    listen      443 ssl;
    server_name www.example.com;

    location / {
        root   /usr/share/nginx/html;
        index  index.html;
    }
}


来源:https://stackoverflow.com/questions/39716786/how-to-redirect-http-to-https-in-nginx-docker-elastic-beanstalk

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!