Skip to content

Commit

Permalink
build: serve static files using nginx web server in front of Node (#33)
Browse files Browse the repository at this point in the history
This improves performance under load since 1) nginx is more performant than Node’s Express-server, 2) nginx’s performance is not impacted by Node being busy with server-side rendering tasks for dynamic content.
  • Loading branch information
rasek-sls authored Jan 9, 2024
1 parent 1599fe5 commit 8952fdc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

Added

- nginx web server to serve static files in front of Node. This improves performance under load since 1) nginx is more performant than Node’s Express-server, 2) nginx’s performance is not impacted by Node being busy with server-side rendering tasks for dynamic content.


## [1.1.0] – 2023-12-28

### Added
Expand Down
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ WORKDIR /digital-edition-frontend-ng

# 2. Create intermediate build image, starting from base image.
FROM base AS build
# notify docker that static browser content will be a volume, so it can be used by nginx
# the volume should automatically be populated with the correct files from the image on first docker compose up
VOLUME [ "/digital-edition-frontend-ng/dist/app/browser" ]
# Copy all files from the source folder to the
# workdir in the container filesystem.
COPY . .
Expand Down
18 changes: 15 additions & 3 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
services:
web:
nginx:
image: docker.io/library/nginx:1.25.3
depends_on:
- web
ports:
- 2089:80
restart: unless-stopped
volumes:
- browser-static:/static
- ./nginx.conf:/etc/nginx/conf.d/default.conf
web:
build:
context: .
dockerfile: Dockerfile
Expand All @@ -9,5 +18,8 @@ services:
- "granska-api.sls.fi:172.16.2.136"
- "testa-vonwright.sls.fi:172.16.2.136"
image: ghcr.io/slsfi/digital-edition-frontend-ng:main
ports:
- 2089:4201
restart: unless-stopped
volumes:
- browser-static:/digital-edition-frontend-ng/dist/app/browser
volumes:
browser-static:
28 changes: 28 additions & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
upstream nodejs {
server web:4201;
}

server {
listen 80;

root /static;

location / {
try_files $uri sv/$uri @backend;
}

location @backend {
proxy_pass http://nodejs;

proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme;
}
}

0 comments on commit 8952fdc

Please sign in to comment.