nginx常用配置解析

常见 Nginx 配置的解析和使用说明,包括基本配置、反向代理、负载均衡、缓存等常用场景

1. 基础配置

配置文件路径:

  • 主配置文件:/etc/nginx/nginx.conf
  • 站点配置:通常在 /etc/nginx/conf.d/*.conf 或 /etc/nginx/sites-available/ 中。
    常见结构:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
worker_processes auto;  # 自动分配工作进程数
events {
worker_connections 1024; # 每个进程的最大连接数
}

http {
include mime.types; # 文件类型映射
default_type application/octet-stream; # 默认 MIME 类型
sendfile on; # 优化传输效率
keepalive_timeout 65; # 连接保持时间

server {
listen 80; # 监听端口
server_name example.com; # 绑定域名

location / {
root /usr/share/nginx/html; # 根目录
index index.html index.htm; # 默认首页
}

error_page 404 /404.html; # 自定义 404 错误页面
}
}

2. 反向代理

Nginx 常用作反向代理,将请求转发到后端服务。

1
2
3
4
5
6
7
8
9
10
11
server {
listen 80;
server_name example.com;

location / {
proxy_pass http://127.0.0.1:8080; # 转发到后端服务
proxy_set_header Host $host; # 保留原始请求的 Host 头
proxy_set_header X-Real-IP $remote_addr; # 客户端真实 IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

常用指令:

  • proxy_pass:指定后端服务地址。
  • proxy_set_header:自定义请求头转发。
  • proxy_connect_timeout:连接超时时间。
  • proxy_read_timeout:后端响应超时时间。
  1. 负载均衡
    Nginx 支持多种负载均衡策略。

配置示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
upstream backend {
server 127.0.0.1:8080; # 后端服务 1
server 127.0.0.1:8081; # 后端服务 2
}

server {
listen 80;
server_name example.com;

location / {
proxy_pass http://backend; # 转发到 upstream 定义的后端组
}
}

负载均衡策略:

  • 轮询(默认): 请求按顺序分配。
  • 权重: 设置每个后端的请求分配权重。
1
2
3
4
upstream backend {
server 127.0.0.1:8080 weight=3;
server 127.0.0.1:8081 weight=1;
}

IP 哈希: 按客户端 IP 分配请求,保持会话一致性。

1
2
3
4
5
upstream backend {
ip_hash;
server 127.0.0.1:8080;
server 127.0.0.1:8081;
}

4. 静态文件服务

为网站提供高效的静态文件服务。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server {
listen 80;
server_name static.example.com;

location / {
root /var/www/static; # 静态文件存放目录
index index.html;
}

location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|ttf|svg|eot)$ {
expires 30d; # 设置缓存时间
access_log off; # 禁用日志
}
}

5. 缓存配置

Nginx 支持代理缓存和本地缓存。

代理缓存:

1
2
3
4
5
6
7
8
9
10
11
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:10m inactive=60m max_size=1g;

server {
location / {
proxy_cache my_cache; # 使用定义的缓存区域
proxy_pass http://backend;
proxy_cache_key $host$uri$is_args$args; # 缓存键
proxy_cache_valid 200 302 10m; # 有效响应缓存时间
proxy_cache_valid 404 1m; # 404 响应缓存时间
}
}

静态文件缓存:

1
2
3
4
5
location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|ttf|svg|eot)$ {
expires max; # 缓存有效期设置为最大值
log_not_found off; # 禁止记录 404 日志
access_log off; # 禁止记录访问日志
}

6. HTTPS 配置

为站点配置 SSL 加密。

配置示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server {
listen 443 ssl;
server_name example.com;

ssl_certificate /etc/nginx/ssl/example.com.crt; # SSL 证书路径
ssl_certificate_key /etc/nginx/ssl/example.com.key; # SSL 密钥路径

location / {
proxy_pass http://127.0.0.1:8080;
}
}

server {
listen 80;
server_name example.com;

return 301 https://$host$request_uri; # HTTP 跳转到 HTTPS
}

7. 日志管理

Nginx 提供灵活的访问和错误日志配置。

1
2
3
4
5
6
7
http {
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;
error_log /var/log/nginx/error.log warn;
}

总结

功能 核心指令
静态文件服务 root、index、expires
反向代理 proxy_pass、proxy_set_header
负载均衡 upstream、负载均衡策略(轮询、权重、IP 哈希等)
缓存配置 proxy_cache_path、proxy_cache
HTTPS 支持 ssl_certificate、ssl_certificate_key、强制 HTTPS 跳转
日志管理 log_format、access_log、error_log
-------------本文结束感谢您的阅读-------------

欢迎关注我的其它发布渠道