skip to Main Content

I am new to nginx and trying to understand what is going on here. I have a docker compose file that starts up a nginx container like so:

  proxy:
    image: nginx:alpine
    container_name: proxy
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./proxy/default.conf:/etc/nginx/conf.d/default.conf

Which copies my default.conf into the nginx container, which looks like this:

server {
    listen       80;
    listen  [::]:80;
    server_name localhost testthis;

    return 301 https://www.google.com$request_uri;
}

So if I run curl -I http://localhost, I see google.com as expected

HTTP/1.1 301 Moved Permanently
Server: nginx/1.21.6
Date: Fri, 25 Feb 2022 06:39:39 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: https://www.google.com/

But if I run curl -I http://testthis, I get this response:

curl: (6) Could not resolve host: testthis

Why is this happening if the server names are on the same server block? Eventually I am wanting to set up a custom domain and subdomains to forward requests to specific localhost ports per app but not understanding how this works too well.

2

Answers


  1. curl --connect-to testthis:80:127.0.0.1:80 http://google.com should do the trick

    Login or Signup to reply.
  2. curl -I http://localhost works because localhost, by default, resolves to an IP from your machine (127.0.0.1), not because it’s listed in your nginx’s default.conf. And Docker configures your machine to forward traffic on port 80 and 443 (the ones used for HTTP and HTTPS) to that container.

    The server_name directive in nginx’s configuration makes it recognize requests with that DNS in the request’s Host: header. It does not advertise that as a name for your network.

    For that to work, you need to make your computer recognize testthis as a name for your computer. On Linux, edit /etc/hosts and add this line:

    127.0.0.1 testthis
    

    On Windows, I don’t know, but you can certainly search for "windows hosts file" and get a similar method.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search