Nginx, Nodejs, Web

Node.js + Nginx

So this will explain about what I’ve been doing recently. Basically I have iOS mobile app that consume node.js based API (usual JSON REST stuff). And now the challenge is to deploy it
to production (and hope for it to be approved by Apple :))

So the node.js server itself is an Express based web-server application and configured to be persistent by using ‘forever’ forever will ensure our backend-server to restart itself after crash (not quite a lot, but somehow it happen)

I use this command to run forever using custom log location (in which will be logrotated)

forever start -a -o /opt/myserver/myserver.log -l /opt/myserver/myserver.log -e /opt/myserver/myserver.log app.js

Nginx will be the one who listen on port 80 and reverse-proxy-ing all outside request to our node.js web-server. This is the setup that I use

server {
  listen 80;

  server_name myserver.com;
  root /var/www/myserver;

  access_log "/var/www/myserver/access.log";
  error_log "/var/www/myserver/error.log" error;

  charset utf-8;

  default_type  application/octet-stream;
  sendfile        on;

  # would be awesome if your mobile-app can utilize keep-alives!
  keepalive_timeout  65;

  # enable gzip
  gzip on;
  gzip_comp_level 6;
  gzip_vary on;
  gzip_min_length  1000;
  gzip_proxied any;
  gzip_buffers 16 8k; 

  # we only gzip these mime-types (since there's no use to gzip jpegs)
  gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

  # tell-client to cache all 'assets'
  location ~* \.(?:jpg|jpeg|gif|png|ico|gz|svg|svgz|mp4|ogg|ogv|webm)$ {
    expires 1M;
    access_log off;
    add_header Cache-Control "public";
  }

  # disable logging for some `common` files
  # Disable logging for favicon
  location = /favicon.ico {
        log_not_found off;
        access_log off;
  }

  # Disable logging for robots.txt
  location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
  }

  # Prevent clients from accessing hidden files (starting with a dot)
  location ~* (^|/)\. {
    return 403;
  }

  # Prevent clients from accessing to backup/config/source files
  location ~* (\.(bak|config|sql|fla|psd|ini|log|sh|inc|swp|dist)|~)$ {
    return 403;
  }

  # reverse-proxy here, if your have multiple machine/cores would be better to use UPSTREAM so nginx can load-balance requests
  try_files $uri $uri/ @mynodeserver; 
  location @mynodeserver {
        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 $http_host;
        proxy_set_header   X-NginX-Proxy true;
        proxy_set_header   Connection "";
        proxy_pass         http://127.0.0.1:8088;
  } 
}

That is all, easy peasy huh? 🙂

The nginx setup is a combination from awesome h5bp boilerplate

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s