【问题标题】:How to bind Express-Gateway "host" configuration property to localhost with Nginx reverse proxy?如何使用 Nginx 反向代理将 Express-Gateway “主机”配置属性绑定到 localhost?
【发布时间】:2020-11-27 01:26:08
【问题描述】:

Express-Gateway 无法绑定到 localhost 或 127.0.0.1

调用端点直接按预期工作:

 curl http://localhost:5000/ip 

 curl http://localhost:5010/erp

通过端口 5000 上的 ExpressGateway 访问所有端点按预期工作

 curl http://localhost:5000/ip 

 curl http://localhost:5000/api/erp

问题

nginx反向代理正常工作,但访问网关时返回失败响应

Cannot GET /api/erp

绑定主机:gateway.config.yml 中 http 的 localhost 没有任何作用。 即使我将主机更改为另一个 IP 地址和端口,端口也会反映更改,但主机的 IP 地址在 express-gateway 控制台中保持不变,为 [:::5000]。

请问,我该如何解决?

gateway.config.yml

http:
  port: 5000


admin:
  port: 9876
  host: localhost

apiEndpoints:
  api:
    host: localhost
    paths: '/ip'

  erp:
    host: localhost
    paths: ['/api/erp', '/api/erp/*']                    
                          
serviceEndpoints:
  httpbin:
    url: 'https://httpbin.org'

  erpService:
    url: 'http://localhost:5010'                     
                          
      
policies:
  - basic-auth
  - cors
  - expression
  - key-auth
  - log
  - oauth2
  - proxy
  - rate-limit

pipelines:
  default:
    apiEndpoints:
      - api
    policies:
    # Uncomment `key-auth:` when instructed to in the Getting Started guide.
    # - key-auth:
      - proxy:
          - action:
              serviceEndpoint: httpbin 
              changeOrigin: true

  erpPipeline:
    apiEndpoints:
      - erp
    policies:
    # Uncomment `key-auth:` when instructed to in the Getting Started guide.
    # - key-auth:
      - proxy:
          - action:
              serviceEndpoint: erpService
              changeOrigin: true

Nginx 的反向代理


server {
listen 82;

location / {
        proxy_pass http://localhost:5010;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        }
}


server {
listen 81;

location / {
        proxy_pass http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        }
}

【问题讨论】:

    标签: nginx reverse-proxy api-gateway express-gateway


    【解决方案1】:

    您需要为 gateway.config.* 文件指定主机名

    "http": {
        "hostname": "0.0.0.0",
        "port": 1234
      },
    

    主机名也可以是localhost


    对于 express-gateway,如果未指定主机名,则使用服务器 IP 地址作为默认主机名。

    示例:您的服务器 IP 地址:123.456.789.12 启动网关时会显示以下日志 gateway http server listening on 123.456.789.12:5000

    这就是为什么nginx不能调用localhost:5000

    当指定主机名时,日志应该是: info: gateway http server listening on 0.0.0.0:5000

    【讨论】:

    • 我明确表示设置主机名也不起作用。
    • 您可以在启动网关时显示日志吗?
    【解决方案2】:

    在此部分将 localhost 更改为您的本地 ip:

    erpService:
        url: 'http://localhost:5010'
    

    换个例子:

    erpService:
        url: 'http://192.168.0.3:5010'
    

    并将您的 nginx 配置端口 82 更改为 5010

    server {
    listen 5010;
    
    location / {
            proxy_pass http://localhost:5010;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
            }
    }
    

    这个配置对我有用。

    【讨论】:

      【解决方案3】:

      您可以更改本地主机。 这个解决方案对我有用:

      http:
        port: 8080
      admin:
        port: 9876
        host: localhost
      apiEndpoints:
        api:
          host: "gateway.example.com"   =>>main domain for gateway
          paths: "/ip"
        panel:
          host: "gateway.example.com"  =>>main domain for gateway
          paths: "/panel/api/v1/*"
        account:
          host: "gateway.example.com"  =>>main domain for gateway
          paths: "/account/api/v1/*"
      serviceEndpoints:
        httpbin:
          url: "https://httpbin.org"  
        panelService:
          urls:
            - "https://panel-api.example.com"   =>> panel domain 
        accountService:
          urls:
            - "https://account-api.example.com" =>> account domain
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-25
        • 1970-01-01
        • 2021-08-30
        • 1970-01-01
        • 1970-01-01
        • 2016-07-14
        • 1970-01-01
        相关资源
        最近更新 更多