Skip to content

Commit a01836b

Browse files
committed
Improve Docker Nginx deployment: Add IPv6, HTTP/2, SSL support, gzip compression and image caching
Also adds auto certificate generation for SSL in case no existing certificates are found.
1 parent 77c84a5 commit a01836b

File tree

3 files changed

+80
-6
lines changed

3 files changed

+80
-6
lines changed
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
# Stage 1: Serve the website with nginx
2-
FROM nginx:stable-alpine
2+
FROM nginx:mainline-alpine
33
# Copy the built website files to the nginx serve directory
44
COPY dist/dockerfiles-website/browser /usr/share/nginx/html
55
# Replace the default nginx configuration with our custom configuration (with proxy enabled)
66
COPY docker/deploy-local-build/default.conf /etc/nginx/conf.d/default.conf
7+
# Create directory for SSL certificates
8+
RUN mkdir -p /etc/nginx/ssl
79
# Expose ports
8-
EXPOSE 80
10+
EXPOSE 80 443
911

10-
# Start nginx
11-
CMD ["sh", "-c", "nginx -g 'daemon off;'"]
12+
# Start nginx with a script that generates certificates if needed
13+
RUN apk add openssl
14+
COPY docker/deploy-local-build/start.sh /start.sh
15+
RUN chmod +x /start.sh
16+
CMD ["/start.sh"]

docker/deploy-local-build/default.conf

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,53 @@
22
proxy_cache_path /var/cache/nginx/dockerhub_cache levels=1:2 keys_zone=dockerhub_cache:10m max_size=1g inactive=60m use_temp_path=off;
33

44
server {
5-
listen 80;
5+
listen 80; # IPv4
6+
listen [::]:80; # IPv6
7+
# HTTP/2 support (requires SSL)
8+
http2 on;
9+
listen 443 ssl;
10+
listen [::]:443 ssl;
611
server_name localhost;
712

13+
# SSL configuration (required for HTTP/2)
14+
ssl_certificate /etc/nginx/ssl/nginx.crt;
15+
ssl_certificate_key /etc/nginx/ssl/nginx.key;
16+
ssl_protocols TLSv1.3;
17+
ssl_session_cache shared:SSL:10m;
18+
ssl_session_timeout 10m;
19+
20+
# Security headers
21+
add_header X-Content-Type-Options nosniff;
22+
add_header X-Frame-Options SAMEORIGIN;
23+
add_header X-XSS-Protection "1; mode=block";
24+
add_header Referrer-Policy no-referrer-when-downgrade;
25+
26+
# Compression settings
27+
gzip on;
28+
gzip_comp_level 5;
29+
gzip_min_length 4096;
30+
gzip_proxied any;
31+
gzip_vary on;
32+
gzip_types
33+
application/javascript
34+
application/json
35+
application/xml
36+
text/css
37+
text/javascript
38+
text/plain
39+
text/xml;
40+
841
# Default location block for serving static files
942
location / {
1043
root /usr/share/nginx/html;
1144
index index.html index.htm;
1245
try_files $uri $uri/ /index.html;
46+
47+
# Cache control for static assets
48+
location ~* \.(jpg|jpeg|png|webp|gif|ico|svg)$ {
49+
expires 1d;
50+
add_header Cache-Control "public, no-transform, must-revalidate";
51+
}
1352
}
1453

1554
# Error pages configuration
@@ -19,7 +58,7 @@ server {
1958
}
2059

2160
# Proxy configuration for Docker Hub API requests with caching.
22-
location /v2/namespaces/pegi3s/repositories/ {
61+
location /v2/namespaces/pegi3s/repositories {
2362
resolver 1.1.1.1; # DNS resolver for domain name lookup.
2463
set $upstream_endpoint https://hub.docker.com; # The upstream service to proxy requests to.
2564

@@ -28,9 +67,18 @@ server {
2867
proxy_cache_valid 200 302 10m; # Cache 200 and 302 responses for 10 minutes
2968
proxy_cache_valid 404 1m; # Cache 404 responses for 1 minute
3069
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
70+
proxy_cache_lock on; # Prevents multiple clients from requesting the same uncached item.
71+
proxy_cache_background_update on; # Updates cache in background without blocking clients.
3172
add_header X-Proxy-Cache $upstream_cache_status; # Adds a header to the response with the cache status.
3273

3374
# Pass the request to the upstream server
3475
proxy_pass $upstream_endpoint$request_uri;
3576
}
77+
78+
# Deny access to hidden files
79+
location ~ /\. {
80+
deny all;
81+
access_log off;
82+
log_not_found off;
83+
}
3684
}

docker/deploy-local-build/start.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/sh
2+
set -e
3+
4+
# Check if certificates exist, if not generate self-signed ones
5+
if [ ! -f /etc/nginx/ssl/nginx.crt ] || [ ! -f /etc/nginx/ssl/nginx.key ]; then
6+
echo "SSL certificates not found, generating self-signed certificates..."
7+
echo "Using $(openssl version)"
8+
mkdir -p /etc/nginx/ssl
9+
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
10+
-keyout /etc/nginx/ssl/nginx.key \
11+
-out /etc/nginx/ssl/nginx.crt \
12+
-subj "/CN=localhost" \
13+
-addext "subjectAltName=DNS:localhost,IP:127.0.0.1"
14+
echo "Self-signed certificates generated."
15+
else
16+
echo "Using existing SSL certificates."
17+
fi
18+
19+
# Start nginx
20+
echo "Starting Nginx..."
21+
exec nginx -g 'daemon off;'

0 commit comments

Comments
 (0)