0

I've a service running in 9000 port and setup ssl using letsencrypt. I would like to forward all request from http to https.

http://demo.mydomain.com -> https://demo.mydomain.com 
http://www.demo.mydomain.com -> https://demo.mydomain.com 
www.demo.mydomain.com -> https://demo.mydomain.com

Here's my configuration

vi /etc/nginx/sites-available/default

server {
        root /var/www/html;

        server_name demo.mydomain.com;

        location / {
                proxy_pass http://127.0.0.1:9000;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }


    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/demo.mydomain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/demo.mydomain.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}



server {
    if ($host = demo.mydomain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

     listen 80 default_server;
     listen [::]:80 default_server;

     server_name demo.mydomain.com;
     return 301 https://$host$request_uri;
     #return 404; # managed by Certbot

}

# nginx -t

# systemctl reload nginx.service

# curl -I https://demo.mydomain.com -> It works

# curl -I http://demo.mydomain.com -> It times out

I've tried many similar solutions but nothing worked for me. Thanks for any clue.

  • Have you read your logs? – Ginnungagap Sep 25 at 21:25
0

It was my stupid mistake. Port 80 wasn't open in aws instance. However here's my working configuration for force redirect from http to https for a service running in a different port.

server {
        server_name demo.mydomain.com www.demo.mydomain.com;

        location / {
                proxy_pass http://127.0.0.1:9000;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/demo.mydomain.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/demo.mydomain.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
    if ($host = demo.mydomain.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

     listen 80 default_server;
     listen [::]:80 default_server;

     server_name demo.mydomain.com;
     #return 404; # managed by Certbot
}

Also make sure to open ports 80 and 443. Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.