Nginx
docker部署
初始化操作
# 创建目录
mkdir -p ./nginx/html ./nginx/conf.d
# 拷贝nginx配置文件
docker run --rm -it nginx:latest cat /etc/nginx/conf.d/default.conf > ./nginx/conf.d/default.conf
docker run --rm -it nginx:latest cat /etc/nginx/nginx.conf > ./nginx/nginx.conf
docker run --rm -it nginx:latest cat /usr/share/nginx/html/index.html > ./nginx/html/index.html创建yaml
docker-compose.yaml
version: '3.8'
services:
nginx:
image: nginx:latest
container_name: nginx # 容器名称
restart: always # 自动重启
environment:
- TZ=Asia/Shanghai # 设置时区
ports:
- "80:80" # 映射端口
deploy:
resources:
limits:
cpus: "0.5" # 限制 CPU 核心数
memory: "512M" # 限制内存大小
volumes:
- ./nginx/html:/usr/share/nginx/html # 数据持久化
- ./nginx/conf.d:/etc/nginx/conf.d # 配置持久化
- ./nginx/nginx.conf:/etc/nginx/nginx.conf # 配置持久化启动
docker compose up -d
yum部署
执行
yum install -y nginx nginx-all-modules修改,添加最大文件打开数配置
/usr/lib/systemd/system/nginx.service
LimitNOFILE=65000启动
systemctl start nginx
# 开机自启
systemctl enable nginx
# 重新加载systemctl配置
systemctl daemon-reload配置示例
80与443同时使用
server {
listen 80;
listen 443 ssl;
server_name test.hxq.cn;
# 证书配置
ssl_certificate ssl/test.hxq.cn.crt;
ssl_certificate_key ssl/test.hxq.cn.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
# 压缩配置
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_types application/octet-stream application/atom+xml application/geo+json application/javascript application/x-javascript application/json application/ld+json application/manifest+json application/rdf+xml application/rss+xml application/xhtml+xml application/xml font/eot font/otf font/ttf image/svg+xml text/css text/javascript text/plain text/xml;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_pass http://192.168.1.2:8080;
}
}80强制跳转443
# 第一个server块:专门处理80端口请求,强制跳转至443 HTTPS
server {
listen 80;
server_name test.hxq.cn;
# 核心跳转配置:301永久重定向(SEO友好,浏览器会缓存跳转规则)
# $scheme 自动识别协议,$host 自动获取当前server_name,$request_uri 保留完整请求路径和参数
return 301 https://$host$request_uri;
}真实ip转发配置
# 传递客户端直接IP(若Nginx前无反向代理/CDN,此值即为真实客户端IP)
proxy_set_header X-Real-IP $remote_addr;
# 传递IP链路(包含客户端真实IP+各级代理IP)
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 传递客户端原始请求协议(http/https)
proxy_set_header X-Forwarded-Proto $scheme;
# 传递客户端原始请求主机名
proxy_set_header X-Forwarded-Host $host;指定host转发
# 两种方案可选,根据后端服务需求选择其一
# 方案1:传递原始客户端请求的Host(即test.hxq.cn,推荐,保持请求一致性)
# 方案2:传递后端服务需要的自定义Host(例如后端要求Host为backend.hxq.cn,按需修改)
# proxy_set_header Host backend.hxq.cn;
proxy_set_header Host $host;url下划线支持
# 开启请求头中的下划线支持(默认off,会忽略含下划线的请求头)
# 补充:Nginx默认支持URL路径中的下划线,此配置主要解决"请求头下划线"和"反向代理传递下划线参数"问题
underscores_in_headers on; 跨域配置
# 允许请求来源(动态适配前端域名,比硬编码*更安全)
add_header Access-Control-Allow-Origin $http_origin;
# 允许的HTTP请求方法
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS,PATCH;
# 允许的请求头
add_header Access-Control-Allow-Headers Origin,Content-Type,Accept,Authorization,X-Real-IP,X-Forwarded-For;
# 允许携带Cookie/认证信息(配合前端withCredentials使用)
add_header Access-Control-Allow-Credentials true;
# 预检请求(OPTIONS)缓存时间(1天),减少预检请求次数
add_header Access-Control-Max-Age 86400;
# 处理预检请求(OPTIONS):直接返回204,无需转发到后端服务
if ($request_method = OPTIONS) {
return 204;
}长连接配置
# 基础:启用HTTP/1.1协议(WebSocket代理的前提)
proxy_http_version 1.1;
# 核心1:传递客户端的Upgrade请求头给后端服务
proxy_set_header Upgrade $http_upgrade;
# 核心2:强制设置Connection请求头为"upgrade",触发协议升级
proxy_set_header Connection "upgrade";ailis和root
| 对比项 | root 指令 | alias 指令 |
|---|---|---|
| 路径拼接规则 | 最终路径 = root 路径 + 完整请求 URI | 最终路径 = alias 路径 + (请求 URI - location 匹配部分) |
| 生效层级 | http、server、location 均可 | 仅 location 块中有效 |
| 对 location 匹配要求 | 无特殊要求,支持 =、 |
location(=、^~、普通前缀)时最优,正则 location 中不推荐使用(可能失效) |
| 目录末尾 | 无强制要求,有无 / 对拼接结果影响较小 | 建议添加 /,否则多级路径拼接可能出现错误 |
| 适用场景 | 匹配整个网站根目录、常规静态文件目录(URI 与目录结构一致) | 匹配局部路径、需要替换 URI 前缀的场景(URI 与目录结构不一致) |
斜杠说明
proxy_pass 末尾是否加 /,核心影响Nginx 向后端转发请求时的 URI 拼接规则,分为两种核心场景:
- proxy_pass 末尾不加 /:Nginx 会保留客户端请求的「完整 URI」(包含 location 匹配的前缀部分),拼接在后端地址后。
- proxy_pass 末尾加 /:Nginx 会剔除客户端请求 URI 中与 location 匹配的前缀部分,仅将剩余 URI 拼接在后端地址后。
日志格式配置
# 定义名为 main 的Nginx访问日志格式
# 格式规范:key:"value" 形式包裹字段,避免特殊字符破坏日志结构;字段按「请求信息→状态信息→业务信息→客户端信息」归类
log_format main '$remote_addr - $remote_user [$time_local] ' # 基础连接信息
# $remote_addr: 客户端IP地址(Nginx直接对接的客户端,无前端代理时即为真实IP)
# $remote_user: HTTP基本认证用户名(未配置auth_basic时显示为"-")
# $time_local: 服务器本地时间(格式:dd/MMM/yyyy:HH:mm:ss +时区)
'requesthost:"$http_host"; ' # 客户端请求的主机名(域名/IP:端口,如 test.hxq.cn:8080)
'"$request" ' # 完整请求行(格式:请求方法 请求URI HTTP协议版本,如 GET /test HTTP/1.1)
'requesttime:"$request_time"; ' # 整个请求的处理耗时(单位:秒,精确到毫秒,如 0.023)
'$status $body_bytes_sent ' # 响应状态信息
# $status: Nginx返回给客户端的HTTP状态码(如 200成功、502网关错误)
# $body_bytes_sent: Nginx发送给客户端的响应体字节数(不含响应头)
'"$http_referer" ' # 请求来源页面(Referer请求头,直接访问时显示为"-")
'request_body:"$request_body" ' # 客户端请求体内容(仅POST/PUT等方法有值,大体积请求体可能为空)
'content_type:"$http_content_type" ' # 请求体的媒体类型(Content-Type请求头,无请求体时显示为"-")
'request_length:"$request_length" ' # 客户端请求的总字节数(包含请求头+请求体)
'upstream_response_time:"$upstream_response_time" ' # 反向代理专用:Nginx等待后端服务响应的耗时(单位:秒,多个后端用逗号分隔)
'upstream_addr:"$upstream_addr" ' # 反向代理专用:实际转发的后端服务地址(IP:端口,多个后端用逗号分隔)
'"$http_user_agent" ' # 客户端用户代理标识(浏览器/爬虫类型,如 Chrome/143.0.0.0)
'x_forwarded_for:"$http_x_forwarded_for" ' # 客户端真实IP链路(X-Forwarded-For请求头,无前端代理时显示为"-")
'access_control_request_headers:"$http_access_control_request_headers"'; # 跨域预检专用:前端声明的自定义请求头(非OPTIONS请求时显示为"-")