即使我在 nginx.conf 里把这两个参数设置后
client_body_temp_path /tmp;
client_max_body_size 20m;
依然没什么用,后端用的是php,前端上传被调用时都没到调用后端接口就直接被阻止了不知道是什么原因?


即使我在 nginx.conf 里把这两个参数设置后
client_body_temp_path /tmp;
client_max_body_size 20m;
依然没什么用,后端用的是php,前端上传被调用时都没到调用后端接口就直接被阻止了不知道是什么原因?


关注随着nginx的使用,一台服务器下的域名及端口多了以后,在nginx.conf配置文件中就需要不断的配置server。
久而久之,就会发现nginx.conf特别臃肿。
所以我在这里对这个nginx.conf进行解耦拆分,让每个端口都有自己独立的配置文件。

针对问题中的具体情况,需要考虑以下几个方面:
location /upload {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,Keep-Alive,X-Requested-With,If-Modified-Since';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,Keep-Alive,X-Requested-With,If-Modified-Since';
if ($request_method = 'POST') {
add_header 'Access-Control-Expose-Headers' 'X-Requested-With,Content-Type,Content-Length';
add_header 'Access-Control-Allow-Origin' '*';
}
...
}
总之,正确的配置nginx参数和添加CORS头信息,以及优化后端逻辑都可以在保证上传PDF文件成功的同时,提高整个系统的稳定性。