1.nginx服務器的安裝
sudo apt-get install nginx
2.nginx服務器的啟動
sudo /etc/init.d/nginx start
3.多域名對應不同的后臺服務器
在/etc/nginx/sites-enabled/ 目錄中刪除default配置文件
新建域名對應的配置文件
比如test.youlunshidai.com
#通過upstream nodejs__upstream 可以配置多臺nodejs節點,做負載均衡
upstream nodejs__upstream__test__youlunshidai {
server 127.0.0.1:8089; #服務器地址和端口號
#server 127.0.0.1:8088; #負載均衡服務器地址和端口號
keepalive 64; #設置存活時間。如果不設置可能會產生大量的timewait
}
server {
listen 80;
server_name test.youlunshidai.com;
access_log /var/log/nginx/test_youlunshidai.log; #nginx日志文件
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_set_header Connection "";
proxy_http_version 1.1;
proxy_pass http://nodejs__upstream__test__youlunshidai; #反向代理轉發 http://nodejs__upstream;
}
}
如果還有其他域名和后臺服務器,在目錄下繼續新建對應的域名配置文件即可
參考文章
Nginx反向代理Nodejs – log4js日志IP顯示錯誤
http://www.dataguru.cn/article-3558-1.html