Robert Robert - 1 year ago 97
Node.js Question

How to allow http requests in Node.js Nginx?

I have a node app (using PM2) listening on on my DigitalOcean droplet running Ubuntu. However, I have a problem. Everything works fine except for the fact that all of my http post requests recieve a 404 Not Found error. I have no idea why.

Here is what my Nginx conf file looks like:

server {
root /var/www/app_folder;


location / {

It loads everything fine. Static images, CSS, html, and even the javascript files. However, all my outgoing HTTP posts are 404.

Any help would be greatly appreciated. Thanks ahead of time.

EDIT: My node.js file is like any other. Here's the rundown.'/someURL', function(req, res) {...}

app.listen(3000, "");

Answer Source

So I solved it!

I was running Ubuntu 14.04 on my DigitalOcean Droplet. The problem was that when i called sudo apt-get install nginx, it would install NginX version 1.4.6 automatically. However, the latest stable release of NginX is version 1.8.0. Here are the steps to installing the latest version:

  1. Add NginX PPA sudo add-apt-repository ppa:nginx/stable
    1. If add-apt-repository isn't available, then do the following:
      1. For Ubuntu version v12.04 or lower: sudo apt-get install python-software-properties, then re-run the first command sudo add-apt-repository ppa:nginx/stable
      2. For Ubuntu version greater than v12.04: sudo apt-get install software-properties-common, then re-run the first command sudo add-apt-repository ppa:nginx/stable
  2. Now run an update: sudo apt-get update
  3. Finally, install NginX: sudo apt-get install nginx

Next, configure NginX:

  1. Navigate to /etc/nginx/
  2. cd sites-available
  3. touch YOUR_APP NOTE: "YOUR_APP" should be replaced with whatever you wish to call your Node.js app.
  4. sudo vi YOUR_APP and configure your web server to listen correctly.

Here is a sample of the web server code:

server {  
    listen 80;

    location / {
        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://YOUR_APP_LOCAL_IP:YOUR_APP_PORT;
        proxy_redirect off;

In your Node.js file, there will either be a line similar to this: app.listen(3000, "");
or this: app.listen(3000);

  • If you have the first version, fill in "YOUR_APP_LOCAL_IP" with "" or whatever you have in place of that in your node.js file, and fill in "YOUR_APP_PORT" with "3000" or whatever port you have it set to listen to.
  • If you have the second version, fill in "YOUR_APP_LOCAL_IP" with "localhost"
    and fill in "YOUR_APP_PORT" with "3000" or whatever port you have it set to listen to.

That should be it! Make sure to daemonize the app as well. You can use something like PM2 for that. Hope this helps anyone who was in a similar position like me pulling their hair out.

EDIT: Here is a good link that summarizes all of this.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download