Node.js 在nginx下运行nodejs
我目前正在试用nginx和nodejs,其中connect运行nginx中代理的nodejs。我的问题是,我目前没有在根(/)下运行nodejs,而是在/data下运行nodejs,因为nginx应该正常处理静态请求。nodejs不必知道它在/data下,但它似乎是必需的 换句话说。我希望nodejs“思考”它运行在/。可能吗 nginx配置:Node.js 在nginx下运行nodejs,node.js,nginx,Node.js,Nginx,我目前正在试用nginx和nodejs,其中connect运行nginx中代理的nodejs。我的问题是,我目前没有在根(/)下运行nodejs,而是在/data下运行nodejs,因为nginx应该正常处理静态请求。nodejs不必知道它在/data下,但它似乎是必需的 换句话说。我希望nodejs“思考”它运行在/。可能吗 nginx配置: upstream app_node { server 127.0.0.1:3000; } server { ... locatio
upstream app_node {
server 127.0.0.1:3000;
}
server {
...
location /data {
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://app_node/data;
proxy_redirect off;
}
}
nodejs代码:
exports.routes = function(app) {
// I don't want "data" here. My nodejs app should be able to run under
// any folder
app.get('/data', function(req, res, params) {
res.writeHead(200, { 'Content-type': 'text/plain' });
res.end('app.get /data');
});
// I don't want "data" here either
app.get('/data/test', function(req, res, params) {
res.writeHead(200, { 'Content-type': 'text/plain' });
res.end('app.get /data/test');
});
};
临时解决方案: 这对我来说并不理想,但它现在必须到期,因为我至少可以配置它。我正在使用以下命令启动节点服务器:
node app2.js /data
处理根节点的代码:
exports.routes = function(app) {
var me = this;
// get the root path
me.proxyRootPath = process.argv[2];
// using the proxyRootPath to answer to the correct request
app.get(me.proxyRootPath, function(req, res, params) {
res.writeHead(200, { 'Content-type': 'text/plain' });
res.end('app.get /data');
});
// using the proxyRootPath to answer to the correct request
app.get(me.proxyRootPath + '/test', function(req, res, params) {
res.writeHead(200, { 'Content-type': 'text/plain' });
res.end('app.get /data/test');
});
};
解决方案:
- 编写应用程序以在命令行上查找目录。运行应用程序时,在命令行上传递目录
- 编写应用程序以在特定环境变量中查找目录。运行应用程序时,首先设置环境变量
- 编写应用程序时,假定其当前目录是预期目录。运行应用程序时,首先切换到目录
- 编写你的应用程序,在目录树中查找已知文件(例如,在目录树中查找
,然后找到该文件的目录就是预期的目录)/app2.js
module.exports = function temp_rewrite() {
return function (req, res, next) {
req.url = '/data' + req.url;
next();
}
}
在您的Express应用程序中,执行以下操作:
app.config
// your configuration
app.configure(function(){
...
app.use(require('./rewriter.js').temp_rewrite());
...
});
// here are the routes
// notice you don't need to write '/data' in front anymore all the time
app.get('/', function (req, res) {
res.send('This is actually site.com/data/');
});
app.get('/example', function (req, res) {
res.send('This is actually site.com/data/example')
});
我通常会这样做:
nginx配置:
upstream app_node {
server 127.0.0.1:3000;
}
server {
...
location /data/ {
...
proxy_pass http://app_node
rewrite ^/data/(.*)? /$1 break;
}
}
app.get('/something', function(req, res, next) {
...
});
node.js应用程序:
upstream app_node {
server 127.0.0.1:3000;
}
server {
...
location /data/ {
...
proxy_pass http://app_node
rewrite ^/data/(.*)? /$1 break;
}
}
app.get('/something', function(req, res, next) {
...
});
然后我可以从外部世界发出请求,例如http://my-host-name/data/something?someKey=someValue
,哪个nginx将传递给node.js应用程序,谁只会将其视为/something?someKey=someValue
(因为url已重写)
我知道这不是唯一的方法,但我认为这是最好的方法之一,因为我一直尊重“的哲学”。在这里,Nginx擅长重写URL,而Node.js擅长处理请求本身。问得好。我还没有找到解决这个问题的办法。我的应用程序中还有两条路径/web应用程序的应用程序和/或所有静态文件的静态应用程序。仅供参考。我添加了一个config.js,并在其中定义了设置以及各种内容。至少作为应用程序的一部分,它位于一个位置,而不是分散在启动配置或难以找到的环境变量中。请尝试在端口号后放置“/”。我一直在寻找一个“更好”的答案,但似乎nodejs并没有特别意识到它所处的环境。始终解析完整的url。我需要对来自ASP.Net的应用程序文件夹是根目录的内容进行思考。第三条不适用于我的案件,但其他的都是完全有效的。你再次。。。也许你可以改为开发我的应用:)。重写正是我所追求的,在我使用connect时,它会很好地工作。没问题,我总是很高兴能够帮助其他人解决Node.js相关的问题。当我刚刚学习Node&Express时,我有很多问题要问自己,像TJ(Express&Connect作者)这样的人真的很好地回答了我所有的问题。当然,我可以让你的应用程序:P