Nijo
Nijo

Reputation: 841

how to redirect http to https in nginx docker elastic beanstalk

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.

Upvotes: 1

Views: 2528

Answers (2)

Nijo
Nijo

Reputation: 841

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.

Upvotes: 3

Bernard
Bernard

Reputation: 17271

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

Upvotes: -2

Related Questions