我想通过 nginx 将某些参数传递给 nodejs。
虽然我仍然使用 fastcgi,但我可以这样做:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
现在我基本上是在为 node.js 搜索完全相同的功能
这将是我当前的配置:
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;
}
我怎样才能做到这一点? - 还有,我如何读取 node.js 中传递的值?
最佳答案
直接解决您问题的简短回答,在您的 nginx 配置中,添加如下行:
proxy_set_header X-My-Custom-Param-1 $whatever_variable_you_want_to_pass;
并在您的快速路由处理函数中读取它,
req.get('X-My-Custom-Param-1');
但是,如果您解释了您试图解决的更大问题以及您认为需要传递的具体值,我们可以提供具体帮助。您很可能没有很好地解决已解决的问题。我还没有看到任何需要此类设置的实际用例。
关于node.js - nginx 到 node.js - 传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17483641/