P
P
PendalF892021-01-06 13:54:16
Nginx
PendalF89, 2021-01-06 13:54:16

Is it possible to set a variable in Nginx without an IF block?

I need to get the subdomain for further use in the config. Now I'm doing this:

set $subdomain "";
if ($host ~ "^(.*)\.site\.com\.local$") {
    set $subdomain $1;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2021-01-06
@PendalF89

nginx.org/ru/docs/http/ngx_http_map_module.html

map $host $subdomain {
    default "";
    ~^(.*)\.site\.com\.local$  $1;
}

Or use a regular expression with named selection as the server name.
nginx.org/ru/docs/http/ngx_http_core_module.html#s...
server_name ~^(?<subdomain>.*)\.site\.com\.local$;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question