简体   繁体   English

如何使用 Nginx 反向代理将 Express-Gateway “主机”配置属性绑定到本地主机?

[英]How to bind Express-Gateway "host" configuration property to localhost with Nginx reverse proxy?

Express-Gateway is unable to bind to localhost or 127.0.0.1 Express-Gateway无法绑定到 localhost 或 127.0.0.1

Calling endpoints directly works as expected:调用端点直接按预期工作:

 curl http://localhost:5000/ip 

 curl http://localhost:5010/erp

Accessing all endpoints via the ExpressGateway on port 5000 works as expected通过端口 5000 上的 ExpressGateway 访问所有端点按预期工作

 curl http://localhost:5000/ip 

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

The issue问题

The nginx reverse proxy works normally but returns a failed response when accessing the gateway nginx反向代理正常工作但访问网关时返回失败响应

Cannot GET /api/erp

Binding host: localhost for the http in gateway.config.yml has no effect whatsoever.绑定主机:gateway.config.yml 中 http 的 localhost 没有任何作用。 Even when I change the host to another IP Address and port, the port reflects the change but the IP address of the host remains unchanged as [:::5000] in the express-gateway console.即使我将主机更改为另一个 IP 地址和端口,端口也会反映更改,但主机的 IP 地址在快速网关控制台中保持不变为 [:::5000]。

Please, how can I resolve this?请问,我该如何解决这个问题?

gateway.config.yml网关.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

The Reverse proxy with Nginx 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;
        }
}

You need to specify the hostname to gateway.config.* file您需要为 gateway.config.* 文件指定主机名

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

The host name could be localhost as well.主机名也可以是localhost


For express-gateway, If the hostname is not specified, the server IP address will be used as the default hostname.对于 express-gateway,如果未指定主机名,则服务器 IP 地址将用作默认主机名。

Example: Your server IP address: 123.456.789.12 The following log will show up when you start the gateway gateway http server listening on 123.456.789.12:5000示例:您的服务器 IP 地址: 123.456.789.12当您启动网关gateway http server listening on 123.456.789.12:5000时,将显示以下日志

That's why nginx can't call to localhost:5000这就是 nginx 无法调用localhost:5000的原因

When the hostname specified, the log should be: info: gateway http server listening on 0.0.0.0:5000当指定主机名时,日志应为: info: gateway http server listening on 0.0.0.0:5000

Change localhost to your local ip in this part:在此部分中将 localhost 更改为您的本地 ip:

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

Change to example:更改为示例:

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

And change in your nginx config port 82 to 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;
        }
}

This config works for me.这个配置对我有用。

You can change the localhost.您可以更改本地主机。 This solution works for me:这个解决方案对我有用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM