Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
388 views
in Technique[技术] by (71.8m points)

nginx doesn't proxy_pass to local virtual host

Setup

I have three virtual hosts: host.test is proxy_passing all requests to backend. Backend is available from WAN via some domain name (backend-domain.net).

There is also a fallback (default_server on port 80).

Configuration

server {
    listen 80;
    server_name host.test;

    location /_/ {
        proxy_pass http://127.0.0.1:80/;
        proxy_set_header Host "backend-domain.net";
    }
}

server {
    listen 80;
    server_name backend-domain.net;
    access_log /var/log/backend-domain.net.log;
    root /var/www/html;
}

server {
    listen 80 default_server;
    access_log /var/log/default-server.log;
}

Problem

I'd like to get rid of the DNS resolver and use just the IP + Host header. (I know that I can use a local DNS resolver, but I'd like to keep things simple).

For some reason I cannot proxy_pass using the local address 127.0.0.1. What I've tried:

  • [doesn't work] proxy_pass to 127.0.0.1 and set Host header
  • [doesn't work] proxy_pass to 127.0.0.1:80 and set Host header
  • [doesn't work] proxy_pass to localhost and set Host header
  • [doesn't work] all of above + changing proxy_http_version
  • [works] proxy_pass to http://backend-domain.net

All requests made to host.test are displayed in fallback host's logs. It shows valid host:

# /var/log/default-server.log:
2021/01/05 20:43:46 [error] 17860#17860: blah-blah, client: 127.0.0.1, server: 127.0.0.1, request: "GET / HTTP/1.1", host: "backend-domain.net"

Question

Why are requests being processed by default_server even though the correct Host header is set? How do I make my setup work?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With this configuration, requests get passed to backend-domain.net.

events {}         # event context have to be defined to consider config valid

http {

  error_log stderr;

  server {
      listen 8888;
      server_name host.test;
      access_log /tmp/ngxacc-host.test.log;

      location /_/ {
          proxy_pass http://127.0.0.1:8888/;
          proxy_set_header Host "backend-domain.net";
      }
  }

  server {
      listen 8888;
      server_name backend-domain.net;
      access_log /tmp/ngxacc-backend-domain.net.log;
      root /var/www/html;
  }
  server {
      listen 8888 default_server;
      access_log /tmp/ngxacc-default-server.log;
  }

}

Had to change the port number since I have an Nginx and an Apache running myself.

$ wget --header='Host: host.test' -S 'http://127.0.0.1:8888/_/'
--2021-01-06 11:41:06--  http://127.0.0.1:8888/_/
Verbinding maken met 127.0.0.1:8888... verbonden.
HTTP-verzoek is verzonden; wachten op antwoord... 
  HTTP/1.1 200 OK
  Server: nginx/1.18.0
  Date: Wed, 06 Jan 2021 10:41:06 GMT
  Content-Type: text/html
  Content-Length: 11574
  Connection: keep-alive
  Last-Modified: Wed, 12 Aug 2020 14:26:07 GMT
  ETag: "5f33fbff-2d36"
  Accept-Ranges: bytes
Lengte: 11574 (11K) [text/html]
Wordt opgeslagen als: ‘index.html.2’
# cat ngxacc-host.test.log
127.0.0.1 - - [06/Jan/2021:11:41:06 +0100] "GET /_/ HTTP/1.1" 200 11574 "-" "Wget/1.20.3 (linux-gnu)"
# cat ngxacc-backend-domain.net.log
127.0.0.1 - - [06/Jan/2021:11:41:06 +0100] "GET / HTTP/1.0" 200 11574 "-" "Wget/1.20.3 (linux-gnu)"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...