Nginx はフロント エンド サーバーとして機能し、この場合はリクエストを node.js サーバーにプロキシします。したがって、ノードの nginx 構成ファイルをセットアップする必要があります。
これは、Ubuntuボックスで行ったことです。
yourdomain.com
次の場所にファイルを作成します/etc/nginx/sites-available/
。
vim /etc/nginx/sites-available/yourdomain.com
その中には、次のようなものが必要です。
# 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;
}
}
nginx (>= 1.3.13) で websocket リクエストも処理する場合は、location /
セクションに次の行を追加します。
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
このセットアップが完了したら、上記の構成ファイルで定義されたサイトを有効にする必要があります。
cd /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/yourdomain.com yourdomain.com
でノード サーバー アプリを作成し/var/www/yourdomain/app.js
、で実行します。localhost:3000
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/');
構文の誤りをテストします。
nginx -t
nginx を再起動します。
sudo /etc/init.d/nginx restart
最後にノード サーバーを起動します。
cd /var/www/yourdomain/ && node app.js
yourdomain.com に「Hello World」が表示されるはずです。
ノード サーバーの起動に関する最後の注意: ノード デーモンには何らかの監視システムを使用する必要があります。upstart と monit を使用した node に関する素晴らしいチュートリアルがあります。