0

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]。

请问,我该如何解决这个问题?

网关.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;
        }
}
4

3 回答 3

0

您需要为 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

于 2020-11-14T15:25:35.330 回答
0

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

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
于 2021-09-24T13:53:14.867 回答
0

在这部分将 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;
        }
}

这个配置对我有用。

于 2021-03-15T22:26:52.780 回答