nginx.conf 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. server {
  2. listen 80;
  3. server_name localhost;
  4. root /usr/share/nginx/html;
  5. index index.html;
  6. # Enable gzip compression
  7. gzip on;
  8. gzip_vary on;
  9. gzip_min_length 1024;
  10. gzip_types text/plain text/css text/xml text/javascript application/javascript application/xml+rss application/json;
  11. # Handle Vue.js SPA routing
  12. location / {
  13. try_files $uri $uri/ /index.html;
  14. }
  15. # API proxy to backend with service discovery
  16. location /api/ {
  17. # Use resolver for Docker DNS
  18. resolver 127.0.0.11 valid=30s;
  19. set $backend_endpoint http://backend:80;
  20. # Use HTTPS for backend if frontend is HTTPS
  21. if ($scheme = https) {
  22. set $backend_endpoint http://backend:80;
  23. }
  24. proxy_pass $backend_endpoint;
  25. proxy_set_header Host $host;
  26. proxy_set_header X-Real-IP $remote_addr;
  27. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  28. proxy_set_header X-Forwarded-Proto $scheme;
  29. # Error handling for backend service
  30. proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
  31. proxy_connect_timeout 5s;
  32. proxy_send_timeout 10s;
  33. proxy_read_timeout 10s;
  34. # Handle CORS headers
  35. add_header Access-Control-Allow-Origin "*" always;
  36. add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS" always;
  37. add_header Access-Control-Allow-Headers "Content-Type, Authorization, X-Requested-With" always;
  38. # Handle preflight requests
  39. if ($request_method = 'OPTIONS') {
  40. add_header Access-Control-Allow-Origin "*";
  41. add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS";
  42. add_header Access-Control-Allow-Headers "Content-Type, Authorization, X-Requested-With";
  43. add_header Content-Length 0;
  44. add_header Content-Type text/plain;
  45. return 204;
  46. }
  47. }
  48. # Cache static assets
  49. location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
  50. expires 1y;
  51. add_header Cache-Control "public, immutable";
  52. }
  53. # Security headers
  54. add_header X-Frame-Options "SAMEORIGIN" always;
  55. add_header X-Content-Type-Options "nosniff" always;
  56. add_header X-XSS-Protection "1; mode=block" always;
  57. add_header Referrer-Policy "strict-origin-when-cross-origin" always;
  58. # Error pages
  59. error_page 404 /index.html;
  60. error_page 500 502 503 504 /50x.html;
  61. location = /50x.html {
  62. root /usr/share/nginx/html;
  63. }
  64. }