Nginx 详解笔记
一、服务架构简介
常见的 Web 服务架构有以下几种:
| 架构名称 | 组成说明 | 适用场景 |
|---|---|---|
| LNMP | Linux + Nginx + MySQL + PHP | PHP 动态网站 |
| LNMT | Linux + Nginx + MySQL + Tomcat | Java Web 应用 |
| LAMP | Linux + Apache + MySQL + PHP | 传统 PHP + Apache 环境 |
| LNMP | Linux + Nginx + MySQL + Python(如 Django/Flask) | Python Web 应用 |
Nginx 常作为反向代理服务器或静态资源服务器,具有高并发、低内存占用的特点。
二、Nginx 安装方式
1. 编译安装
- 灵活,可自定义模块
- 步骤较复杂,适合定制化需求
2. Yum 安装(EPEL 仓库)
- 版本较旧(如 1.20)
- 配置不易读,不建议生产使用
3. 官方仓库安装(推荐)
步骤:
# 1. 配置 Nginx 官方仓库
cat > /etc/yum.repos.d/nginx.repo << EOF
[nginx-stable]
name=nginx stable repo baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key EOF # 2. 安装 yum -y install nginx # 3. 查看版本 nginx -v # nginx version: nginx/1.24.0
三、Nginx 配置文件解析
主配置文件:/etc/nginx/nginx.conf
核心区块
user nginx; # 运行 Nginx 的用户
worker_processes auto; # 工作进程数,建议设为 CPU 核数
error_log /var/log/nginx/error.log notice; # 错误日志
pid /var/run/nginx.pid; # 进程 PID 文件
事件模块
events {
worker_connections 25532; # 每个进程的最大连接数
}
HTTP 模块
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;
keepalive_timeout 65;
# gzip on; # 可开启压缩
include /etc/nginx/conf.d/*.conf; # 包含业务配置文件
}
四、配置一个简单的虚拟主机
1. 创建业务配置文件
在 /etc/nginx/conf.d/ 下创建 default.conf:
server {
listen 80;
server_name www.game.com;
location / {
root /code;
index index.html index.htm;
}
}
2. 创建网站目录与测试页面
mkdir -p /code
echo "Hello, Nginx!" > /code/index.html
3. 检查语法并重启
nginx -t # 检查语法
systemctl restart nginx # 重启服务
4. 验证服务
netstat -tnulp | grep :80
curl http://localhost
五、多业务配置方式
1. 多 IP 方式
- 为服务器绑定多个 IP
- 每个 IP 对应一个业务
示例:
server {
listen 10.0.0.7:80;
root /code;
}
server {
listen 10.0.0.8:80;
root /data;
}
2. 多端口方式
- 同一 IP,不同端口对应不同业务
示例:
server {
listen 80;
root /code;
}
server {
listen 81;
root /data;
}
3. 多域名方式(企业常用)
- 同一 IP 和端口,通过域名区分业务
示例:
server {
listen 80;
server_name www.game.com;
root /code;
}
server {
listen 80;
server_name www.xbw.com;
root /data;
}
若访问未配置的域名,Nginx 默认返回第一个配置的 server 块内容。
六、Nginx 启动与管理
两种启动方式:
1. 使用 systemctl(推荐)
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl reload nginx # 平滑重载配置
systemctl enable nginx # 开机自启
2. 使用二进制文件
/usr/sbin/nginx # 启动
/usr/sbin/nginx -s stop # 停止
/usr/sbin/nginx -s reload # 重载
/usr/sbin/nginx -t # 检查语法
注意:同一时间只能使用一种方式管理 Nginx。
七、拓展知识
Nginx 性能优化建议:
- 调整
worker_processes为 CPU 核数 - 适当增加
worker_connections - 启用
gzip压缩静态资源 - 使用
expires设置缓存头,减少请求
日志管理:
- 访问日志:
access_log - 错误日志:
error_log - 可配置日志轮转,避免文件过大
安全建议:
- 隐藏 Nginx 版本号(在
http块中加server_tokens off;) - 限制请求频率与连接数
- 使用 HTTPS 并配置 SSL/TLS
八、常见问题与解决
1. 端口占用
netstat -tnulp | grep :80
# 如果被占用,可修改 listen 端口或停止冲突服务
2. 权限问题
- 确保
root目录有正确权限(如755) - 运行用户
nginx需有读取权限
3. 配置生效问题
- 每次修改配置后,执行
nginx -t检查语法 - 使用
systemctl reload nginx平滑重载
Comments NOTHING