简体   繁体   English

无法访问 nginx 容器后面的容器化服务

[英]Can't reach containerized service behind nginx container

I got this architecture and it is almost working.我得到了这个架构,它几乎可以工作了。

  • I can curl the service directly and it dispatches the requests to the replicas running behind.我可以直接curl服务,并将请求分派给后面运行的副本。
  • Also I can curl the nginx container.我也可以卷曲nginx容器。
  • Additionally I can run docker exec -i nginx curl http://articleservice:8080/somemethod without any issues.另外我可以运行docker exec -i nginx curl http://articleservice:8080/somemethod没有任何问题。

But something seems to be wrong with my config.但是我的配置似乎有问题。 The nginx and the container are on the same network. nginx 和容器在同一个网络上。

Architecture:建筑学:

                                             [replica 2]
                                            /
[incoming traffic] -> [nginx] -> [service a]
                                            \
                                             [replica 1]

docker-compose.yml docker-compose.yml

version: "3"
services:
  articleservice:
    image: elps/articleservice:1.1.0.5
    deploy:
      replicas: 2
      restart_policy:
        condition: on-failure
      placement:
        constraints:
          - node.role == manager
    ports:
      - "8080:8080"
    environment:
      - MYSQL_HOST=192.168.178.96
      - MYSQL_DB=catalog
      - MYSQL_USER=root
      - MYSQL_PASSWORD=root
    networks:
      - webnet     

  nginx-default:
    image: nginx
    deploy:
      restart_policy:
        condition: on-failure
      placement:
        constraints:
          - node.role == manager
    ports:
      - "80:80"
    networks:
      - webnet

networks:
  webnet:

nginx-config nginx配置

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

  server_name example.com;

  location /article {
      proxy_pass http://articleservice:8080/; 
  }
}

Once again, I forgot to add the host in the header.再一次,我忘记在标题中添加主机。

Solution:解决方案:

curl -H 'host: example.com' http://articleservice:8080/somemethod

You should create a volume in the nginx-default service so it can use the nginx.conf file.您应该在 nginx-default 服务中创建一个卷,以便它可以使用 nginx.conf 文件。

Example:例子:

  nginx-default:
    image: nginx
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
    deploy:
      restart_policy:
        condition: on-failure
      placement:
        constraints:
          - node.role == manager
    ports:
      - "80:80"
    networks:
      - webnet

Another problem is that if you access the url example.com/article/someMethod it will translate to articleservice:8080/article/someMethod另一个问题是,如果您访问 url example.com/article/someMethod它将转换为articleservice:8080/article/someMethod
To remove the prefix "/article" part you can rewrite it:要删除前缀“/article”部分,您可以重写它:

  location /article {
      proxy_pass http://articleservice:8080/; 
      rewrite ^/article(.*)$ $1 break;
  }

Now you can use curl example.com/article/someMethod to use the articleService现在你可以使用curl example.com/article/someMethod来使用 articleService

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

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