diff --git a/nginx/conf.d/default.conf b/nginx/conf.d/default.conf index 8791706..8e280ee 100644 --- a/nginx/conf.d/default.conf +++ b/nginx/conf.d/default.conf @@ -32,8 +32,8 @@ server { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } - - # API 路由 - 所有 /api/ 开头的请求转发到后端 + + # API 路由 - 所有 /api/ 开头的请求转发到后端 location /api/ { proxy_pass http://backend; proxy_set_header Host $host; @@ -55,6 +55,23 @@ server { proxy_intercept_errors on; error_page 502 503 504 /50x.html; } + + + location ^~ /uploads/ { + proxy_pass http://backend/uploads/; + 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; + + expires 1y; + add_header Cache-Control "public, immutable"; + + add_header Access-Control-Allow-Origin "*"; + add_header Access-Control-Allow-Methods "GET, OPTIONS"; + add_header Access-Control-Allow-Headers "Origin, Content-Type, Accept"; + } + # 静态文件路由 - 直接转发到前端 location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { @@ -69,24 +86,6 @@ server { add_header Cache-Control "public, immutable"; } - # 上传文件路由 - 直接访问后端的上传目录 - location /uploads/ { - proxy_pass http://backend; - 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; - - # 缓存设置 - expires 1y; - add_header Cache-Control "public, immutable"; - - # 允许跨域访问 - add_header Access-Control-Allow-Origin "*"; - add_header Access-Control-Allow-Methods "GET, OPTIONS"; - add_header Access-Control-Allow-Headers "Origin, Content-Type, Accept"; - } - # 微信公众号验证文件路由 - 根目录的TXT文件直接访问后端uploads目录 location ~ ^/[^/]+\.txt$ { # 检查文件是否存在于uploads目录