浏览 2.8k
这是之前踩过的坑,我们要在a.example.com某个目录里面增加允许b.example.com的跨域请求,原来的配置是这样:
location /static/ {
proxy_pass http://backend;
proxy_set_header Host $host;
}
然后我一开始是改为了下面的配置:
location /static/ {
if ($http_origin ~* ((http|https)?://b\.example\.com$)) {
add_header 'Access-Control-Allow-Origin' $http_origin;
add_header 'Access-Control-Allow-Credentials' 'true';
}
proxy_pass http://backend;
proxy_set_header Host $host;
}
但是这样是不生效的,调试了一段时间,期间尝试增加了各种跨域相关的其他头,都没有作用,然后尝试调整了跨域配置的位置就好了,调整成了以下这样就可以了:
location /static/ {
proxy_pass http://backend;
proxy_set_header Host $host;
if ($http_origin ~* ((http|https)?://b\.example\.com$)) {
add_header 'Access-Control-Allow-Origin' $http_origin;
add_header 'Access-Control-Allow-Credentials' 'true';
}
}
虽然阴差阳错解决了问题,不过不明白其中的原理,所以想请教一下陶辉老师这个问题。
按点赞数排序
按时间排序
1、在动不了客户端的情况下,只能调大large_client_header_buffers。
2、更换HTTP2/3协议,在不涉及业务层代码变动(HTTP/1语义不变)的基础上,HPACK和QPACK都可以通过动态表压缩头部
新建一个node服务,使用nginx-upload-module
提供的upload_pass
参数将处理过的请求转发到我们的node服务上面。分两步走:
修改nginx配置,将nginx-upload-module
存储文件的路径和文件名称塞到请求的body参数中。同时将请求转发到node服务具体配置:
location /upload {
upload_pass /res_upload;
upload_store /usr/local/nginx/html;
upload_resumable on;
upload_state_store /tmp/state;
upload_set_form_field "${upload_field_name}_name" $upload_file_name;
upload_set_form_field "${upload_field_name}_content_type" $upload_content_type;
upload_set_form_field "${upload_field_name}_path" $upload_tmp_path;
upload_aggregate_form_field "${upload_field_name}_md5" $upload_file_md5;
upload_aggregate_form_field "${upload_field_name}_size" $upload_file_size;
}
location /res_upload {
#default_type text/html;
#return 200 "success";
proxy_pass http://localhost:8080;
}
const http = require('http');
const fs = require('fs');
http.createServer((req, res) => {
let body = '';
req.on('data', chunk => {
body += chunk; // 获取form-data
});
req.on('end', () => {
console.log(body);
const params = parseForm(body); // json化参数
rename(params.file_path, params.file_name);
res.end('success: you can get it from: /' + params.file_name);
})
}).listen(8080)
function parseForm(data) {
const reg = /name="([\w_]+)"\s+(.+)\s/g;
const params = {};
let matched;
while((matched = reg.exec(data))){
params[matched[1]] = matched[2];
}
console.log(params);
return params;
}
function rename(source, name){
const path = require('path');
const dir = path.dirname(source);
fs.renameSync(source, path.join(dir, name));
}