centos 7 + nginx + nodejsのnginxの設定メモ
nginxの設定を以下にメモ。
参考にさせて頂いた記事
設定ファイルの作成
/etc/nginx/conf.d/default.confを同じ階層にコピーして、sample.confみたいにする。
/etc/nginx/conf.d/sample.conf
upstream node-sampleapp { server localhost:3000; } server { listen 80; server_name www.aaa.bbb.ccc; charset utf-8; access_log /var/log/nginx/access-nodeApp.log main; error_log /var/log/nginx/error-nodeApp.log; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; location / { proxy_pass http://node-sampleapp/; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
これで、www.aaa.bbb.cccにアクセスするだけで、www.aaa.bbb.ccc:3000などを指定する必要がなくなる!
注意
//これは☓ proxy_pass node-sampleapp/; //これでOK proxy_pass http://node-sampleapp/;
ここを気をつけないと以下のエラーがでてしまうようだ。しっかり、「http://」を付けましょう。
nginx: [emerg] invalid URL prefix in /etc/nginx/conf.d/node-app.conf:21 nginx: configuration file /etc/nginx/nginx.conf test failed