【问题标题】:Hiding Node app port numbers on one domain with nginx reverse proxy使用 nginx 反向代理在一个域上隐藏节点应用程序端口号
【发布时间】:2018-08-08 22:09:57
【问题描述】:

我还是 nginx 的相对初学者,希望能得到一些帮助和澄清我正在做的事情。

假设我有 2 个 Node 应用,app1app2

我有一个生产服务器,但我想先在本地测试它。

目前 app1 监听 8000 端口,app2 监听 8001 端口。

所以目前,它们位于localhost:8000localhost:8001,并在生产服务器中以production.example.com:8000production.server.com:8001 访问。

我的问题是,如何隐藏端口号并将它们分配给特定的 URL?

我希望可以从生产服务器中的localhost/app1localhost/app2 以及production.example.com/app1production.server.com/app2 访问结果。

我不知道我在nginx.conf 中出了什么问题,所以我希望有人能在这个问题上帮助我。这些应用程序都有 HTML 表单,所以我需要它们发布到 production.example.com/app1 或类似 production.example.com/app2/download 的东西。由于每个应用程序中公共文件夹的位置,它们的 CSS 也会中断,因为它们只在 /public/css.css 中,而不是在 app2/public/css.css 中。

我可以通过在 Node 应用程序中分别添加 /app1/app2 来更改所有表单操作和路由器获取/发布以及样式表引用,但这感觉就像我做错了什么,因为我不应该更改我的任何路由器信息。

这是我的nginx.conf 文件:

编辑:这就是我现在所拥有的:

server {
        # ...

        location /app1 {
                rewrite ^/app1$ / break;
                rewrite ^/app1/(.*) /$1 break;
                proxy_pass      http://127.0.0.1:8000;
        }

        location /app2 {
                rewrite ^/app2$ / break;
                rewrite ^/app2/(.*) /$1 break;
                proxy_pass      http://127.0.0.1:8001;
        }
}

我仍然遇到相同的问题,即节点应用程序本身没有使用它们的上下文。

【问题讨论】:

    标签: node.js nginx proxy reverse-proxy reverse


    【解决方案1】:

    所以,我有同样的配置,大概有 4-5 个微服务。这是我使用的配置。

    server {
    
        listen       80;
        server_name  localhost;
    
        #charset koi8-r;
    
        #access_log  logs/host.access.log  main;
        location /ifttt {
            rewrite ^/ifttt$ / break;
            rewrite ^/ifttt/(.*) /$1 break;
            proxy_pass http://127.0.0.1:8080;
        }
        location /actions {
            rewrite ^/actions$ / break;
            rewrite ^/actions/(.*) /$1 break;
            proxy_pass http://127.0.0.1:5000;
        }
        location /events {
            rewrite ^/events$ / break;
            rewrite ^/events/(.*) /$1 break;
            proxy_pass http://127.0.0.1:5050;
        }
        location / {
            proxy_pass http://127.0.0.1:8081;
        }
    }
    

    希望它可以作为建设性的参考。我们来一个,

    location /actions {
        rewrite ^/actions$ / break;
        rewrite ^/actions/(.*) /$1 break;
        proxy_pass http://127.0.0.1:5000;
    }
    

    因此,第一次重写仅匹配 localhost/actions 并将其转发给 http://127.0.0.1:5000 第二个匹配localhost/actions/<anything>并转发给http://127.0.0.1:5000/<anything>

    我认为您在正则表达式匹配之前缺少斜杠 (/)。

    编辑:

    使用您的评论作为参考

    索引位于/app1/index
    页面位于/app1/index/flashfireblast
    并且导航栏标题需要引用/app1/stylesheets/css.css

    因此,要从/app1/index/flashfireblast 寻址/app1/stylesheets/css.css,您需要添加../stylesheets/css.css 作为样式表href。

    供参考:
    如果当前目录是/var/www
    1) /:表示Root。 /
    2) ./:表示当前目录。 /var/www
    3) ../:表示上一个目录。 /var/

    【讨论】:

    • 您好,感谢您的回复。我有你的建议,但我仍然有节点应用程序不知道它们的上下文的问题。例如,在 app2 中,它只是一个网络表单并发布到自己。但是,路由器中的上下文是“/”,而 HTML formaction 是“/”。但是,当前 url 是 localhost/app2,新的 formaction 应该是“/app2/”。我认为路由器不需要更改,但 HTML 表单需要更改。有没有其他办法解决?
    • 如果您考虑一下,如果它发布到“/”,那么它本质上就是发布到 localhost。所以,更简单的方法应该是使用它自己的 url 或使用绝对 url,或者只是在 / 之前添加 . 使其成为 ./ ,这意味着当前路径
    • 如何在 / 之前引用它?索引为/app1/index,有/app1/index/flashfireblast的页面,导航栏标题引用./stylesheets/css.css,即/app1/index/flashfireblast/stylesheets/css.css,不存在。样式表和客户端 Javascript 位于 /stylesheets/css.css 等。如何在不硬编码 URL 的情况下从 /app1/index/flashfireblast 引用 /app1/stylesheets/css.css?我希望能够在不更改任何硬编码的情况下在本地对其进行测试。
    • 我不得不重写很多,但我让它们工作了,谢谢你的建议。
    猜你喜欢
    • 2022-01-07
    • 2019-06-01
    • 1970-01-01
    • 2021-02-04
    • 2019-10-21
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    相关资源
    最近更新 更多