Nginx 配置文件整体分为:全局块、events 块、http 块、server 块、location 块 五层结构。主配置一般为 nginx.conf,业务站点通常放在 conf.d/*.conf 下。
配置语法:
指令 参数;分号不能省略;#为注释。
一、整体结构概览
# 【全局块】运行级别,不属于任何块
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /run/nginx.pid;
# 【events块】网络模型相关
events {
worker_connections 1024;
}
# 【http块】HTTP服务核心,可包含多个server
http {
include 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;
# 虚拟主机,一个server对应一个网站
server {
listen 80;
server_name localhost;
# 路由匹配块
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
1. 全局块(最外层,不在任何 {})
影响 Nginx 整体运行。
# 指定Nginx工作进程运行用户,Linux安全建议用nginx,不要root
user nginx nginx;
# worker进程数,auto=等于CPU核心数,CPU密集建议等于核心数
worker_processes auto;
# 错误日志路径、日志级别:debug|info|notice|warn|error|crit
error_log /var/log/nginx/error.log warn;
# pid文件,记录nginx主进程ID
pid /run/nginx.pid;
# 单个worker最大打开文件句柄数,大于系统ulimit‑n
worker_rlimit_nofile 65535;
2. events {} 块:连接模型配置
控制网络 IO 模型、并发连接能力
events {
# 每个worker进程最大并发连接数,总最大连接=worker_processes * worker_connections
worker_connections 1024;
# 使用epoll IO多路复用(Linux),高性能,linux默认开启
use epoll;
# 收到请求后尽快交出连接,降低上下文切换
multi_accept on;
}
最大并发不是
worker_connections,长连接场景要区分。
3. http {} 块:HTTP 协议全局配置
可以包含多个 server{},处理 http、mime、日志、缓存、压缩、代理公共参数。
http {
# 引入媒体类型映射文件,扩展名‑>Content‑Type
include 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"';
# 访问日志,使用main格式
access_log /var/log/nginx/access.log main;
# ----------------文件传输优化----------------
# sendfile零拷贝,静态文件高性能,必须on
sendfile on;
# 大文件减少报文碎片
tcp_nopush on;
tcp_nodelay on;
# keepalive长连接超时,客户端多久没动作断开
keepalive_timeout 65;
# 单长连接最多请求次数
keepalive_requests 100;
# 开启gzip压缩,文本类压缩,图片视频不要压
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml;
gzip_min_length 1k;
# 允许include拆分配置,conf.d下所有conf虚拟主机
include conf.d/*.conf;
}
http 常用内置变量(日志、location、if 常用)
4. server {} 块:虚拟主机
一个 server = 一个虚拟站点;靠 listen+server_name 区分不同网站。
server {
# 监听端口;listen 80; listen 443 ssl; listen 8080;
listen 80;
# 域名,多个空格隔开;支持通配符 *.xxx.com
server_name www.test.com test.com;
# 站点根目录,相对路径基于nginx安装目录
root /usr/share/nginx/html;
# 默认首页文件
index index.html index.htm index.php;
# 错误页面跳转
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location / {
# ...
}
}
虚拟主机匹配优先级:精确域名 > 通配符前缀 > 通配符后缀 > 默认 server
5. location {} 块:URI 路由匹配(重点)
语法:location [匹配修饰符] uri { ... }
匹配修饰符优先级(从高到低)
=精确匹配,完全相等,最高优先级^~前缀匹配,匹配成功不再检查正则~区分大小写正则匹配~*不区分大小写正则匹配无修饰符:普通前缀匹配(最低)
示例:
# 精确匹配 / ,只有访问 http://xxx/ 命中
location = / {
root html;
index index.html;
}
# ^~ 前缀匹配,匹配以/api/开头,命中后跳过正则
location ^~ /api/ {
proxy_pass http://127.0.0.1:8080;
}
# ~* 正则,不区分大小写,匹配图片后缀
location ~* \.(jpg|png|gif|js|css)$ {
expires 7d; # 浏览器缓存7天
}
# 默认兜底,所有没匹配到的路由
location / {
root html;
index index.html;
}
location 核心指令
root vs alias(极易混淆)
root:最终路径 =root路径 + uri
location /static/ {
root /data/www;
}
# 访问 /static/a.js → 文件 /data/www/static/a.js
alias:最终路径 =alias替换uri,uri 会被 alias 替换掉
location /static/ {
alias /data/www/;
}
# 访问 /static/a.js → 文件 /data/www/a.js
alias 结尾建议加
/;root 不要加。
proxy_pass 反向代理
location /api/ {
proxy_pass http://127.0.0.1:8080/;
# 传递真实客户端IP给后端
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_connect_timeout 30s;
proxy_read_timeout 60s;
}
proxy_pass 末尾
/有无,会影响路径拼接,坑最多。
proxy_pass http://127.0.0.1:8080不带斜杠:后端收到/api/xxx
proxy_pass http://127.0.0.1:8080/带斜杠:后端收到/xxx
expires 静态资源缓存
location ~* \.(js|css|png|jpg)$ {
expires 7d; # Cache‑Max‑Age 7天
add_header Cache‑Control "public";
}
return 重定向
# 302临时跳转
return 302 https://www.baidu.com;
# 301永久跳转
return 301 https://www.baidu.com;
# 返回http状态码
return 403;
6. if 条件判断(慎用,坑多)
nginx 的 if 是重写模块指令,不是通用 if,复杂逻辑尽量用 map。
location / {
if ($http_user_agent ~* "curl") {
return 403;
}
}
7. HTTPS ssl 配置示例 (server 块)
server {
listen 443 ssl;
server_name www.test.com;
ssl_certificate cert/test.crt;
ssl_certificate_key cert/test.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html;
}
}
# http 80强制跳转https
server {
listen 80;
server_name www.test.com;
return 301 https://$host$request_uri;
}
常用运维命令
# 检查配置语法是否正确(修改配置必执行)
nginx -t
# 平滑重载配置,不中断业务
nginx -s reload
# 停止nginx
nginx -s stop
nginx -s quit
常见坑总结
配置末尾分号不要丢;
root和alias路径拼接逻辑差异;proxy_pass末尾斜杠/影响转发路径;location 匹配顺序:
=>^~>~/~*> 普通前缀;worker_connections是单进程上限,不是全局总并发;if 尽量少用,复杂条件优先
map;include 拆分配置便于维护,不要把所有站点写进 nginx.conf。
本文原创作者:易君召,详见:https://www.yijunzhao.cc/about,转载请注明出处。
原文链接
欢迎访问