【问题标题】:Set up nginx and node apps on ubuntu在 ubuntu 上设置 nginx 和 node 应用程序
【发布时间】:2017-12-22 17:42:34
【问题描述】:

情况是这样的——

  • node express app1(后端应用程序 - REST API 服务器) - 在端口 8888 上运行 - 一堆 POST 路由(身份验证、api、下载、上传..) - 与 mysql 服务器通信 - 由开发人员 #1 维护
  • node (webpack, react) app2 (front end app) 在端口 80 上运行 - 由开发人员 #2 维护

本周我们设置了 nginx 和 HTTPS(通过 Let's encrypt),这意味着当用户访问 www.oursite.com - 它被重定向到带有漂亮绿色锁和安全站点的 https 站点。到目前为止,一切都很好。但是,当用户输入她的电子邮件时,app2 应该调用 app1 并且应该发送带有注册令牌的电子邮件。在我们尝试设置 nginx 并在两个应用程序上使用 http 之前,这很有效。现在这个电话没有发生。这是我们的 nginx 设置:

server {
    listen 80;
    listen [::]:80;

    root /var/www/oursite.com/html;

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

    server_name oursite.com www.oursite.com;

    location / {
        try_files $uri $uri/ =404;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/oursite.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/oursite.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot




    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    # Redirect non-https traffic to https
    # if ($scheme != "https") {
    #     return 301 https://$host$request_uri;
    # } # managed by Certbot

}

我的问题(开发人员 #1) - 我是否应该将我的应用程序(后端应用程序 - app1 - 后端)中的所有路由替换为 https 而不是 http? Apparently Chrome is not calling http request 来自 https 站点(如我们的情况) 那么我应该如何设置 nginx - 我相信我应该在 nginx 设置中添加另一个位置部分。 如果有其他建议,请告诉我们。

【问题讨论】:

  • 是的,我认为您应该能够使用环回 IP 地址(或本地主机)向 app2 发送 https 请求

标签: node.js ubuntu express nginx https


【解决方案1】:

每个端口都必须有自己的 server{ } 块,因此端口 80 得到一个,端口 443 得到另一个,每个端口都有一个监听指令到各自的端​​口......在上面你有两个端口在同一个 server{ } 块

如果应用程序暴露在互联网上,他们应该使用 https,如果不是,他们使用 http 的传入流量(端口 80 块必须被重定向以将流量发送到 443 ......所以你的 nginx 配置文件看起来类似于

server { 

    listen  80 ;
    listen  [::]:80 ;

    server_name example.com, www.example.com;

    # Redirect all HTTP requests to HTTPS 
    rewrite ^/(.*) https://example.com/$1 permanent;
}

server {

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

    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/oursite.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/oursite.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    location / {

        # direct traffic to my server on its host IP and port
        proxy_pass http://172.19.0.5:3000/; # notice http not https
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
    }
}

在您的情况下,app1 似乎没有暴露在互联网上,因此不会出现在您的 nginx 配置中......它只接收来自 app2 下游的 http 流量......以上所有配置仅适用于您的 app2......使用安全 https 端口 443(或重定向到 443 的端口 80)的传入 Internet 流量接收所谓的 TLS 终止...由 nginx TLS 终止从 Internet 屏蔽的应用程序接收 http 流量而不是 https,这很好,因为应用程序端口是从互联网上看不到......所以你的 app2 获得 http 流量而不是 https

【讨论】:

  • 感谢您的回答,但我是否需要 node express (app1) 上的 https 服务器,或者我可以通过 app1 上的 http 以某种方式进行管理 - 因为 app2 在 https 上并且正在调用 app1,即(当前)不在https上??希望我在这里清楚我的问题
【解决方案2】:

我们通过将 app1(后端)也设置为 https 来实现这一点。但是,这适用于所有主要浏览器,除了 Firefox。所以我想我现在应该发布一个新问题。该网站不允许通过 Firefox 上的表格 - 但前提是地址没有 www。这意味着https://testsite.com -> 不工作,https://www.testsite.com 工作正常。但在所有其他浏览器上都可以正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-07
    • 2011-08-22
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    • 2015-07-18
    • 1970-01-01
    • 2019-04-04
    相关资源
    最近更新 更多