Skip to content

Instantly share code, notes, and snippets.

@fahimbabarpatel
Last active September 30, 2015 14:14
Show Gist options
  • Save fahimbabarpatel/b5600ec4cc7324b18737 to your computer and use it in GitHub Desktop.
Save fahimbabarpatel/b5600ec4cc7324b18737 to your computer and use it in GitHub Desktop.
Node.js + Nginx setup + Amazon Linux
sudo yum install -y nginx
sudo npm install -g forever
Open /etc/nginx/nginx.conf in sudo edit mode and replace http block
 http {
  include mime.types;
  default_type application/octet-stream;
  sendfile on;

  # backend applications
  upstream nodes {
    server 127.0.0.1:3000;
    keepalive 64;
  }

  server {
    listen 80;
    server_name localhost; # It can be domain URL

    # static resources
    location ~ ^/(robots.txt|humans.txt|favicon.ico) {
      root /usr/share/nginx/html;
      access_log off;
      expires max;
    }

    # everything else goes to backend node apps
    location / {
      proxy_pass http://nodes;

      proxy_redirect off;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header Host $host;
      proxy_set_header X-NginX-Proxy true;
      proxy_set_header Connection "";
      proxy_http_version 1.1;
    }
  }
}

Start your node.js application using forever

forever start server.js ( Assuming that node.js application listens at 3000 port )

All setup done. Restart your nginx sudo service nginx restart and hit server IP("server_name" value).

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment