Skip to content
This repository has been archived by the owner on Mar 16, 2022. It is now read-only.

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. #105

Closed
MrSpark2591 opened this issue Mar 7, 2017 · 5 comments

Comments

@MrSpark2591
Copy link

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.

@h2non
Copy link
Owner

h2non commented Mar 22, 2017

Can you show some code?

@MrSpark2591
Copy link
Author

MrSpark2591 commented Mar 22, 2017

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();
          }
        } 
      })

@tomas-fp
Copy link

tomas-fp commented Mar 22, 2017

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();
          }
        } 
      })

@MrSpark2591
Copy link
Author

MrSpark2591 commented Mar 22, 2017

yeh tried, that is not working :(
for other APIs it's working well but when it comes to file upload it fails.

@MrSpark2591 MrSpark2591 changed the title for multipart file 'useForward' is making request to fail when some async task needs to be done like fetching data from some api. 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. Jun 22, 2017
@MrSpark2591 MrSpark2591 reopened this Jun 22, 2017
@MrSpark2591
Copy link
Author

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;
}`

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@h2non @MrSpark2591 @tomas-fp and others