Skip to content

Instantly share code, notes, and snippets.

@rosskevin
Created October 1, 2018 19:58
Show Gist options
  • Select an option

  • Save rosskevin/da27417ecee0810d1a8e4955166ff061 to your computer and use it in GitHub Desktop.

Select an option

Save rosskevin/da27417ecee0810d1a8e4955166ff061 to your computer and use it in GitHub Desktop.

Revisions

  1. rosskevin created this gist Oct 1, 2018.
    20 changes: 20 additions & 0 deletions Dockerfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    FROM nginx:alpine

    # https://thepracticalsysadmin.com/templated-nginx-configuration-with-bash-and-docker/
    ENV LISTEN_PORT=80 \
    NGINX_ENV=production \
    SERVER_NAME=_ \
    RESOLVER=8.8.8.8 \
    UPSTREAM_API=api:3000 \
    UPSTREAM_API_PROTO=http \
    WORKDIR=/www \
    ESC='$'

    WORKDIR ${WORKDIR}
    COPY . .

    COPY nginx.production.template /etc/nginx/nginx.production.template
    COPY nginx.development.template /etc/nginx/nginx.development.template

    EXPOSE ${LISTEN_PORT}
    CMD /bin/sh -c "envsubst < ${WORKDIR}/nginx.${NGINX_ENV}.template > /etc/nginx/nginx.conf && nginx -g 'daemon off;' || cat /etc/nginx/nginx.conf"
    86 changes: 86 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,86 @@
    events {}

    http {
    error_log stderr;
    access_log /dev/stdout;

    upstream upstream_api {
    server ${UPSTREAM_API};
    }

    upstream upstream_webpack {
    server ${UPSTREAM_WEBPACK};
    }

    server {
    listen ${LISTEN_PORT};
    server_name ${SERVER_NAME};
    resolver ${RESOLVER};

    # gzip configuration
    gzip on;
    gzip_disable "msie6";
    gzip_comp_level 6;
    gzip_min_length 1100;
    gzip_buffers 16 8k;
    gzip_proxied any;
    gzip_types
    # text/html is always compressed by HttpGzipModule
    text/css
    text/javascript
    text/xml
    text/plain
    text/x-component
    application/javascript
    application/json
    application/xml
    application/rss+xml
    font/truetype
    font/opentype
    application/vnd.ms-fontobject
    image/svg+xml;

    # Allow injecting extra configuration into the server block
    ${SERVER_EXTRA_CONF}

    # define the public application root
    root ${WORKDIR}/public;
    index index.html;

    # deny requests for files that should never be accessed
    location ~ /\. {
    deny all;
    }

    location ~* ^.+\.(rb|log)$ {
    deny all;
    }

    # send non-static file requests to the app server
    location ~* \/(_live|_ready|_before|_travel_to|_travel_back|graphql|validators|notifications) {
    try_files ${ESC}uri @api;
    expires -1;
    break;
    }

    # serve everything else via webpack
    location / {
    rewrite ^(.*)$ / break;
    try_files ${ESC}uri @webpack;
    expires -1;
    }

    location @api {
    proxy_set_header X-Real-IP ${ESC}remote_addr;
    proxy_set_header X-Forwarded-For ${ESC}proxy_add_x_forwarded_for;
    proxy_set_header Host ${ESC}http_host;
    proxy_redirect off;
    proxy_pass ${UPSTREAM_API_PROTO}://upstream_api;
    }

    location @webpack {
    proxy_redirect off;
    proxy_pass ${UPSTREAM_WEBPACK_PROTO}://upstream_webpack;
    }
    }
    }