Search code examples
node.jsparametersnginxproxypass

nginx to node.js - pass params


I would like to pass certain parameters to nodejs over nginx.

While I still used fastcgi, I could do that this way:

fastcgi_param   SCRIPT_FILENAME         $document_root$fastcgi_script_name;
fastcgi_param   PATH_INFO               $fastcgi_script_name;

And now I'm basically searching the exact same functionality, for node.js

This would be my current configuration:

server {
    # ... other stuff ...

    location / {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Nginx-Proxy true;

        proxy_pass http://node;
        proxy_redirect off;

        # pass any parameter here
    }

}

upstream node {
        server  127.0.0.1:8080;
}

How can I accomplish that? - And, how can I read the passed value in node.js?


Solution

  • The short answer that directly addresses your question, in your nginx config, add lines like this:

    proxy_set_header X-My-Custom-Param-1 $whatever_variable_you_want_to_pass;
    

    And to read that in your express route handler function,

    req.get('X-My-Custom-Param-1');
    

    However, if you explain the larger problem you are trying to solve and the specific value(s) you think you need to pass, we can give specific help if. It's likely you are solving a solved problem poorly. I haven't seen any real-world use case where such a setup was necessary.