ExpressGateway / express-gateway

A microservices API Gateway built on top of Express.js
https://www.express-gateway.io
Apache License 2.0
2.97k stars 344 forks source link

How to bind express-gateway "host" to localhost and use an Nginx reverse proxy? #1004

Open Nditah opened 4 years ago

Nditah commented 4 years ago

Entering Issues

Express-Gateway is unable to bind to localhost or 127.0.0.1

The Reverse proxy works as expected for any other service apart from the gateway.

Accessing the gateway via the nginx returns a Cannot GET /api/erp

However, access the gateway locally on the machine works well as expected.

Binding host: localhost for the http in gateway.config.yml has no effect whatsoever.

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;
        }
}

ahmedmolawale commented 3 years ago

I am also having this concern. @Nditah did you find a way around this?

pdkproitf commented 3 years ago

Me too. I got a problem with this.

I found that the gateway always listens on the server IP address request but not the localhost request. I think that we need to make it support localhost requests.

pdkproitf commented 3 years ago

I found the solution, config the hostname to 0.0.0.0

"http": {
    "hostname": "0.0.0.0",
    "port": 1234
  },
iamsajidjaved commented 3 years ago

I have formatted gateway.config.yml through onlineyamltools.com after my changes and it has started working.

NOW IT LOOK LIKE:

http:
  port: 8080
admin:
  port: 9876
  host: localhost
apiEndpoints:
  api:
    host: localhost
    paths: /ip
  reqres:
    host: localhost
    paths: /api/users
serviceEndpoints:
  httpbin:
    url: https://httpbin.org
  reqresserv:
    url: https://reqres.in
policies:
  - basic-auth
  - cors
  - expression
  - key-auth
  - log
  - oauth2
  - proxy
  - rate-limit
pipelines:
  default:
    apiEndpoints:
      - api
    policies:
      - proxy:
          - action:
              serviceEndpoint: httpbin
              changeOrigin: true
  reqrespipeline:
    apiEndpoints:
      - reqres
    policies:
      - proxy:
          - action:
              serviceEndpoint: reqresserv
              changeOrigin: true
philipsjp26 commented 3 years ago

how reverse proxy in nginx for express gateway, my app not found always

Masum-Osman commented 2 years ago

@Nditah would you check your nginx config file? I can see two location / and I don't know how come those both run with different 5000 and 5010 port? How may it differentiate?

BTW, It is possible what you are trying to.