gpt4 book ai didi

node.js - Nginx X-Accel-Redirect 不适用于 node.js

转载 作者:搜寻专家 更新时间:2023-11-01 00:24:58 25 4
gpt4 key购买 nike

我正在尝试在 node.js 支持的 nginx 上设置授权文件访问。出于某种原因,所有示例都不适合我。我正在尝试从 /data/private/files

服务器文件

我的 nginx 配置:

...
server {
listen 4000;
server_name localhost;

location / {
proxy_pass http://127.0.0.1:3000/;
}

location /files {
root /data/private;
internal;
}

我的 Node server.js:

var http = require('http');
http.createServer(function (req, res) {
console.log(req.url);
res.end('works');
}).listen(3000);

当我请求 http://localhost:4000/xyz 时,请求被正确地传递到 Node 。当我请求 http://localhost:4000/files/test.jpg 时,我只得到一个 404,没有任何东西传递给 Node 。我究竟做错了什么?当我推荐 internal 时,nginx 会直接正确提供 test.jpg,所以我假设路径是正确的?

我很确定我以前在某个时候有过这个工作,但是在某个地方的不同服务器上可能有不同的 Node 和 nginx 版本。尝试使用 nginx 1.6.0 和 1.2.6, Node v0.10.21。我还添加了您在所有示例中找到的所有 proxy_set_header 和 proxy_pass 选项,但没有任何效果。我现在正在基于 Vagrant 的 Ubuntu VM 中运行它,但在 Mac 上也不起作用。

我知道我必须通过 res.setHeader("X-Accel-Redirect", req.url); 设置 header ,但这不是这里的问题,因为我什至都不知道进入我可以在 Node 中设置所需 header 的阶段。

最佳答案

您误解了 internalX-Accel-Redirect 的工作方式。

主要思想是您转到某个代理到应用程序的 URL。然后应用程序决定您是否应该访问文件。在前一种情况下,它使用 X-Accel-Redirect 响应 protected url(一个带有 internal)。

所以你应该转到其他一些 URL,例如http://localhost:4000/get/files/test.jpg 您的应用程序可能如下所示:

var http = require('http');
http.createServer(function (req, res) {
if (req.url.indexOf('/get/files/') == 0) {
if (userHasRightToAccess()) {
res.setHeader('X-Accel-Redirect', res.url.slice(4));
res.end('');
} else {
// return some error
}
} else {
console.log(req.url);
res.end('works');
}
}).listen(3000);

关于node.js - Nginx X-Accel-Redirect 不适用于 node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23904521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com