Reputation: 137
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?
Upvotes: 3
Views: 4090
Reputation: 146014
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.
Upvotes: 5
Reputation: 23047
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'
}
}
Upvotes: 0