在NGINX上为节点js应用程序启用HTTPS不起作用

时间:2017-09-27 17:57:48

标签: node.js nginx https reverse-proxy

我使用的是在ubuntu服务器中部署的简单的hello world express JS(8080端口)应用程序,NGINX反向代理设置如下所示。

该应用程序适用于http端口但不适用于https端口

nginx版本:nginx / 1.10.3(Ubuntu) OpenSSL 1.0.2g 2016年3月1日

我的配置文件是这样的:

server {
        listen 80;
        listen 443 default ssl;
        server_name localhost;

         ssl_certificate /root/mydir/ssl/certificate.crt;
         ssl_certificate_key /root/mydir/ssl/private.key;

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

配置适用于我的域testdomain.com的http连接 但完全没有https://testdomain.comhttps://www.testdomain.com

任何人都可以帮助解决此配置出现的问题

注意:sslforfree.com生成的ssl证书

1 个答案:

答案 0 :(得分:1)

server {
  listen       80;
   server_name example.com;

   # force redirect http to https
   rewrite ^ https://$http_host$request_uri? permanent;    
}

server {
  listen 443;
   ssl on;
   ssl_certificate /root/mydir/ssl/certificate.crt;
   ssl_certificate_key /root/mydir/ssl/private.key;
   server_name example.com;

   proxy_pass         http://127.0.0.1:8000;
   proxy_set_header   Host $host;
   proxy_set_header   X-Real-IP $remote_addr;
   proxy_set_header   X-Forwarded-Proto https;
   ....
 }
相关问题