node.js - nginx 到 node.js - 传递参数

标签 node.js parameters nginx proxypass

我想通过 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/

相关文章:

c++ - 如何返回函数的参数数量?

logging - 如何在 Nginx 日志中查看不可用的服务器?

r - 如何将 iframe 指向 docker 网络中的 url?

javascript - NodeJS + Javascript 如何导出值而不是 promise ?

javascript - 如何在 eclipse 中运行 Node.js 文件?

javafx - 传递参数JavaFX FXML

php - CodeIgniter 登录时不允许 Nginx 405

javascript - 尽管 async/await,nestJS Mongoose 回调在返回后仍被调用

node.js - 当您有新文档和现有文档的 JSON 数组时,是否有一个函数可以将新文档添加到数据库中?

oracle - 访问 Oracle Forms/OC4J 中的 URL 参数