for multipart file or large body pay load 'useForward' is making request to fail when some async task needs to be done like fetching data from some api.
MrSpark2591 opened this issue · 5 comments
Please help me with this. As for every request, I have to query Redis to get data and attach them in header and pass it to respective java APIs. It is making request fail when there is file need to be send.
Can you show some code?
proxy
.useForward(function(req, res, next) {
var cookies = cookie.parse(req.headers.cookie);
if (cookies.token) {
var session = JSON.parse(redis.getSession(cookies.token)); // this line is redis call which is async task and will take some time
if (session) {
req.headers['authorization'] = session.token.accessToken;
req.headers['auth-provider'] = session.token.authProvider;
}
next();
} else {
next();
}
}
})
What about something like, assuming it's a callback-based async code:
proxy.useForward(function(req, res, next) {
var cookies = cookie.parse(req.headers.cookie);
if (cookies.token) {
redis.getSession(cookies.token, (err, token) => {
if (err) {
// do something with the error
return next(err)
}
var session = JSON.parse(token)
if (session) {
req.headers['authorization'] = session.token.accessToken;
req.headers['auth-provider'] = session.token.authProvider;
}
next()
})
} else {
next();
}
}
})
yeh tried, that is not working :(
for other APIs it's working well but when it comes to file upload it fails.
in Nginx it is giving me : prematurely closed connection while reading response header from upstream client
My nginx config
`server {
listen 80;
server_name peoplehum.dev www.peoplehum.dev;
#rewrite ^/(.*)/$ /$1 permanent;
charset utf-8;
keepalive_requests 100;
keepalive_timeout 100s;
gzip on;
gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 6;
gzip_proxied any;
gzip_buffers 16 8k;
gzip_disable "MSIE [1-6].(?!.*SV1)";
client_max_body_size 16M;
#include /etc/nginx/proxy_header.conf;
#include /etc/nginx/proxy_buffer.conf;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_set_header HOST $host;
#X-Forwarded-Proto header gives the proxied server information about the schema of the original client request (whether it was an http or an https request).
proxy_set_header X-Forwarded-Proto $scheme;
#The X-Real-IP is set to the IP address of the client so that the proxy can correctly make decisions or log based on this information.
proxy_set_header X-Real-IP $remote_addr;
#The X-Forwarded-For header is a list containing the IP addresses of every server the client has been proxied through up to this point.
#In the example above, we set this to the $proxy_add_x_forwarded_for variable.
#This variable takes the value of the original X-Forwarded-For header retrieved from the client and adds the Nginx server's IP address to the end.
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}`