Skip to content
This repository has been archived by the owner on Jun 26, 2024. It is now read-only.

Commit

Permalink
feat: rework deployment, serve static assets with nginx
Browse files Browse the repository at this point in the history
  • Loading branch information
vhf committed Oct 28, 2019
1 parent d95f8ae commit 9cf1710
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 326 deletions.
37 changes: 29 additions & 8 deletions docker-app-prod/Dockerfile.web
Original file line number Diff line number Diff line change
@@ -1,26 +1,47 @@
FROM node:10-stretch

# Stage 0, "build-stage", based on Node.js, to build and compile the frontend
FROM node:10-stretch as build-stage
RUN npm install --global npm@6.7.0

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
ENV NODE_ENV production

COPY . /usr/src/app/
RUN node -v && npm ci
RUN node -v && npm -v && npm ci --production

ENV BUILDING_WITHOUT_PG_ACCESS=yes
# ENV BUILDING_WITHOUT_PG_ACCESS yes

# Build app
RUN npm run build
# RUN npm run build
#
# # Stage 1, based on Nginx, to have only the compiled app, ready for production with Nginx
FROM nginx:1.15

COPY --from=build-stage /usr/src/app /usr/src/app


ENV NODE_PATH=/usr/local/include/node:/usr/local/lib/node_modules:/usr/share/javascript

COPY --from=build-stage /usr/share/javascript /usr/share/javascript
COPY --from=build-stage /usr/local/include/node /usr/local/include/node
COPY --from=build-stage /usr/local/lib/node_modules /usr/local/lib/node_modules
COPY --from=build-stage /usr/local/bin/node /usr/local/bin/node

RUN ln -s /usr/local/lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm

WORKDIR /usr/src/app

ENV BUILDING_WITHOUT_PG_ACCESS=
RUN cp ./docker-app-prod/nginx.conf /etc/nginx/nginx.conf
RUN cp ./docker-app-prod/editor.conf /etc/nginx/conf.d/editor.conf
RUN rm /etc/nginx/conf.d/default.conf

ENV NODE_ENV production
ENV HOST 0.0.0.0
EXPOSE 3000
EXPOSE 8000 3000

# entrypoint
RUN cp docker-app-prod/entrypoint.web.sh /usr/src/app/entrypoint.web.sh
RUN cp /usr/src/app/docker-app-prod/entrypoint.web.sh /usr/src/app/entrypoint.web.sh
RUN chmod 755 /usr/src/app/entrypoint.web.sh

HEALTHCHECK --interval=10s --timeout=20s --start-period=60s --retries=20 CMD node ./healthcheck.js
Expand Down
33 changes: 33 additions & 0 deletions docker-app-prod/editor.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
map $sent_http_content_type $expires {
"text/html" epoch;
"text/html; charset=utf-8" epoch;
default off;
}

server {
listen 8000 default_server;

gzip on;
gzip_types text/plain application/xml text/css application/javascript;
gzip_min_length 1000;

location /_nuxt/ {
alias /usr/src/app/.nuxt/dist/client/;
gzip_static on;
expires max;
add_header Cache-Control public;
}

location / {
expires $expires;

proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 1m;
proxy_connect_timeout 1m;
proxy_pass http://127.0.0.1:3000;
}
}
25 changes: 23 additions & 2 deletions docker-app-prod/entrypoint.web.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,28 @@ set -euo pipefail
node setup/migrate.js

if [ ! -f .built ]; then
npm run build -- --modern=server && touch .built
npm run build -- --modern=server

touch .built
fi

npm run start -- --modern=server
#!/bin/bash
wait_for_pids()
{
for (( i = 1; i <= $#; i++ )) do
wait -n $@
status=$?
echo "received status: "$status
if [ $status -ne 0 ] && [ $status -ne 127 ]; then
exit 1
fi
done
}

npm run start &
pid1=$!

nginx -g 'daemon off;' &
pid2=$!

wait_for_pids $pid2 $pid1
64 changes: 64 additions & 0 deletions docker-app-prod/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
user www-data;
worker_processes 8;
pid /run/nginx.pid;

events {
worker_connections 768;
multi_accept on;
}

http {

##
# Basic Settings
##

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;

server_names_hash_bucket_size 64;
server_name_in_redirect on;

include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format custom '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_host" "$proxy_add_x_forwarded_for"';

access_log /var/log/nginx/access.log custom;
error_log /var/log/nginx/error.log;
access_log /dev/stdout custom;
error_log /dev/stdout info;


gzip on;
gzip_disable msie6;

gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;

gzip_types
# text/html is always compressed by HttpGzipModule
text/css
text/javascript
text/xml
text/plain
text/x-component
application/javascript
application/x-javascript
application/json
application/xml
application/rss+xml
application/atom+xml
font/truetype
font/opentype
application/vnd.ms-fontobject
image/svg+xml;

include /etc/nginx/conf.d/*.conf;
}
Loading

0 comments on commit 9cf1710

Please sign in to comment.