簡體   English   中英

在nginx下運行nodejs

[英]Running nodejs under nginx

我正在嘗試使用在nginx中代理的連接運行nodejs的nginx和nodejs。 我遇到的問題是我目前不在root(/)下運行nodejs但在/ data下運行nginx應該正常處理靜態請求。 nodejs不應該知道它在/ data下,但它似乎是必需的。

換一種說法。 我希望nodejs“思考”它在/運行。 那可能嗎?

nginx配置:

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');
    });
};

我認為這個解決方案可能會更好(如果你使用像Express或類似的東西使用“中間件”邏輯):

添加中間件函數來更改URL如此

rewriter.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;
  }
}

node.js app:

app.get('/something', function(req, res, next) {
  ...
});

然后從外面的世界我可以發出一個請求,例如http://my-host-name/data/something?someKey=someValue ,nginx將傳遞給node.js app,他們只會將其視為/something?someKey=someValue (因為url被重寫)。

我知道這不是唯一的方法,而是更好的方法之一,因為我始終尊重“ 做一件事,做得好的事情 ”的理念。 在這里,Nginx擅長重寫URL,而Node.js善於處理請求本身。

臨時解決方案:對我來說並不理想,但它現在必須到期,因為我至少可以配置它。 我正在使用以下命令啟動節點服務器:

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 ,然后找到該文件的目錄是目標目錄)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM