0

With the following nginx config excerpt:

map $server_name $server_listen_port {
    default 443;
    localhost. 80;
    localhost  80;
}

server {
    server_name "${ENVIRONMENT_SERVER_NAME}.";
    listen $server_listen_port http2 reuseport;
    # ...
}

I get the following error when validating the configuration with ENVIRONMENT_SERVER_NAME set in the environment to "localhost":

nginx: [emerg] host not found in "$server_listen_port" of the "listen" directive in /etc/nginx/conf.d/99_main.conf:38
nginx: configuration file /etc/nginx/nginx.conf test failed

I am trying to decipher what this error message means exactly. Does it mean that the listen statement is invalid because it is missing a hostname?

Or is it perhaps trying to tell me there is no value in the map for the host? I would expect the "default" to be invoked in that case.

I have tried all manner of variations without really getting anywhere.

What is causing this error, and how can I fix it?

  • Many (if not most) Nginx directives do not accept Nginx variables. So it is trying to match the literal string "$server_listen_port" with one of the [accepted formats](http://nginx.org/en/docs/http/ngx_http_core_module.html#listen) for the first parameter. – Richard Smith Aug 11 '23 at 08:08

1 Answers1

0

This should be a comment (but space limited).

You are trying to run HTTP/2 without TLS. While that's theoretically possible, most clients don't support it. I don't know if nginx supports it. It would be easy to test if nginx will at least parse the config and startup using a static configuration.

I am struggling to imagine the value your method of multiplexing sites adds. With TLS, webservers will select a backend based on the connected port and HTTP headers. For HTTPS it is selected based on the port and SNI. You seem to be trying to avoid having multiple server definitions by deferring resolution of the listening port until you have a request to process - but you're not going to get any requests until you have a port open and listening.

symcbean
  • 21,009
  • 1
  • 31
  • 52