对于零基础用户快速搭建Nginx线上代理,第一步应完成以下关键操作:
- 确认服务器操作系统(推荐Ubuntu/CentOS)
- 更新系统:sudo apt update && sudo apt upgrade -y
(Ubuntu)或 sudo yum update -y
(CentOS)
# Ubuntu/Debian
sudo apt install nginx -y
# CentOS/RHEL
sudo yum install epel-release -y
sudo yum install nginx -y
sudo ufw allow 80/tcp # HTTP
sudo ufw allow 443/tcp # HTTPS
# 或使用firewalld(CentOS)
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
sudo systemctl start nginx
sudo systemctl enable nginx
curl http://localhost # 应看到欢迎页面
- 主配置文件:/etc/nginx/nginx.conf
- 站点配置目录:/etc/nginx/conf.d/
- 默认网站根目录:/usr/share/nginx/html
为什么这是第一步: 1. 先确保基础服务正常运行再配置代理规则 2. 避免因环境问题导致后续配置失败 3. 通过默认页验证可快速排查安装问题
后续步骤建议:
1. 备份默认配置:sudo cp /etc/nginx/nginx.conf{,.bak}
2. 学习基础配置语法
3. 在conf.d/
目录创建独立的代理配置文件
常见问题排查:
- 端口冲突:netstat -tulnp | grep 80
- 权限问题:确保Nginx用户(通常为www-data
或nginx
)有访问权限
- 配置测试:sudo nginx -t