diff --git a/Dockerfile b/Dockerfile index 96e299b..d46e7ce 100644 --- a/Dockerfile +++ b/Dockerfile @@ -35,8 +35,14 @@ RUN if [ -n "$APP_SUBFOLDER" ]; then mkdir -p /usr/share/nginx/html/$APP_SUBFOLD # Copy built application COPY --from=builder /app/build /usr/share/nginx/html/${APP_SUBFOLDER} -# Copy nginx configuration -COPY nginx.conf /etc/nginx/nginx.conf +# Copy and configure nginx +COPY nginx.conf /tmp/nginx.conf.template +# Generate nginx.conf with correct subfolder path +RUN if [ -n "$APP_SUBFOLDER" ]; then \ + sed "s|# SUBFOLDER_PLACEHOLDER|location /$APP_SUBFOLDER/ { try_files \$uri \$uri/ /$APP_SUBFOLDER/index.html; }|g" /tmp/nginx.conf.template > /etc/nginx/nginx.conf; \ + else \ + sed "s|# SUBFOLDER_PLACEHOLDER||g" /tmp/nginx.conf.template > /etc/nginx/nginx.conf; \ + fi # Add environment variables ENV Author="Tudor Stanciu" diff --git a/nginx.conf b/nginx.conf index fbb061a..07c5bc1 100644 --- a/nginx.conf +++ b/nginx.conf @@ -46,15 +46,17 @@ http { add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; img-src 'self' data: blob: https://cdn.jsdelivr.net; font-src 'self' https://fonts.gstatic.com; connect-src 'self' http: https:;" always; # Static assets caching - works for any subfolder or root - location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2)$ { + location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|json)$ { expires 1y; add_header Cache-Control "public, immutable"; + try_files $uri =404; } - # SPA fallback - handle client-side routing for React apps + # Dynamic subfolder configuration (generated at build time) + # SUBFOLDER_PLACEHOLDER + + # SPA fallback for root level routes location / { - # First try to serve request as file, then as directory - # If both fail, serve index.html for SPA routing try_files $uri $uri/ /index.html; }