简体   繁体   English

使用Nginx反向代理隐藏一个域上的Node应用程序端口号

[英]Hiding Node app port numbers on one domain with nginx reverse proxy

I'm still a relative beginner in nginx and hope to get some help and clarification on something I'm working on. 我仍然是nginx的相对入门者,希望对我正在从事的工作有所帮助和澄清。

So say I have 2 Node apps, app1 and app2 . 假设我有2个Node应用程序app1app2

I have a production server, but I'm trying to test this locally first. 我有一个生产服务器,但是我想先在本地进行测试。

Currently app1 listens on port 8000 and app2 listens on port 8001. 当前, app1侦听端口8000,而app2侦听端口8001。

So currently, they are sitting at localhost:8000 and localhost:8001 , and would accessed in the production server as production.example.com:8000 and production.server.com:8001 . 因此,当前它们位于localhost:8000localhost:8001 ,并将在生产服务器中作为production.example.com:8000production.server.com:8001

My question is, how can I hide the port numbers and assign them to a specific URL? 我的问题是,如何隐藏端口号并将它们分配给特定的URL?

I want the result to be accessible from localhost/app1 and localhost/app2 , and production.example.com/app1 and production.server.com/app2 in the production server. 我希望可以从生产服务器中的localhost/app1localhost/app2以及production.example.com/app1production.server.com/app2访问结果。

I don't know what I'm getting wrong in the nginx.conf , so I hope someone can help me on this issue. 我不知道我在nginx.conf中出了什么问题,所以我希望有人可以在这个问题上为我提供帮助。 These apps all have HTML forms, so I need them to post to production.example.com/app1 or something like production.example.com/app2/download . 这些应用程序都具有HTML表单,因此我需要将它们发布到production.example.com/app1或诸如production.example.com/app2/download Their CSS breaks as well due to the location of the public folder in each app, since they are only in /public/css.css , not in app2/public/css.css . 由于它们仅位于/public/css.css ,而不位于app2/public/css.css ,因此它们的CSS也由于每个应用程序中公用文件夹的位置而app2/public/css.css

I can change all of the form actions and router gets/posts, as well as the stylesheet references by adding /app1 and /app2 respectively in the Node apps, but that feels like I'm doing something wrong, as I shouldn't change any of my router info. 我可以通过分别在Node应用程序中添加/app1/app2来更改所有表单动作和路由器获取/发布,以及样式表引用,但是感觉好像我做错了,因为我不应该更改我的任何路由器信息。

Here is my nginx.conf file: 这是我的nginx.conf文件:

Edit: So this is what I have right now: 编辑:所以这就是我现在所拥有的:

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;
        }
}

And I still get the same issue where the node apps themselves are not using their contexts. 我仍然遇到相同的问题,即节点应用程序本身未使用其上下文。

So, I have this same configuration with like 4-5 microservices. 因此,我具有与4-5个微服务相同的配置。 Here is the configuration that I use. 这是我使用的配置。

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;
    }
}

Hope it serves as a constructive reference. 希望它可以作为建设性的参考。 Let's take one, 我们拿一个

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

So, the first rewrite matches only localhost/actions and forwards it to http://127.0.0.1:5000 The second one matches localhost/actions/<anything> and forwards it to http://127.0.0.1:5000/<anything> 因此,第一次重写仅匹配localhost/actions并将其转发到http://127.0.0.1:5000 ;第二次重写匹配localhost/actions/<anything>并将其转发到http://127.0.0.1:5000/<anything>

I think you're missing the slash ( / ) before the regex match. 我认为您在正则表达式匹配之前缺少了斜杠( / )。

Edit: 编辑:

Using Your comment as reference 使用您的评论作为参考

The index is at /app1/index 索引位于/app1/index
a page is at /app1/index/flashfireblast 页面位于/app1/index/flashfireblast
and the navbar header needs to reference /app1/stylesheets/css.css 并且导航栏标题需要引用/app1/stylesheets/css.css

So, To address /app1/stylesheets/css.css from /app1/index/flashfireblast you need to add ../stylesheets/css.css as the stylesheet href. 因此,要从/app1/index/flashfireblast /app1/stylesheets/css.css处理/app1/stylesheets/css.css ,您需要添加../stylesheets/css.css作为样式表href。

For reference: 以供参考:
If current directory is /var/www 如果当前目录是/var/www
1) / : means Root. 1) / :表示根。 /
2) ./ : means Current Directory. 2) ./ :表示当前目录。 /var/www
3) ../ : means Previous directory. 3) ../ :表示上一个目录。 /var/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM