Score:0

host not found in "$server_listen_port" of the "listen" directive

at flag

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?

Richard Smith avatar
jp flag
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.
Score:0
ws flag

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.

Lennart Rolland avatar
at flag
Now that you mention it, of course you are right! That is very useful. Major brainfart on my part!
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.