vi /etc/yum.repos.d/Nginx.repo
[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; }
[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]#
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不会被外部访问。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。