nginx.conf 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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
  16. location /api/ {
  17. proxy_pass http://backend:80;
  18. proxy_set_header Host $host;
  19. proxy_set_header X-Real-IP $remote_addr;
  20. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  21. proxy_set_header X-Forwarded-Proto $scheme;
  22. # Handle CORS headers
  23. add_header Access-Control-Allow-Origin "*" always;
  24. add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS" always;
  25. add_header Access-Control-Allow-Headers "Content-Type, Authorization, X-Requested-With" always;
  26. # Handle preflight requests
  27. if ($request_method = 'OPTIONS') {
  28. add_header Access-Control-Allow-Origin "*";
  29. add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS";
  30. add_header Access-Control-Allow-Headers "Content-Type, Authorization, X-Requested-With";
  31. add_header Content-Length 0;
  32. add_header Content-Type text/plain;
  33. return 204;
  34. }
  35. }
  36. # Cache static assets
  37. location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
  38. expires 1y;
  39. add_header Cache-Control "public, immutable";
  40. }
  41. # Security headers
  42. add_header X-Frame-Options "SAMEORIGIN" always;
  43. add_header X-Content-Type-Options "nosniff" always;
  44. add_header X-XSS-Protection "1; mode=block" always;
  45. add_header Referrer-Policy "strict-origin-when-cross-origin" always;
  46. # Error pages
  47. error_page 404 /index.html;
  48. error_page 500 502 503 504 /50x.html;
  49. location = /50x.html {
  50. root /usr/share/nginx/html;
  51. }
  52. }