【问题标题】:HTTPS with Nginx and NodejsHTTPS 与 Nginx 和 Nodejs
【发布时间】:2019-03-16 08:19:59
【问题描述】:

所以我使用 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.js nginx https


    【解决方案1】:

    如果您已使用 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。

    【讨论】:

    • 但是我无法访问 /var/www/html 中的文件
    • 在服务器中添加一个单独的位置块。位置 /public { 根 /var/www/html; }
    • 我按照我原来的帖子那样做了,非常感谢!
    猜你喜欢
    • 2017-06-08
    • 2017-06-30
    • 1970-01-01
    • 2015-04-21
    • 1970-01-01
    • 1970-01-01
    • 2020-10-05
    • 1970-01-01
    • 2021-04-13
    相关资源
    最近更新 更多