nginx安装
安装
依次运行下面命令
1 2 3 4 5 6 7 8 9 10 11 12
| rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum info nginx
yum install nginx
service nginx start
systemctl enable nginx
nginx -v
|
之后就可以访问公网ip进行测试了
成功则会出现Welcome to nginx!
扩展知识
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| vim /etc/nginx/conf.d/default.conf
/usr/sbin/nginx -s reload
systemctl start firewalld.service 关闭一个服务 systemctl stop firewalld.service
systemctl restart firewalld.service
systemctl status firewalld.service
systemctl enable firewalld.service
systemctl disable firewalld.service
systemctl is-enabled firewalld.service;echo $?
systemctl list-unit-files|grep enabled
|
可能遇到的问题
1
| 具体情况如下 1。本机能ping通虚拟机 2。虚拟机也能ping通本机 3。虚拟机能访问自己的web 4。本机无法访问虚拟己的web 这个问题的原因是服务器的80端口没有打开或防火墙没有关闭
|
解决方案:
1 2 3 4
| systemctl status firewalld.service systemctl stop firewalld.service systemctl disable firewalld.service systemctl status firewalld.service
|
阿里云域名解析,Nginx二级域名的配置
需求
我的nodeAPI项目跑在3000端口上,但是购买的阿里云ECS有nginx服务支持80端口。我可以这样访问我的项目,api.shifeng1993.com:3000,但是哪里有见过域名后面加端口的,因此,将3000端口映射到80上。
配置云解析
在阿里云解析将二级域名解析到 ECS 的IP地址,
注意不要写端口号。添加a记录
端口映射
在项目目录下新建nginx配置文件
开启node项目
1 2 3 4 5 6
| pm2 start app.js
pm2 startup
pm2 save
|
新建 yourNodeProject/nginx.conf
1 2 3 4 5 6 7 8 9 10 11
| server { listen 80; server_name api.shifeng1993.com; location / { root /home/mongo-server; index static/index.html proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; proxy_pass http://127.0.0.1:3000; } }
|
将项目配置软链到nginx配置下
1
| ln -s yourNodeProject/nginx.conf /etc/nginx/conf.d/lab.beacelee.com.conf
|
重启nginx
1
| systemctl restart nginx.service
|
测试
输入你的api域名,就可以看到对应的服务器转发端口了。