can nginx set variable from file?

Solution 1:

I don't think you can "read" files into variables by default.

There are third party modules which may read a value of a location (which itself could refer to files) into a variable, for example nginx-eval-module.

But for your use case I think is overkill (and you will depend on a third party module).

What NGINX can do is include code snippets in your configuration.

For example, you can do:

# nginx configuration
server {
    include /www/test.txt
    ...
}

# /www/test.txt
server_name
    example.com
    example.net
    ...
;

and so on.

If test.txt is an automatically generated file, it should be pretty easy to modify the generation script to include server_name at the beginning and ; at the end.

If it's modified manually, well then it's a no brainer.