Enhance Dockerfile and nginx configuration for dynamic subfolder handling and improved asset caching

This commit is contained in:
Tudor Stanciu 2025-09-29 01:17:07 +03:00
parent 0bfbe6e9f2
commit caf38d468f
2 changed files with 14 additions and 6 deletions

View File

@ -35,8 +35,14 @@ RUN if [ -n "$APP_SUBFOLDER" ]; then mkdir -p /usr/share/nginx/html/$APP_SUBFOLD
# Copy built application # Copy built application
COPY --from=builder /app/build /usr/share/nginx/html/${APP_SUBFOLDER} COPY --from=builder /app/build /usr/share/nginx/html/${APP_SUBFOLDER}
# Copy nginx configuration # Copy and configure nginx
COPY nginx.conf /etc/nginx/nginx.conf 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 # Add environment variables
ENV Author="Tudor Stanciu" ENV Author="Tudor Stanciu"

View File

@ -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; 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 # 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; expires 1y;
add_header Cache-Control "public, immutable"; 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 / { 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; try_files $uri $uri/ /index.html;
} }