简体   繁体   中英

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

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

 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

Cannot GET /api/erp

Binding host: localhost for the http in gateway.config.yml has no effect whatsoever. 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.

Please, how can I resolve this?

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

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

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

The host name could be localhost as well.


For express-gateway, If the hostname is not specified, the server IP address will be used as the default hostname.

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

That's why nginx can't call to localhost:5000

When the hostname specified, the log should be: info: gateway http server listening on 0.0.0.0:5000

Change localhost to your local ip in this part:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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