首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >nginx-代理没有转发到码头-集装箱?

nginx-代理没有转发到码头-集装箱?
EN

Stack Overflow用户
提问于 2022-11-07 14:42:06
回答 1查看 42关注 0票数 0

我有一个系统,由两个码头-集装箱,前端和后端。前端容器拥有一个带有颤振web应用程序的nginx-webserver,而后端提供了前端使用的REST接口。前端和后端都是独立的项目,并且都可以独立地工作。两者都在我外部创建的同一个停靠网络(“my”)中。

代码语言:javascript
复制
docker network create my-network

下面是后端的docker-compose.yml:

代码语言:javascript
复制
version: '3'
services:
  my-backend:
    image: my-backend
    ports:
      - "8080:8080"
    networks:
      - my-network
networks:
  my-network:
    driver: bridge
    external: true

这里是最前沿的一个:

代码语言:javascript
复制
version: '3'
services:
  my-frontend:
    image: my-frontend
    ports:
      - "4200:80"
    networks:
      - my-network
networks:
  my-network:
    driver: bridge
    external: true

一切都很好,我可以在我的前端容器中使用curl http://my-backend:8080/somepath

我的nginx.conf如下:

代码语言:javascript
复制
events {}

http {
    include    /etc/nginx/conf/mime.types;

    server {
        listen 80 default_server;

        root '/usr/share/nginx/html';
        index index.html index.htm;

        location / {
            try_files $uri $uri/ /index.html;
        }

        location /backend {
          proxy_pass http://my-backend:8080;
        }

        location /example {
          proxy_pass https://example.com;
        }
    }
}

正如我所说的,在前端的容器中,我可以使用curl解析my-backend:8080。我还可以从浏览器(使用http://localhost:4200)和示例转发(http://localhost:4200/example)访问前端,但是http://localhost:4200/backend没有到达它的目的地(而http://localhost:8080工作得非常好)。超时后,nginx返回404。有谁能给我解释一下有什么问题吗?

Docker-我使用的图像是最新的nginx和最新的openjdk。

编辑:

我稍微修改了nginx.conf文件,添加

代码语言:javascript
复制
    location /backend {
      resolver 127.0.0.11; # added this line also to http and server-section
      set $backend http://ausanz-backend:8080;
      proxy_pass $backend;
    }

此外,我使用nginx调试。在调试输出中,我看到IP地址被正确解析(调试输出的某些部分):

代码语言:javascript
复制
my-frontend_1  | 2022/11/08 12:56:02 [debug] 30#30: *5 http request line: "GET /backend/deployment-info HTTP/1.1"
my-frontend_1  | 2022/11/08 12:56:02 [debug] 30#30: *5 http uri: "/backend/deployment-info"
my-frontend_1  | 2022/11/08 12:56:02 [debug] 30#30: *5 test location: "backend"
my-frontend_1  | 2022/11/08 12:56:02 [debug] 30#30: *5 using configuration "/backend"
my-frontend_1  | 2022/11/08 12:56:02 [debug] 30#30: *5 rewrite phase: 3
my-frontend_1  | 2022/11/08 12:56:02 [debug] 30#30: *5 http script value: "http://my-backend:8080"
my-frontend_1  | 2022/11/08 12:56:02 [debug] 30#30: *5 http script set $backend
my-frontend_1  | 2022/11/08 12:56:02 [debug] 30#30: *5 http script var: "http://my-backend:8080"
my-frontend_1  | 2022/11/08 12:56:02 [debug] 30#30: *5 http script copy: "Host"
my-frontend_1  | 2022/11/08 12:56:02 [debug] 30#30: *5 http script var: "my-backend:8080"
my-frontend_1  | 2022/11/08 12:56:02 [debug] 30#30: *5 http script copy: "Connection"
my-frontend_1  | 2022/11/08 12:56:02 [debug] 30#30: *5 http script copy: "close"
my-frontend_1  | "GET /backend/deployment-info HTTP/1.0
my-frontend_1  | Host: my-backend:8080
my-frontend_1  | Connection: close
my-frontend_1  | 2022/11/08 12:56:02 [debug] 30#30: *5 http upstream resolve: "/backend/deployment-info?"
my-frontend_1  | 2022/11/08 12:56:02 [debug] 30#30: *5 name was resolved to 172.19.0.2
my-frontend_1  | 2022/11/08 12:56:02 [debug] 30#30: *5 connect to 172.19.0.2:8080, fd:13 #7
my-frontend_1  | 2022/11/08 12:56:02 [debug] 30#30: *5 http upstream connect: -2
my-frontend_1  | 2022/11/08 12:56:02 [debug] 30#30: *5 http upstream request: "/backend/deployment-info?"
my-frontend_1  | 2022/11/08 12:56:02 [debug] 30#30: *5 http proxy status 404 "404 Not Found"

我证实了curl 172.19.0.2:8080/deployment-info实际上是我的后端。

EN

回答 1

Stack Overflow用户

发布于 2022-11-08 14:04:28

解决办法相当简单。总是使用path+query参数调用后端。nginx规则没有反映出这一点。解决方案是使用regex作为proxy_pass。不过,解析器仍然需要,否则我得到了502个坏网关。

代码语言:javascript
复制
    location ~ ^/backend/(.*) {
      resolver 127.0.0.11;
      proxy_pass http://my-backend:8080/$1$is_args$args;
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74348369

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档