通过docker host对nginx映射路由,为每个路由调用不同的容器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过docker host对nginx映射路由,为每个路由调用不同的容器相关的知识,希望对你有一定的参考价值。

我正在尝试通过docker host映射路由,为每个路由调用不同的容器。

我有一个docker-compose有2个服务,而且服务是:5000端口。我的nginx.conf映射如下代码:

location /template-api {

        rewrite ^/template-api/?(.*) /$1 break;
        proxy_pass         http://template-api:5000;
        proxy_redirect     off;
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Host $server_name;
    }

    location /api-plan  {
            rewrite ^/api-plan/?(.*) /$1 break;
            proxy_pass         http://api-plan:5000;
            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
    }

何时,我调用localhost:8000 / api-template / documentation,调用静态文件的路由或返回静态文件的路由。他在localhost:8000 / swaggerui中返回错误,找不到文件。错误的发生是因为swagger ui文件夹在localhost:8000 / api-template / swaggerui和localhost:8000 / api-plan / swaggerui

要修复该错误,添加到nginx,conf映射/路由:

server { 
listen 8000;

    access_log  /var/log/nginx/api.log;
    error_log   /var/log/nginx/api_error.log;
    server_name localhost;

    location / {
        proxy_pass         http://api-plan:5000;
        proxy_redirect     off;
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Host $server_name;

    }

现在,当我调用localhost:8000 / api-plan / documentation时,结果成功,但是,当我调用localhost:8000 / template-api / documentation时,API会重定向到localhost:8000 / api-plan /导致错误的路由。

答案

试试这个:

server { 
    listen 8000;
    access_log  /var/log/nginx/api.log;
    error_log   /var/log/nginx/api_error.log;
    server_name localhost;

    location / {
        proxy_pass         http://$server_name:8080/swaggerUi;
        proxy_redirect     off;
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Host $server_name;

    }
    location /template-api {

            rewrite ^/template-api/?(.*) /$1 break;
            proxy_pass         http://template-api:5000;
            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
    }

    location /api-plan  {
            rewrite ^/api-plan/?(.*) /$1 break;
            proxy_pass         http://api-plan:5000;
            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
    }
}
另一答案

您可以使用cookie来检测哪个后端应该收到/swaggerui请求。这是非常不安全的,因为用户可以编辑cookie并尝试访问其他主机,但由于它是本地测试环境,它只是起作用。

server { 
    listen 8000;
    access_log  /var/log/nginx/api.log;
    error_log   /var/log/nginx/api_error.log;
    server_name localhost;

    location ~ ^/swaggerui {
            resolver 127.0.0.11 ipv6=off;
            proxy_pass         http://$cookie_origin:5000;
            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;

    }

    location /template-api {
            rewrite ^/template-api/?(.*) /$1 break;
            proxy_pass         http://template-api:5000;
            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
            add_header         Set-Cookie "origin=template-api;Domain=localhost;Path=/;Max-Age=100000";
    }

    location /api-plan  {
            rewrite ^/api-plan/?(.*) /$1 break;
            proxy_pass         http://api-plan:5000;
            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
            add_header         Set-Cookie "origin=plan-api;Domain=localhost;Path=/;Max-Age=100000";
    }
}

以上是关于通过docker host对nginx映射路由,为每个路由调用不同的容器的主要内容,如果未能解决你的问题,请参考以下文章

Dockers安装nginx

避免Kafka客户端无法连接Docker上运行的Kafka,又名:Docker如何添加hosts映射

Docker搭建Kong--配置Service并添加Key-auth

官方文档,才是正途-docker-compose

Docker网络

docker容器域名映射