nginx 到 node.js - 传递参数

use*_*099 3 parameters nginx proxypass node.js

我想通过 nginx 将某些参数传递给 nodejs。

虽然我仍然使用 fastcgi,但我可以这样做:

fastcgi_param   SCRIPT_FILENAME         $document_root$fastcgi_script_name;
fastcgi_param   PATH_INFO               $fastcgi_script_name;
Run Code Online (Sandbox Code Playgroud)

现在我基本上是在为 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;
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?- 而且,如何读取 node.js 中传递的值?

Pet*_*ons 5

直接解决您的问题的简短答案,在您的 nginx 配置中,添加如下行:

proxy_set_header X-My-Custom-Param-1 $whatever_variable_you_want_to_pass;
Run Code Online (Sandbox Code Playgroud)

并在您的快速路由处理程序函数中读取它,

req.get('X-My-Custom-Param-1');
Run Code Online (Sandbox Code Playgroud)

但是,如果您解释了您尝试解决的更大问题以及您认为需要传递的特定值,我们可以提供特定帮助。您很可能在解决已解决的问题时做得很差。我还没有看到任何需要这种设置的实际用例。