简体   繁体   中英

Setup NGINX reverse proxy with SSL?

I'm trying to setup a Node.js Express server with https and to do that I am using a NGINX reverse proxy. My VPS is running Ubuntu 18.04. I updated the default server configuration in /etc/nginx/sites-enabled/default to this so it works with SSL (if it works):

server {

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

    listen 433 ssl default_server;
    listen [::]:433 ssl default_server;

    ssl_certificate /etc/nginx/fullchain.pem;
    ssl_certificate_key /etc/nginx/privkey.pem;

    root /var/www/html;

    index index.html index.htm index.nginx-debian.html;

    server_name mediaserver;

        location / {
            proxy_pass http://localhost:3000;
            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;
        }
}

I ran sudo systemctl restart nginx to restart NGINX. But the issue is that when I go to the IP Address of my server without https, it opens up fine, but when I go to the IP Address of my server with https, it says the site can't be reached. Any suggested fixes? Thanks.

You need to route the traffic to https instead of http. I also added the proxy_redirect directive which will route any insecure requests to https.

EDIT: Also one problem that I see, is that you are listening on the wrong port. It should be 443 , not 433 . Notice that I changed proxy_redirect to use port 80.

server {

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

    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;

    ssl_certificate /etc/nginx/fullchain.pem;
    ssl_certificate_key /etc/nginx/privkey.pem;

    root /var/www/html;

    index index.html index.htm index.nginx-debian.html;

    server_name mediaserver;

        location / {
          proxy_pass https://localhost:3000;
          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;
          proxy_redirect http://localhost:80 https://localhost:3000;
        }
}

Here is a link to the associated NGINX documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM