Nginx static webpage and Docker with env variable url Nginx static webpage and Docker with env variable url nginx nginx

Nginx static webpage and Docker with env variable url


If I understood correctly you need a dynamic web page containing a script that makes HTTP calls to a configurable URL.That can be achieved using Server Side Includes. with nginx. The webpage could include a configuration file that is created during the initialization of the container.

Create the file into the nginx document root when the image is first started. For example:

docker run -e URL=http://stackoverflow.com --entrypoint="/bin/sh" nginx  -c 'echo ${URL} > /usr/share/nginx/html/url_config & exec nginx -g "daemon off;"'

For a real world scenario create a custom image based on nginx and override the entrypoint. The entrypoint creates the configuration file with the URL environment variable and eventually launches nginx in foreground.

Include the configuration file in the web page using the #include SSI directive

<script>...const http = new XMLHttpRequest();const url = '<!--#include file="url_config" -->';http.open("GET", url);http.send();...

Configure nginx to process SSI by adding the ssi on; directive

http {    ssi on;    ...}

Hope it helps.


Yes, it is possible,But you can do this using docker network if you want some routing. but you can do anything with Docker. According you question title you want to use ENV in URL in the Nginx Configuration. Here is the way to do that.

  • Set Environment in Docker run command or Dockerfile
  • Use that Environment variable in your Nginx config file
    docker run --name nginx -e APP_HOST_NAME="myexample.com" -e APP_HOST_PORT="3000" yourimage_nginx

Now you can use these URL in your nginx configuration.

    server {        set_by_lua $curr_server_name 'return os.getenv("APP_HOST_NAME")';        set_by_lua $curr_server_port 'return os.getenv("APP_HOST_PORT")';        proxy_pass         http://$curr_server_name/index.html;    }

To deal with ENV with nginx you can check lua-nginx-module