Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Nginx works as a front end server, which in this case proxies the requests to a node.js server. Therefore you need to setup an nginx config file for node.</p> <p>This is what I have done in my Ubuntu box:</p> <p>Create the file <code>yourdomain.com</code> at <code>/etc/nginx/sites-available/</code>:</p> <pre><code>vim /etc/nginx/sites-available/yourdomain.com </code></pre> <p>In it you should have something like: </p> <pre><code># the IP(s) on which your node server is running. I chose port 3000. upstream app_yourdomain { server 127.0.0.1:3000; keepalive 8; } # the nginx server instance server { listen 80; listen [::]:80; server_name yourdomain.com www.yourdomain.com; access_log /var/log/nginx/yourdomain.com.log; # pass the request to the node.js server with the correct headers # and much more can be added, see nginx config options 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://app_yourdomain/; proxy_redirect off; } } </code></pre> <p>If you want nginx (>= 1.3.13) to handle websocket requests as well, add the following lines in the <code>location /</code> section:</p> <pre><code>proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; </code></pre> <p>Once you have this setup you must enable the site defined in the config file above:</p> <pre><code>cd /etc/nginx/sites-enabled/ ln -s /etc/nginx/sites-available/yourdomain.com yourdomain.com </code></pre> <p>Create your node server app at <code>/var/www/yourdomain/app.js</code> and run it at <code>localhost:3000</code></p> <pre><code>var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(3000, "127.0.0.1"); console.log('Server running at http://127.0.0.1:3000/'); </code></pre> <p>Test for syntax mistakes:</p> <pre><code>nginx -t </code></pre> <p>Restart nginx:</p> <pre><code>sudo /etc/init.d/nginx restart </code></pre> <p>Lastly start the node server:</p> <pre><code>cd /var/www/yourdomain/ &amp;&amp; node app.js </code></pre> <p>Now you should see "Hello World" at yourdomain.com</p> <p>One last note with regards to starting the node server: you should use some kind of monitoring system for the node daemon. There is an awesome <a href="http://howtonode.org/deploying-node-upstart-monit" rel="noreferrer">tutorial on node with upstart and monit</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload