【问题标题】:How to serve two back-end applications running on different ports with Nginx location?如何使用 Nginx 位置为在不同端口上运行的两个后端应用程序提供服务?
【发布时间】:2021-10-17 11:14:09
【问题描述】:

引导和 Angular 前端应用程序在 docker 容器上运行。我确实拥有一个域并使用 Nginx 反向代理来传递对适当前端的请求,后端是 spring-boot。默认 80 正在为我的前端服务以进行更多说明。

预告详情:

Sample domain  : xyz.com
Front-end app  : running on default port 80  
Back-end spring-boot app: running on port 8085

我的 Nginx 配置的第一部分:

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

    server_name xyz.com;


    location / {
            root /var/www/site;
            try_files $uri $uri/ /index.html;


    location /api{
            proxy_pass http://localhost:8085;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header connection "upgrade";
    }

}

当我输入 xyz.com 时,我的网站已启动并运行,并且有一些前端路由:

: xyz.com/blogs
: xyz.com/images
: xyz.com/news

这些是我在 springboot 后端的休息路线:

: xyz.com/api/article/getAll
: xyz.com/api/public/mail/sendmail

有趣的是我无法为运行在 8085 端口上的后端应用程序提供服务。

这意味着每当我的 URL 带有 /api/* 时,它应该转到端口 8085,但在我的情况下它显示“404 未找到”。

为什么我的休息路线不起作用,我的 Nginx 配置中是否遗漏了什么?

注意:Spring boot 容器工作正常,在 docker 或应用程序中没有问题,我认为问题出在 Nginx 配置尤其是位置块。

【问题讨论】:

  • FE 和 BE 在同一个 docker 容器上吗?
  • FE & nginx 不在容器中,只有 Spring-boot 运行在 Docker 容器端口 8085 @xTheDoctah
  • 所以让我看看我是否清楚,这些组件 Docker、Fe、Be 和 Nginx 都在同一台机器上,但 Springboot 它在 docker 内。对吗?
  • @xTheDoctah 是的,您似乎是对的,所有服务器都在同一台服务器上。 FE,Nginx 不在容器内。只有 BE(spring-boot)在 docker 里面
  • 你能运行 docker port [container name] 看看“external_port”是否正确吗? (如果您可以将输出添加到帖子中,审查ips(如果公开),那就太棒了)

标签: spring-boot docker nginx ubuntu-18.04 nginx-location


【解决方案1】:

该块首先为/ 提供服务,然后是/api。这应该反过来,然后事情就会正常工作。

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

    server_name xyz.com;

    location /api{
            proxy_pass http://localhost:8085;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header connection "upgrade";
    }

    location / {
            root /var/www/site;
            try_files $uri $uri/ /index.html;
    }

}

我遇到了同样的问题,并通过这种方式解决了。更多帮助请参考NGINX reverse proxy for multiple locations not working

【讨论】:

  • 你知道当我输入 xyz.com/api 时会出现什么情况,这显示了白标错误,但路由不起作用,它说找不到,例如:xyz.com/api/*/*xyz.com/api/*
猜你喜欢
  • 1970-01-01
  • 2019-08-07
  • 2019-07-25
  • 2013-04-22
  • 2017-10-16
  • 1970-01-01
  • 2019-05-27
  • 2020-09-27
  • 2015-07-31
相关资源
最近更新 更多