3

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?

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I think that'll help a lot. The scenario is, that I'm passing different config-file paths to the server, since the config files arent stored in /var/www/ but in /etc/. The NGINX config itself takes care of that, so that I can have different dev configs (dev, staging, etc) - and directly steer them over the NGINX conf.
Damn, I just figured that with node, it doesnt make too much sense. In PHP it did, since every call was the whole process - but with node I should just start it with params.
Yes, in node the convention is to use just a simple config file (.js or .json) or environment variables.
Sure, I had that - but I wanted to define the config-path (path to the config file) outside from NGINX, to be able to change it to different settings if required. I managed to do that now, and I am handling a new command-line argument --config=/path/ now
0

Any parameters will be passed by default and all you need is to handle routing in node it self.

Please check out express.js as it allows to define very flexible routes with regexp (if needed).
Bear in mind that params in express is different from query data ($_GET in PHP). As query data goes after question mark in URL, but params are defined in routes.
For example:

app.get('/user/:id', function(req, res, next) {
  res.send({
    params: req.params
    query: req.query
  }); // will respond with json object with 'id'
});

Then test it, with url: http://example.com/user/23?foo=bar&hello=world
It will output:

{
  params: {
    id: 23
  },
  query: {
    foo: 'bar',
    hello: 'world'
  }
}

2 Comments

You mean GET params, right? I wasnt actually talking about GET parameters, but about 'internal' paramters - if I'm using the wrong wording, I didnt know that. I'm refering to NGINX config file entries. (for example, a certain path, etc)
GET parameters are query

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.