1

I'm trying to set up what might be the "hello world" of proxy pass redirects. It entails a default route, and one single conditional route.

I want all of the traffic to my server to proxy some ip 1.2.3.4 unless it matches /blog in which case it should proxy 5.5.5.5

I've spent some time guessing and checking with the config, and read a bunch of docs, but the amount of variance is overwhelming, and I can't seem to find an anchor point of even a single fully functional simple example to seed from. So far, this is what I think should work:

http {
  server {
    listen 80;

    location / {
      proxy_pass 1.2.3.4
    }

    location ~ /blog {
      proxy_pass 5.5.5.5
    }
  }
}

I've updated the config like so and reloaded without any errors, but when I navigate to the server I just get the default Welcome to nginx! landing page.

What am I missing here?

Seph Reed
  • 121
  • 4

2 Answers2

2

Your server block is missing the server_name directive.

From the documentation:

In this configuration nginx tests only the request’s header field “Host” to determine which server the request should be routed to. If its value does not match any server name, or the request does not contain this header field at all, then nginx will route the request to the default server for this port.

Highlighting by me.

Your block has no server name, so the request is routed to the default server, which is defined in the default config.

You only need to add server_name:

http {
server {
    listen 80;
    server_name example.com;

    # the rest of your config
}
}

No need to remove everything from the default config.

Gerald Schneider
  • 19,757
  • 8
  • 52
  • 79
0

Ends up this setup is mostly correct. listen 80 can be dropped as that's the default value, otherwise it's fine.

My issue was that the default ubuntu installation has some stuff that needs to be specifically disabled. I found an answer by looking at this question:

https://stackoverflow.com/questions/14460935/nginx-only-shows-welcome-page

In short, navigate to etc/nginx/sites-enabled. ls to see what's in there. If you see anything that's linked you can use sudo unlink ./default-or-name-of-item.

Then there won't be anything in sites enabled to serve... my guess is that location / is really, really low on the totem poll or priorities when choosing something to serve.

Seph Reed
  • 121
  • 4
  • 1
    all you needed was a `server_name` directive. With a name present your server block would have gotten precedence over the defaults. – Gerald Schneider Nov 05 '19 at 19:56