Skip to content

Instantly share code, notes, and snippets.

@ezhov-da
Forked from rosskevin/Dockerfile
Created October 13, 2021 08:12
Show Gist options
  • Save ezhov-da/b20c829609d789fb115f4beeb3c9f86e to your computer and use it in GitHub Desktop.
Save ezhov-da/b20c829609d789fb115f4beeb3c9f86e to your computer and use it in GitHub Desktop.
nginx envsubst escape $
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"
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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment