Skip to content

SPA + API 模板

适合前端静态资源由 Nginx 托管,接口通过 /api/ 转发到后端服务。

替换:

  • example.com
  • /var/www/app/dist
  • 127.0.0.1:3000
nginx
server {
    listen 80;
    listen [::]:80;
    server_name example.com;

    root /var/www/app/dist;
    index index.html;

    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;

    client_max_body_size 20m;

    location /api/ {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        proxy_connect_timeout 30s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;
    }

    location / {
        try_files $uri $uri/ /index.html;
        add_header Cache-Control "no-store";
    }

    location ~* ^/(?:assets|static)/ {
        try_files $uri =404;
        access_log off;
        add_header Cache-Control "public, max-age=31536000, immutable";
    }
}

基于 MIT 许可发布