|
1 | | -worker_processes auto; |
| 1 | +worker_processes auto; # Use all available CPU cores for maximum efficiency |
2 | 2 |
|
3 | | -# Store PID in /tmp (always writable) |
4 | | -pid /tmp/nginx.pid; |
| 3 | +# Store PID in /tmp (this directory is always writable in containers) |
| 4 | +pid /tmp/nginx.pid; # Prevent permission issues inside Docker containers |
5 | 5 |
|
6 | 6 | events { |
7 | | - worker_connections 1024; |
| 7 | + worker_connections 1024; # Max concurrent connections per worker |
8 | 8 | } |
9 | 9 |
|
10 | 10 | http { |
11 | | - include /etc/nginx/mime.types; |
12 | | - default_type application/octet-stream; |
13 | | - |
14 | | - # Disable logging to avoid permission issues |
15 | | - access_log off; |
16 | | - error_log /dev/stderr warn; |
17 | | - |
18 | | - # Optimize static file serving |
19 | | - sendfile on; |
20 | | - tcp_nopush on; |
21 | | - tcp_nodelay on; |
22 | | - keepalive_timeout 65; |
23 | | - keepalive_requests 1000; |
24 | | - |
25 | | - # Gzip compression for optimized delivery |
26 | | - gzip on; |
27 | | - gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml; |
28 | | - gzip_min_length 256; |
29 | | - gzip_vary on; |
| 11 | + include /etc/nginx/mime.types; # Load standard MIME types for correct content handling |
| 12 | + default_type application/octet-stream; # Fallback MIME type for unknown file types |
| 13 | + |
| 14 | + # Disable access logs to avoid permission issues and reduce output noise |
| 15 | + access_log off; # Turn off access logs completely |
| 16 | + error_log /dev/stderr warn; # Send error logs to stderr for visibility in Docker |
| 17 | + |
| 18 | + # Optimize static file delivery |
| 19 | + sendfile on; # Enable efficient file transfers using kernel mechanisms |
| 20 | + tcp_nopush on; # Optimize packet sizes when sending large files |
| 21 | + tcp_nodelay on; # Disable buffering for low-latency responses |
| 22 | + keepalive_timeout 65; # Time to keep connections open for reuse |
| 23 | + keepalive_requests 1000; # Allow many requests per connection before closing |
| 24 | + |
| 25 | + # Enable gzip compression for faster load times |
| 26 | + gzip on; # Turn on gzip compression |
| 27 | + gzip_types # Specify which file types should be compressed |
| 28 | + text/plain |
| 29 | + text/css |
| 30 | + application/json |
| 31 | + application/javascript |
| 32 | + text/xml |
| 33 | + application/xml |
| 34 | + application/xml+rss |
| 35 | + text/javascript |
| 36 | + image/svg+xml; |
| 37 | + gzip_min_length 256; # Only compress responses larger than 256 bytes |
| 38 | + gzip_vary on; # Add Vary: Accept-Encoding header for proxies/CDNs |
30 | 39 |
|
31 | 40 | server { |
32 | | - listen 8080; |
33 | | - server_name localhost; |
| 41 | + listen 8080; # Expose the app on port 8080 |
| 42 | + server_name localhost; # Default server name for local setups |
34 | 43 |
|
35 | | - # Root directory where React.js build files are placed |
36 | | - root /usr/share/nginx/html; |
37 | | - index index.html; |
| 44 | + # Root directory where the React build artifacts (index.html, static folder, etc.) are placed |
| 45 | + root /usr/share/nginx/html; # Serve files from this directory |
| 46 | + index index.html; # Default file served on directory access |
38 | 47 |
|
39 | | - # Serve React.js static files with proper caching |
| 48 | + # Main route handler — supports client-side routing in React |
40 | 49 | location / { |
41 | | - try_files $uri /index.html; |
| 50 | + try_files $uri /index.html; # If file is missing, fall back to index.html for SPA routing |
42 | 51 | } |
43 | 52 |
|
44 | | - # Serve static assets with long cache expiration |
| 53 | + # Cache static assets aggressively for better performance |
45 | 54 | location ~* \.(?:ico|css|js|gif|jpe?g|png|woff2?|eot|ttf|svg|map)$ { |
46 | | - expires 1y; |
47 | | - access_log off; |
48 | | - add_header Cache-Control "public, immutable"; |
| 55 | + expires 1y; # Cache for 1 year (safe for hashed filenames) |
| 56 | + access_log off; # Disable logs for static files |
| 57 | + add_header Cache-Control "public, immutable"; # Tell browsers that files won't change |
49 | 58 | } |
50 | 59 |
|
51 | | - # Handle React.js client-side routing |
| 60 | + # Additional static assets path (React’s /static/ directory) |
52 | 61 | location /static/ { |
53 | | - expires 1y; |
54 | | - add_header Cache-Control "public, immutable"; |
| 62 | + expires 1y; # Cache assets from /static for 1 year |
| 63 | + add_header Cache-Control "public, immutable"; # Ensure long-term caching |
55 | 64 | } |
56 | 65 | } |
57 | 66 | } |
0 commit comments