1.添加nginx的安装源
vi /etc/yum.repos.d/nginx.repo
2.输入下面内容,并保存退出
[nginx] name=nginx repo baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/ gpgcheck=0 enabled=1
这里是RHEL7
[nginx] name=nginx repo baseurl=http://nginx.org/packages/mainline/rhel/7/$basearch/ gpgcheck=0 enabled=1
3.安装
yum install nginx
4.启动
[root@freesaber tmp]# systemctl start nginx [root@freesaber tmp]# systemctl status nginx ● nginx.service - nginx - high performance web server Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled) Active: active (running) since Thu 2019-03-28 16:16:38 CST; 7s ago Docs: http://nginx.org/en/docs/ Process: 6538 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS) Main PID: 6539 (nginx) CGroup: /system.slice/nginx.service ├─6539 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf └─6540 nginx: worker process
下面安装node
1.安装nvm https://github.com/creationix/nvm
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
2.进入nodejs官网,查看当前nodejs的版本
3.使用nvm安装node
nvm install 10.15.3
4.指定node的版本
[root@freesaber ~]# nvm use v10.15.3 Now using node v10.15.3 (npm v6.4.1) [root@freesaber ~]# nvm alias default v10.15.3 default -> v10.15.3
5.查看版本
[root@freesaber ~]# node -v v10.15.3 [root@freesaber ~]# npm -v 6.4.1
6.编写一段node.js脚本,并运行
vi app.js
const http = require('http')
http.createServer(function(req,res){
res.writeHead(200,{'Content-Type':'text/plan;charset=utf-8'});
res.end('来自8081端口的node响应')
}).listen(8081);
node app.js
7.在阿里云的安全组中添加端口入规则
8.访问
下面是我的两个域名,默认都是访问的服务器的80端口
http://www.freesaber.cn/ http://nodetree.freesaber.cn/
由于没有配置nginx访问这两个域名,都会得到同一个返回页面
下面将我的二级域名nodetree.freesaber.cn反向代理到我的8081端口,新开一个shell远程窗口,不要关闭node
1.nginx的目录结构和配置文件,我们新增的配置放在conf.d目录下
[root@freesaber nginx]# cd /etc/nginx [root@freesaber nginx]# ll total 40 drwxr-xr-x 2 root root 4096 Mar 28 16:14 conf.d -rw-r--r-- 1 root root 1007 Mar 26 22:27 fastcgi_params -rw-r--r-- 1 root root 2837 Mar 26 22:27 koi-utf -rw-r--r-- 1 root root 2223 Mar 26 22:27 koi-win -rw-r--r-- 1 root root 5231 Mar 26 22:27 mime.types lrwxrwxrwx 1 root root 29 Mar 28 16:14 modules -> ../../usr/lib64/nginx/modules -rw-r--r-- 1 root root 643 Mar 26 22:25 nginx.conf -rw-r--r-- 1 root root 636 Mar 26 22:27 scgi_params -rw-r--r-- 1 root root 664 Mar 26 22:27 uwsgi_params -rw-r--r-- 1 root root 3610 Mar 26 22:27 win-utf
[root@freesaber nginx]# cat nginx.conf user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; }
2.复制一份默认的配置文件
[root@freesaber nginx]# cd /etc/nginx/conf.d [root@freesaber conf.d]# ls default.conf [root@freesaber conf.d]# cp default.conf nodetree.conf [root@freesaber conf.d]# ll total 8 -rw-r--r-- 1 root root 1093 Mar 26 22:25 default.conf -rw-r--r-- 1 root root 1093 Mar 28 16:58 nodetree.conf [root@freesaber conf.d]#
3.修改内容新增的nodetree.conf,并保存退出
upstream freesaber{ server 127.0.0.1:8081; } server { listen 80; server_name nodetree.freesaber.cn; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forward-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-Nginx-Proxy true; proxy_pass http://freesaber; proxy_redirect off; } }
4.重启nginx,访问二级域名,这时请求被反向代理到了8081端口的node服务,而对外的服务都是nginx的80端口。其实这个时候,可以从阿里云的安全组中删除8081的入规则。因为nginx代理到本地的8081,而8081不会被外部访问。