Nginx 基础
Nginx 是一个开源、轻量级、高性能的 Web 服务器,也用作 HTTP/HTTPS 的反向代理服务器、负载均衡器和 HTTP 缓存。
一、安装方式
方式一:yum 安装
适用于 CentOS / RHEL 7+:
yum install -y epel-release
yum install -y nginx
systemctl start nginx
systemctl enable nginx
方式二:源码编译安装
# 1. 安装依赖
yum install -y gcc pcre-devel zlib-devel
# 2. 创建运行用户
useradd -M -s /sbin/nologin nginx
# 3. 下载解压
wget http://nginx.org/download/nginx-1.24.0.tar.gz
tar -axf nginx-1.24.0.tar.gz
cd nginx-1.24.0
# 4. 编译安装
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
make && make install
# 5. 启动
/usr/local/nginx/sbin/nginx
路径优化与开机自启:
export PATH="/usr/local/nginx/sbin:$PATH"
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
二、配置文件结构
核心文件
| 配置文件 | 用途 |
|---|---|
nginx.conf | 主配置文件 |
conf.d/*.conf | 子配置文件 |
nginx.conf 基本结构
worker_processes 1; # 全局块
events {
worker_connections 1024; # events 块
}
http {
include mime.types;
default_type application/octet-stream;
upstream backend { # 后端服务器组
server 192.168.1.10:8080;
}
server { # server 块(虚拟主机)
listen 80;
server_name localhost;
location / { # location 块
root html;
index index.html;
}
}
}
三、基础命令
nginx # 启动
nginx -s stop # 停止
nginx -s reload # 重载配置(不中断服务)
nginx -t # 检查配置语法
nginx -V # 查看版本与编译参数
四、虚拟主机
| 类型 | 示例 | 说明 |
|---|---|---|
| 基于端口 | listen 81 | 不同端口不同站点 |
| 基于 IP | listen 192.168.1.1:80 | 多网卡 |
| 基于域名 | server_name www.a.com | 最常用 |
server {
listen 80;
server_name www.a.com;
location / {
root /var/www/a;
index index.html;
}
}
server {
listen 80;
server_name www.b.com;
location / {
root /var/www/b;
index index.html;
}
}
五、Location 匹配规则
匹配优先级
优先级从高到低:
1. = 精确匹配 location = /abc
2. ^~ 前缀优先匹配 location ^~ /static/
3. ~/~* 正则匹配 location ~ \.php$
4. 无前缀 普通前缀匹配 location /abc
root 与 alias 区别
# root:URI 追加到目录后
location /images/ {
root /var/www/html;
}
# 请求 /images/logo.png → /var/www/html/images/logo.png
# alias:替换匹配路径
location /images/ {
alias /var/data/photos/;
}
# 请求 /images/logo.png → /var/data/photos/logo.png
六、反向代理
server {
listen 80;
server_name api.example.com;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
常用参数:
| 参数 | 作用 |
|---|---|
proxy_pass | 后端地址 |
proxy_connect_timeout | 连接超时 |
proxy_read_timeout | 读取超时 |
proxy_buffer_size | 响应头缓冲区 |
七、负载均衡
upstream backend {
# 加权轮询
server 192.168.1.10:8080 weight=3;
server 192.168.1.11:8080 weight=2;
server 192.168.1.12:8080 weight=1;
}
server {
listen 80;
location / {
proxy_pass http://backend;
}
}
算法:
| 算法 | 说明 |
|---|---|
round-robin(默认) | 轮询 |
weight | 加权轮询 |
ip_hash | IP 哈希(Session 保持) |
least_conn | 最小连接数 |
健康检查:
upstream backend {
server 192.168.1.10:8080 max_fails=3 fail_timeout=30s;
server 192.168.1.11:8080 max_fails=3 fail_timeout=30s;
}
max_fails=3:连续失败 3 次认为不可用fail_timeout=30s:30 秒内不再转发
八、HTTPS
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/nginx/ssl/example.com.pem;
ssl_certificate_key /etc/nginx/ssl/example.com.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
location / {
root /var/www/html;
index index.html;
}
}
# HTTP 跳转 HTTPS
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
九、PHP 解析(FastCGI)
server {
listen 80;
root /var/www/html;
location / {
index index.php index.html;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
| 通信方式 | 优点 |
|---|---|
| Unix Socket | 更快更安全,仅本地 |
| TCP Socket(127.0.0.1:9000) | 可跨主机 |
十、与 Tomcat 配合
upstream tomcat {
server 127.0.0.1:8080;
server 127.0.0.1:8081;
}
server {
listen 80;
server_name app.example.com;
# 静态资源 Nginx 直接处理
location /static/ {
root /var/www/app;
expires 7d;
}
# 动态请求转发 Tomcat
location / {
proxy_pass http://tomcat;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
十一、配置速查
| 场景 | 配置 |
|---|---|
| 静态缓存 | expires 7d; |
| 上传限制 | client_max_body_size 10m; |
| 禁止敏感文件 | location ~ /\. { deny all; } |
| URL 重写 | rewrite ^/old$ /new permanent; |
| 配置验证 | nginx -t |
| 重载配置 | nginx -s reload |
关联文档
- Tomcat 基础 — Tomcat 与 Nginx 配合
- 20-areas/cloud-native/kubernetes/network/K8s Ingress 详解 — Ingress Nginx
- 20-areas/cloud-native/network/protocol/HTTP 协议 — HTTP 协议
- 面试题/Nginx 面试题 — Nginx 面试题(待整理)