阿里云配置 node.js + Nginx 反向代理
假设你已经配置好node环境,并后台启动了程序(如用pm2开启一个端口)这里只说最基础的配置
- 安装Nginx
- 配置Nginx
- 重启Nginx
- 卸载Nginx
安装Nginx
sudo apt-get update
sudo apt-get install nginx
配置Nginx
sudo vi /etc/nginx/sites-available/default
server {
listen 80;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
可以添加多个server,前提是你在所购买的域名上已经设置好了解析的ip地址
重启Nginx
- 测试配置
sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
- 加载配置
sudo nginx -s reload
- 重启Nginx
sudo service nginx restart
如果想要卸载的话
- 移除文件除了config
sudo apt-get remove nginx nginx-common
- 移除所有
sudo apt-get purge nginx nginx-common
- 移除依赖,为了避免出现意想不到的问题可以不做这一步
sudo apt-get autoremove