如何在CentOS7上为Nginx配置Node.js多站点

1.安装Node.js

yum install epel-release
yun install nodejs

固然你也能够下载Node.js的源码包,经过./configure & make & make install 来安装。html

2.安装Nginx

2.1 添加nginx的源到rpm

rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

若是你的系统不是Centos7怎么办?node

到这里本身找:http://nginx.org/packages/nginx

2.2 安装nginx

yum install nginx

3.配置nginx

3.1 修改ngnix配置支持include配置文件

凡事先备份,当心驶得万年船啊!shell

cp /etc/nginx/nginx.conf  /etc/nginx/nginx.conf.bak
mkdir /etc/nginx/sites-avaiable
mkdir /ect/nginx/sites-enabled

3.2 编辑 /etc/nginx/nginx.conf ,添加对多站点配置的支持

在 http { } 块的末尾加上下面两行:(即在在 } 的前面添加)centos

include /etc/nginx/sites-enabled/*.conf;
server_names_hash_bucket_size 64;

3.3 创建站点配置,分别以www.gso8.com和www.gso9.com 为例:

cp /etc/nginx/conf.d/default.conf /etc/nginx/sites-available/gso8.com.conf
cp /etc/nginx/conf.d/default.conf /etc/nginx/sites-available/gso9.com.conf

eg:gso8.com.conf的配置以下,将对gso8.com的访问转发到本机的8008端口
浏览器

server {
    listen       80;
    server_name  gso8.com www.gso8.com;
    location / {
        proxy_pass http://localhost:8008;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
    error_page  500 502 503 504  /50x.html;
    location = /50x.html {
        root  /usr/share/nginx/html;
    }
}


3.4 创建available网站配置到enabled网站配置的软连接

这样作的好处是,能够灵活关闭部分站点
网站

 ln -s /etc/nginx/sites-available/gso8.com.conf /etc/nginx/sites-enabled/gso8.com.conf
 ln -s /etc/nginx/sites-available/gso9.com.conf /etc/nginx/sites-enabled/gso9.com.conf

4.重启Nginx服务

systemctl restart nginx

或者也能够 service nginx restartspa

若是遇到错误: nginx -t -c /etc/nginx/nginx.conf 检查下配置文件是否正确rest

最后访问你的浏览器,看看网站是否正常!code


最后,给咱的www.gso8.com的站点打个小广告,各位须要用谷歌搜索的同窗不妨访问试试看。

相关文章
相关标签/搜索