首页 > 解决方案 > HTTPS 与 Nginx 和 Nodejs

问题描述

所以我使用 certbot 为 https 配置 Nginx。效果很好(我使用了自动配置)。

现在如何配置我的 Node.js 后端,以便能够从 Nginx 托管的前端发出 GET/POST 请求?

编辑:

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

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
    }

标签: node.jsnginxhttps

解决方案


如果您已使用 Ssl 和节点应用程序正确配置了 Nginx。您发送的请求应与https://URL一起使用。请检查您的 nginx.conf 并验证以下内容。

您可以在 Nginx 中添加 ssl 证书

ssl_certificate  /etc/nginx/ssl/server.crt #cert location
  ssl_certificate_key /etc/nginx/ssl/server.key #key location

在 Nginx 的服务器块中。并且您的 nodejs 应用程序应该在服务器块中这样配置

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

这足以让您的服务器使用 SSL。


推荐阅读