E
E
EmachinesDIMA2022-03-02 11:20:17
Nginx
EmachinesDIMA, 2022-03-02 11:20:17

How to beat passing variables to nginx config from ConfigMap with env variables to set upstream forwarding between services?

Probably too smart with the title, but I'll try to fix it in the description.

There are 2 services. One frontend with react+nginx that handles non-root requests a la "domain.ru/cabinet", there is a second worpress service with php-fpm that handles root requests.

Everything works in containers and was launched with docker-compose. Separation of the monolith and communication between containers was defined as an nginx setting:

set $front_react frontend_${BRANCH_NAME}_react_nginx;

    location ~ \.html$ {
        proxy_pass http://$front_react;
    }


where frontend_${BRANCH_NAME}_react_nginx is the name of the container. Same for wordpress.

So I created the cuber objects, but when I start nginx I get an error: BRANCH_NAME is not known
nginx: [emerg] unknown "branch_name" variable

, despite the fact that I defined it in ConfigMap and mounted it as a volume in Deployment.

How to correctly pass a variable to the nginx config that I can pass on build :
1) pass to Dockerfile ---
COPY default.conf /etc/nginx/conf.d/default.conf
2) write to configmap as data ---
data:
  BRANCH_NAME: pp-3223
  default.conf: |
    server { ....
    }


and avoid problems with passing variables?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Karabanov, 2022-03-02
@karabanov

Yes, the variable does need to be defined. You can define it by means of changing it server_nameto a regular expression with named groups:

# это пример регулярку надо поменять под свои нужды
server_name ~(?<branch_name>[^\.]*)\.(?<tld>[^\.]*)$;

location ~ \.html$ {
    proxy_pass http://frontend_${branch_name}_react_nginx;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question