nginx.conf
conf2 kB55 linesIn assignment_1_solution.zip
assignment_1_solution.zipfrontendnginx.conf
# nginx has two jobs here:
# 1. serve the compiled React app (static files)
# 2. act as a reverse proxy: forward /api/* to the backend container
# The browser therefore talks to ONE origin (http://<host>/), and the backend
# and database never need a public port.
server {
listen 8080;
server_name _;
server_tokens off; # do not advertise the nginx version
root /usr/share/nginx/html;
index index.html;
gzip on;
gzip_types text/css application/javascript application/json image/svg+xml;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "DENY" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
# Container healthcheck (does not depend on the backend).
location = /healthz {
access_log off;
default_type text/plain;
return 200 "ok\n";
}
# Reverse proxy. "backend" is the compose service name, resolved by
# Docker's internal DNS on the compose network.
location /api/ {
proxy_pass http://backend:3000;
proxy_http_version 1.1;
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;
}
# Gotcha: any add_header inside a location block cancels ALL add_header
# lines inherited from server{} (the security headers above would vanish).
# So caching is set with `expires`, which does not have that problem.
# Vite puts content-hashed files in /assets: safe to cache for a year.
location /assets/ {
expires 1y;
}
# Single-page app: unknown paths (/events/3, /login...) return index.html
# and React Router renders the right page in the browser.
location / {
try_files $uri /index.html;
expires -1; # always revalidate index.html
}
}