Skip to content

Instantly share code, notes, and snippets.

@msergo
Forked from xaviervia/nginx-environment.md
Created September 1, 2018 16:21
Show Gist options
  • Select an option

  • Save msergo/6d1b8eb767cfbaec46c807eae1428d15 to your computer and use it in GitHub Desktop.

Select an option

Save msergo/6d1b8eb767cfbaec46c807eae1428d15 to your computer and use it in GitHub Desktop.

Revisions

  1. @xaviervia xaviervia revised this gist Oct 10, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nginx-environment.md
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,7 @@ The solution
    So this is what I did: I created the base configuration file, named it `proxy.conf` and setup docker to add it to the `conf.d` directory while building the image. The command is:

    ```
    ADD proxy.cond /etc/nginx/conf.d/proxy.conf
    ADD proxy.conf /etc/nginx/conf.d/proxy.conf
    ```

    In the `proxy.conf`, I omitted the `upstream` configuration, leaving that for later. I created another file, a `run.sh` file and added it to the image using the Dockerfile. The file was as follows:
  2. @xaviervia xaviervia created this gist May 13, 2014.
    50 changes: 50 additions & 0 deletions nginx-environment.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    How to add environment variables to nginx.conf
    ===============================================

    This is the hack approach to adding environment variables to the nginx configuration files. As with most Google results for this search, the reason is Docker.

    The setup
    ---------

    I intended to deploy two [Docker](http://docker.io) containers.

    1. An instance of an HTTP server providing a REST service (in my case, a JBoss application server running a Spring application)
    2. An nginx that provides a proxy to the REST service.

    Why would I want to use a proxy? Load balancing, caching, firewalling... in my case, it was that the original application did not provide [CORS](http://enable-cors.org/) and the nginx configuration does. You can see the nginx configuration in the proxy.conf file.

    The problem
    -----------

    Both the port and the IP address of the JBoss instance are assigned by Docker dynamically. [Docker provides a linking feature](http://docs.docker.io/use/working_with_links_names/#links-service-discovery-for-docker) for service discovery that allows Docker containers to know the IP and port of services running on a specific container, but the quirk is that it relies on environment variables.

    I'm not actually certain that adding environment variables to an nginx configuration file is impossible, but its at the very least very hard.

    The solution
    ------------

    So this is what I did: I created the base configuration file, named it `proxy.conf` and setup docker to add it to the `conf.d` directory while building the image. The command is:

    ```
    ADD proxy.cond /etc/nginx/conf.d/proxy.conf
    ```

    In the `proxy.conf`, I omitted the `upstream` configuration, leaving that for later. I created another file, a `run.sh` file and added it to the image using the Dockerfile. The file was as follows:

    ```bash
    #!/bin/sh

    (echo "upstream theservice { server $JBOSS_PORT_8080_TCP_ADDR:$JBOSS_PORT_8080_TCP_PORT; }" && cat /etc/nginx/conf.d/proxy.conf) > proxy.conf.new
    mv proxy.conf.new /etc/nginx/conf.d/proxy.conf
    service nginx start
    ```

    What does is, it prepends the line with the upstream configuration.

    Finally, I run the nginx from the `run.sh` script. The Dockerfile command:

    ```
    CMD bash run.sh
    ```

    The trick is that since the container is initialized like that, the configuration file does not get permanently written and the configuration is updated accordingly.
    32 changes: 32 additions & 0 deletions proxy.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    # This part is omitted
    # upstream theservice {
    # server 127.0.0.1:8082;
    # }

    server {
    listen 80;

    location / {

    access_log off;
    proxy_pass http://theservice;

    add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Credentials true;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    if ($request_method = OPTIONS ) {
    add_header Access-Control-Allow-Origin "*";
    add_header Access-Control-Allow-Methods "GET,POST,OPTIONS,PUT,DELETE,PATCH";
    add_header Access-Control-Allow-Headers 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,Keep-Alive,X-Requested-With,If-Modified-Since';
    add_header Access-Control-Allow-Credentials "true";
    add_header 'Access-Control-Max-Age' 1728000;
    add_header Content-Length 0;
    add_header Content-Type text/plain;
    add_header Cache-Control 'max-age=0';
    return 204;
    }
    }
    }
    11 changes: 11 additions & 0 deletions run.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    #!/bin/sh

    # Preprend the upstream configuration
    (echo "upstream theservice { server $JBOSS_PORT_8080_TCP_ADDR:$JBOSS_PORT_8080_TCP_PORT; }" && cat /etc/nginx/conf.d/proxy.conf) > proxy.conf.new
    mv proxy.conf.new /etc/nginx/conf.d/proxy.conf

    # Log the resulting configuration file
    cat /etc/nginx/conf.d/proxy.conf

    # Start nginx
    service nginx start