Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running nodejs under nginx

Tags:

node.js

nginx

I'm currently trying out nginx and nodejs with connect running nodejs proxied in nginx. The problem I have is that I currently don't run nodejs under the root (/) but under /data as nginx should handle the static requests as normal. nodejs should not have to know that it's under /data but it seems to be required.

In other words. I want nodejs to "think" it runs at /. Is that possible?

nginx config:

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 code:

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');
    });
};
like image 857
Asken Avatar asked Nov 14 '11 12:11

Asken


People also ask

Can I run nodejs on Nginx?

Fortunately, you can cache static content, reverse proxy and load balance among multiple application servers, and manage port contention between clients using Nginx. This makes Nginx an excellent tool for increasing Node. js performance.

Why does nodejs need Nginx?

Originally Answered: If Node. js is used, does NGINX or Apache still need to be installed? yes, you need nginx (not apache) to complement nodejs for a serious website. the reason is nginx is easier to deploy and debug (and performs better than nodejs) for “mundane” things like handling https and serving static files.

What is Node js and Nginx?

Conclusion. Node. js is a JS runtime environment that is also an HTTP server with some event-driven features and has many drawbacks in terms of concurrency and high load or user requests to handle a large number of users concurrently. Nginx has the best performance in this case, and it provides the best performance.


2 Answers

I think this solution may be better (if you are using something like Express OR something similar that uses the "middleware" logic):

Add a middleware function to change the url like so

rewriter.js

module.exports = function temp_rewrite() {
  return function (req, res, next) {
    req.url = '/data' + req.url;
    next();
  }
}

In your Express app do like so:

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')
});
like image 99
alessioalex Avatar answered Sep 24 '22 11:09

alessioalex


I normally do this:

nginx config:

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) {
  ...
});

Then from the outside world I could make a request such as http://my-host-name/data/something?someKey=someValue, which nginx will pass over to the node.js app, who will only see it as /something?someKey=someValue (since the url was rewritten).

I know this isn't the only approach, but imo among the better ones, as I always respect the philosophy of "things that do one thing and do it well". Here, Nginx is good at rewriting URLs, whereas Node.js is good at handling the requests themselves.

like image 31
Khang Dinh Avatar answered Sep 25 '22 11:09

Khang Dinh