How to proxy Node.js error responses on NGINX?

I have an application server that is running a Sails.js Node application on top of NGINX. The application appears to be working correctly, however when the server accepts/rejects a request, the response body is empty. The response code is correct, however the body is completely empty. I can verify that the app is generating the correct responses using the applications debugging that I have in place.

This is the NGINX configuration that I have in place:

upstream local-upstream {
        keepalive 64;
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        return 301 https://$host$request_uri;
server {
        listen 443 ssl http2;
        listen [::]:443 ssl http2;
        ssl on;
        ssl_certificate XXX;
        ssl_certificate_key XXX;
        ssl_prefer_server_ciphers on;

        ## OCSP Stapling
        resolver valid=300s;
        resolver_timeout 5s;
        ssl_stapling on;
        ssl_stapling_verify on;
        ssl_trusted_certificate XXX;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        #server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                #try_files $uri $uri/ =404;
                proxy_pass http://local-upstream;
                proxy_redirect off;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;

Submit an answer
Answer a question...

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

Hi @jtittle I was able to speak with the developer of the Sails.js platform. Apparently there was some code hidden away that clears out the data response in production environments within certain types of responses. A 400 response happen to be one of those responses. I am super sorry for the confusion.

@dniccumdesign THANK YOU! After long testing this setup is properly forwarding all the headers from nginx to node app.

what was missing in that list is : proxy_pass (your proxy ip, port);

Definitely problem with the node app, what it helped me is to echo all headers, and get 200response (instead of 401 or 502):

var server = require(‘http’).createServer(function(req, res) { var host = req.headers; console.log(JSON.stringify(req.headers)); res.end(); });

server.listen(process.env.PORT || 8000);

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Jonathan Tittle
DigitalOcean Employee
DigitalOcean Employee badge
July 26, 2017


In place of:

    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;

Please try:

        proxy_buffers 16 32k;
        proxy_buffer_size 64k;
        proxy_busy_buffers_size 128k;
        proxy_cache_bypass $http_pragma $http_authorization;
        proxy_connect_timeout 59s;
        proxy_hide_header X-Powered-By;
        proxy_http_version 1.1;
        proxy_ignore_headers Cache-Control Expires;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
        proxy_no_cache $http_pragma $http_authorization;
        proxy_pass_header Set-Cookie;
        proxy_read_timeout 600;
        proxy_redirect off;
        proxy_send_timeout 600;
        proxy_temp_file_write_size 64k;
        proxy_set_header Accept-Encoding '';
        proxy_set_header Cookie $http_cookie;
        proxy_set_header Host $host;
        proxy_set_header Proxy '';
        proxy_set_header Referer $http_referer;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Original-Request $request_uri;

Once those changes are made, please restart NGINX:

service nginx restart

If you run in to any errors along the way, please tail the logs and let’s take a look:

tail -25 /var/log/nginx/error.log