微服务Api网关的配置Nginx

时间:2020-09-14 10:25:50

标签: nginx microservices gateway api-gateway

我想使用Nginx将请求代理到当前网络接口主机名,例如:

使用URL /catalog/categories/...到主机名catalog:8080的URL /categories/...代理
网址/vitrine/products/...代理到主机名为vitrine:8080的网址{

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

配置应如下所示:

    http {
            ...    
            server {
                    listen 80;
                    server_name yourdomain;
    
                    set $mycatalog catalog:8080;
                    set $myvitrine vitrine:8080;
            }
        
            location ~ ^/catalog/categories/(.*)$ {
                    proxy_set_header Host $host:$server_port;
                    proxy_pass http://$mycatalog/categories/$1$is_args$args;
            }

            location ~ ^/vitrine/products/(.*)$ {
                    proxy_set_header Host $host:$server_port;
                    proxy_pass http://$myvitrine/products/$1$is_args$args;
            }
    }