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. 进入 Node.JS 官网, 查看当前 Node.JS 的版本
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 不会被外部访问.
来源: http://www.bubuko.com/infodetail-3003107.html