如何解决 Kong Gateway 上“从上游服务器收到无效响应”?

如何解决 Kong Gateway 上“从上游服务器收到无效响应”?

我正在使用带有 Compose 的 docker 并创建了一些微服务,并尝试通过 Kong Gateway 连接它们。

为了使用 PHP 访问 MS,我在 Nginx 中创建了反向代理,我可以通过以下 URL 示例进行访问:

http://service1.localhost:8081 (Laravel with PHP MS)
http://service2.localhost:8081 (Laravel with PHP MS)
http://service3.localhost:8081 (Laravel with PHP MS)

http://frontend.localhost:8080 (Nuxt JS With Node MS)

问题是,我有一个只有 Nginx 的容器,它允许我通过反向代理以上述方式使用其他微服务,但 Laravel 中的 MSs URL 有示例路径:

/api/v1/abc /api/v1/abc/def /api/v1/def /api/v1/def/123 /api/v1/abc/abcd-efg

也就是说,它们有几条“深度”路径。当我尝试将这些 URL 插入 Kong 时,访问它们时出现错误:

“从上游服务器收到无效响应”

在浏览器中,它指向 502;我知道 502 是错误的网关,所以我相信错误可能出在我使用的 Nginx conf 文件中的设置中。

我的 Nginx 配置如下(Laravel 中每个 MS 1 个文件,只更改 url 的路径和子域):

server {
    listen 80;
    listen [::]:80;
    server_name service1.localhost;
    
    root /var/www/service1/public;

    error_log  /var/log/nginx/error.logs;
    access_log /var/log/nginx/access.logs;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";
 
    index index.php;
 
    charset utf-8;
 
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
 
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
 
    error_page 404 /index.php;
 
    location ~ \.php$ {
        fastcgi_pass service1.localhost:9000;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;        
    }
 
    location ~ /\.(?!well-known).* {
        deny all;
    }
}

在 docker 日志中,在 Kong 容器中我看到以下错误:

[error] 2140#0: *13211 connect() failed (111: Connection refused) while connecting to upstream, client: 172.19.0.1, server: kong, request: "GET /api/v1/states HTTP/1.1", upstream: "http://172.19.0.2:8000/api/v1/states", host: "localhost:8000"
    [error] 2140#0: *13211 connect() failed (111: Connection refused) while connecting to upstream, client: 172.19.0.1, server: kong, request: "GET /api/v1/states HTTP/1.1", upstream: "http://172.19.0.2:8000/api/v1/states", host: "localhost:8000"
    [error] 2140#0: *13211 connect() failed (111: Connection refused) while connecting to upstream, client: 172.19.0.1, server: kong, request: "GET /api/v1/states HTTP/1.1", upstream: "http://172.19.0.2:8000/api/v1/states", host: "localhost:8000"
    [error] 2140#0: *13211 connect() failed (111: Connection refused) while connecting to upstream, client: 172.19.0.1, server: kong, request: "GET /api/v1/states HTTP/1.1", upstream: "http://172.19.0.2:8000/api/v1/states", host: "localhost:8000"
    [error] 2140#0: *13211 connect() failed (111: Connection refused) while connecting to upstream, client: 172.19.0.1, server: kong, request: "GET /api/v1/states HTTP/1.1", upstream: "http://172.19.0.2:8000/api/v1/states", host: "localhost:8000"
    [error] 2140#0: *13211 connect() failed (111: Connection refused) while connecting to upstream, client: 172.19.0.1, server: kong, request: "GET /api/v1/states HTTP/1.1", upstream: "http://172.19.0.2:8000/api/v1/states", host: "localhost:8000"

由于服务1容器是179.19.0.2,这179.19.0.1网关 IP。所有微服务都在同一个网络上。我已经在windows hosts文件中输入了这些地址。

192.168.0.109 host.docker.internal
192.168.0.109 gateway.docker.internal

而且我也在所有微服务中使用“extra_hosts”配置:

extra_hosts:
  - "host.docker.internal:host-gateway"

我已经尝试过几种方式在 Kong 中进行配置,看过几个教程,看到过几个示例,但没有视频或文章将该示例展示为子域中具有反向代理的微服务……

我已经尝试过将配置放在 Kong 中来搜索 url (service1.localhost:8081),我已经尝试过内部 IP (172.19.0.2:8081) 和 MS 共享的内部端口 (49154),但都不起作用。

PS:我正在使用 WSL2,我不知道它是否相关,但如果有人感兴趣......

相关内容