2

I have nginx container where I want to forward an api call to another server. I want to pass in environment variables from Docker Compose to be able to change sites, without rebuilding the Docker Image.

My setup:

#docker-compose.yml

services:
  web:
    image: myimage
    environment:
      - SITE_URL: my-website.com
      - API_URL: api-site.com

In nginx.conf I use perl_set from "modules/ngx_http_perl_module.so" to load the environment variables.

#default.conf

server {
    listen 80;
    listen [::]:80;
    server_name $site_url;

    location / {
       return 301 https://$host$request_uri;
    }

}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name $site_url;
    charset utf-8;

    location / {
        add_header Cache-Control 'no-store';
        add_header Cache-Control 'no-cache';
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri /index.html;
    }

    location /api/ {
        resolver 127.0.0.11 [::1];
        proxy_pass https://$api_url/api/;
    }

I tried to follow this question:

If I do:

location /api/ {
    resolver 127.0.0.11 [::1];
    set $api_test  https://api-site.com$uri$is_args$args;
    proxy_pass $api_test;
}

It works and I can use $api_test as a variable in proxy_pass. But I'm not able to use api_url even if I do https://$api_url$uri$is_args$args

Any suggestions would be much appreciated!

ChrKong
  • 21
  • 1
  • 2

0 Answers0