Skip to content

Instantly share code, notes, and snippets.

@lotsofcode
Forked from joemccann/nginx + node setup.md
Created August 16, 2013 12:38

Revisions

  1. @joemccann joemccann revised this gist Oct 25, 2010. 1 changed file with 15 additions and 22 deletions.
    37 changes: 15 additions & 22 deletions nginx + node setup.md
    Original file line number Diff line number Diff line change
    @@ -1,25 +1,23 @@
    The idea is to have nginx installed and node installed. Will extend this gist to include how to install those as well, but at the moment, the following assumes you have nginx and node installed on a linux distro (I used Ubuntu).
    The idea is to have nginx installed and node installed. I will extend this gist to include how to install those as well, but at the moment, the following assumes you have nginx 0.7.62 and node 0.2.3 installed on a Linux distro (I used Ubuntu).

    1) nginx is used to serve static files (css, js, images, etc.)
    2) nginx.conf - changes required to allow proxying back thru to your node app.

    So, www.foo.com request comes in
    In a nutshell,

    css, js, and images get served thru nginx
    1) nginx is used to serve static files (css, js, images, etc.)
    2) node serves all the "dynamic" stuff.

    everything else (the request for say index.html or "/") gets served through node.
    So for example, www.foo.com request comes and your css, js, and images get served thru nginx while everything else (the request for say index.html or "/") gets served through node.

    3) nginx listens on port 80.
    4) node listens on port 8124.
    4) node listens on port 8124 (for this example only. you can change this port for your node app).

    So in your <code>/etc/nginx/sites-available/default</code> modify your <code>location /</code> stanza and add the second stanza of this code block:

    So in your /etc/nginx/sites-available/default:
    <code>
    [...]
    location / {
    proxy_pass http://127.0.0.1:8124; #this is the ip:port where your node app runs
    root /var/www/yoursitename;
    expires 30d;
    #uncomment this is you want to name an index file, otherwise a 403 will return:
    #uncomment this is you want to name an index file:
    #index index.php index.html;
    access_log off;
    }
    @@ -28,21 +26,16 @@ So in your /etc/nginx/sites-available/default:
    root /var/www/yoursitename/public;
    }

    location /doc {
    root /usr/share;
    autoindex on;
    allow 127.0.0.1;
    deny all;
    }
    </code>
    [...]
    Note: I did not change the <code>/etc/nginx/nginx.conf<code> file. It is still the default <code>nginx.conf</code> from the nginx installation.

    Restart nginx.
    Now, restart nginx.

    <code>/etc/init.d/nginx restart</code>

    Restart your node app.
    (Re)start your node app.

    <code>node /path/to/your/node/app.js</code>

    Navigate to your site and verify.
    Navigate to your site and verify.

    Enjoy blazing fast static files and blazing fast dynamic content.
  2. @joemccann joemccann renamed this gist Oct 25, 2010. 1 changed file with 16 additions and 3 deletions.
    19 changes: 16 additions & 3 deletions nginx + node setup → nginx + node setup.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    The idea is to have nginx installed and node installed. Will extend this gist to include those soon, but the following assumes you have nginx and node installed on a linux distro (I used Ubuntu
    The idea is to have nginx installed and node installed. Will extend this gist to include how to install those as well, but at the moment, the following assumes you have nginx and node installed on a linux distro (I used Ubuntu).

    1) nginx is used to serve static files (css, js, images, etc.)
    2) nginx.conf - changes required to allow proxying back thru to your node app.
    @@ -13,12 +13,13 @@ everything else (the request for say index.html or "/") gets served through node
    4) node listens on port 8124.

    So in your /etc/nginx/sites-available/default:

    <code>
    [...]
    location / {
    proxy_pass http://127.0.0.1:8124; #this is the ip:port where your node app runs
    root /var/www/yoursitename;
    expires 30d;
    #uncomment this is you want php to pass thru:
    #uncomment this is you want to name an index file, otherwise a 403 will return:
    #index index.php index.html;
    access_log off;
    }
    @@ -33,3 +34,15 @@ So in your /etc/nginx/sites-available/default:
    allow 127.0.0.1;
    deny all;
    }
    </code>
    [...]

    Restart nginx.

    <code>/etc/init.d/nginx restart</code>

    Restart your node app.

    <code>node /path/to/your/node/app.js</code>

    Navigate to your site and verify.
  3. @joemccann joemccann revised this gist Oct 25, 2010. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions nginx + node setup
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    The idea is to have nginx installed and node installed. Will extend this gist to include those soon, but the following assumes you have nginx and node installed on a linux distro (I used Ubuntu

    1) nginx is used to serve static files (css, js, images, etc.)
    2) nginx.conf - changes required to allow proxying back thru to your node app.

  4. @joemccann joemccann created this gist Oct 25, 2010.
    33 changes: 33 additions & 0 deletions nginx + node setup
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    1) nginx is used to serve static files (css, js, images, etc.)
    2) nginx.conf - changes required to allow proxying back thru to your node app.

    So, www.foo.com request comes in

    css, js, and images get served thru nginx

    everything else (the request for say index.html or "/") gets served through node.

    3) nginx listens on port 80.
    4) node listens on port 8124.

    So in your /etc/nginx/sites-available/default:

    location / {
    proxy_pass http://127.0.0.1:8124; #this is the ip:port where your node app runs
    root /var/www/yoursitename;
    expires 30d;
    #uncomment this is you want php to pass thru:
    #index index.php index.html;
    access_log off;
    }

    location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$ {
    root /var/www/yoursitename/public;
    }

    location /doc {
    root /usr/share;
    autoindex on;
    allow 127.0.0.1;
    deny all;
    }