1

I want to reverse proxy like this:

foo.bar.com:3000 -> localhost:4000 bar.foo.com:8080 -> localhost:4000

I think if this is possible, it might look something like this:

server {
  listen 3000
  server_name foo.bar.com

  listen 8080
  server_name bar.foo.com

  proxy_pass localhost:4000
  #A lot more configuration
}

I know I can just create two server directives, each listens on a different port with different server_name but both proxy_pass to localhost:4000.

The problem with this approach is that: redundancy of configuration for each server directive. (The configuration is duplicated - one for each server directive).

Tran Triet
  • 153
  • 1
  • 8

1 Answers1

1

You can add multiple listen directives and multiple hostnames in server_name:

server {
    listen 3000;
    listen 8080;
    server_name foo.bar.com bar.foo.com;

    location / {
        proxy_pass http://localhost:4000;
    }
}
Freddy
  • 1,999
  • 5
  • 12
  • Thanks for the suggestion. But I only want to serve this server with exactly two pairs of server_name:port only, namely `port foo.bar.com:3000` and `bar.foo.com:8080`. Your configuration will serve this server on 4 pairs: `bar.foo.com:8080`, `bar.foo.com:3000` and `foo.bar.com:3000`, `foo.bar.com:8080`. – Tran Triet May 07 '19 at 02:32
  • 1
    Then you will probably have to use two `server`s with or without `include`s. – Freddy May 07 '19 at 06:49