Nginx Reverse Proxy with HTTPS not working

httpsnginxPROXYreverse-proxyssl

I had successfully got the cert of my SSL and now I was trying to implement it into my AWS server with NGINX reserve proxy setup, here is the config file:

server {
    listen       80;
    server_name  example.com;
    return       301 https://www.example.com$request_uri;
}

server {
    listen 443 ssl;
    ssl_certificate /etc/nginx/ssl/cert_chain.crt;
    ssl_certificate_key /etc/nginx/ssl/website.key;
    server_name ~^(?<subdomain>.+)\.example\.com$;

    location / {
        proxy_pass http://www.example.com:8888;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $subdomain.example.com;
        proxy_cache_bypass $http_upgrade;
    }
}

If I change the setting to listen to port 80 then it's working fine. I had enabled port 443 in AWS security group and here is the result of netstat -tulpn | grep 443:

(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      -

So it means it's listening to port 443 already right? I also ensured that the nginx was run as root using ps aux|grep nginx|grep -v grep:

root     11567  0.0  0.3 177080  3060 ?        Ss   09:36   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 11568  0.0  0.7 177344  7568 ?        S    09:36   0:00 nginx: worker process

I had checked the Nginx error log and it got nothing inside, but when I access my url with https it just showing that it took too long to respond. Anyone able to help? Thank you.

Best Answer

replace your first server section with

server {
        listen 80;
        server_name  example.com;
        location / {
        rewrite ^/(.+) https://example.com/ permanent;
        }
       }