一、前言
Nginx 是现代 Web 架构中不可或缺的组件,广泛用于反向代理、负载均衡、静态资源服务和安全网关。
二、基础部署与核心配置
1. 常见安装方式(以 Ubuntu 为例)
sudo apt update
sudo apt install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
2、核心目录概览
目录 | 作用 |
---|---|
/etc/nginx/ |
主配置文件目录(最重要) |
/usr/sbin/nginx |
Nginx 可执行程序(命令行工具) |
/var/log/nginx/ |
日志文件目录(access.log、error.log) |
/usr/share/nginx/ |
默认静态文件根目录(HTML 文件) |
/var/cache/nginx/ |
缓存文件目录(如代理缓存,需要开启缓存在存在这个目录) |
/usr/share/man/man8/nginx.8.gz |
帮助手册(man nginx ) |
/run/nginx.pid |
存放主进程 PID 的文件 |
详细目录与文件说明
2.1 /etc/nginx/
—— 主配置目录(⭐ 核心)
这是你最常打交道的目录,包含所有配置文件。
主要文件:
文件 | 说明 |
---|---|
nginx.conf |
主配置文件,定义全局设置、events、http 块等 |
mime.types |
定义文件扩展名与 MIME 类型的映射(如 .css → text/css ) |
conf.d/ |
子配置片段目录,通常存放 *.conf 文件(如 ssl.conf ) |
sites-available/ |
可用的站点配置(Debian/Ubuntu 特有) |
sites-enabled/ |
启用的站点配置,通过软链接指向 sites-available/ |
💡 Debian/Ubuntu 使用
sites-available
和sites-enabled
实现“多站点管理”,类似 Apache。 CentOS/RHEL 通常只用conf.d/
。每次修改配置前,建议备份并在修改后测试
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak sudo nginx -t # 检查配置是否有问题 sudo nginx -s reload # 向主进程发送信号,重新加载配置文件,热重启
2.2 /var/log/nginx/
—— 日志目录(⭐ 运维必备)
文件 | 说明 |
---|---|
access.log |
访问日志:记录每个 HTTP 请求(IP、时间、URL、状态码等) |
error.log |
错误日志:记录 Nginx 启动、运行、配置错误、后端连接失败等 |
2.3 /usr/share/nginx/html/
—— 默认静态资源根目录
- 这是 Nginx 默认的 网站根目录(Document Root)。
- 当你访问
http://服务器IP
时,Nginx 会返回这个目录下的index.html
。 - 你可以将前端打包文件(如 Vue/React 的
dist/
)复制到这里。
⚠️ 权限:确保
www-data
(Ubuntu)或nginx
(CentOS)用户有读取权限
2.4. /var/cache/nginx/
—— 缓存目录
-
用于存储 代理缓存(如反向代理后端 API 或静态资源)。
-
需要在配置中显式启用:
# 定义缓存区 # 在 http 块中定义缓存区 proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=10ginactive=60muse_temp_path=off;server {listen 80;server_name cached-api.example.com;location /api/ {proxy_pass http://backend_servers;# 启用缓存proxy_cache my_cache;proxy_cache_valid 200 302 10m; # 成功响应缓存 10 分钟proxy_cache_valid 404 1m;proxy_cache_use_stale error timeout updating; # 缓存容错机制} }
-
目录结构由 Nginx 自动管理,无需手动操作。
2.5. /usr/share/man/man8/nginx.8.gz
—— 帮助手册
-
这是
man nginx
命令使用的帮助文档。 -
压缩格式(
.gz
),但man
命令会自动解压。 -
查看方式:
man nginx
一个Nginx配置例子如下
# main段配置信息
user www-data; # 指定运行 Nginx 的 woker 子进程的属主和属组,其中组可以不指定
pid /run/nginx.pid; # Nginx 服务启动时的 pid 存放位置
worker_processes auto; # Nginx 进程数,一般设置为和 CPU 核数一样
# worker_cpu_affinity 0001 0010 0100 1000; # 将每个 worker 进程绑定到特定 CPU 核心(CPU 亲和性)worker_rlimit_nofile 20480; # 设置每个 Nginx worker 进程能打开的最大文件描述符数 相当于在进程启动时执行 ulimit -n 20480
worker_rlimit_core 50M; # 限制每个 worker 进程可生成的核心转储文件(core dump)大小当 Nginx worker 崩溃(如段错误)时,系统会将其内存状态写入一个 core.* 文件 可选 0 或者 unlimited
working_directory /opt/nginx/tmp; # core 文件生成路径该目录必须存在且 Nginx 用户(www-data)有写权限:worker_priority -10; # 设置 worker 进程的调度优先级(Linux nice 值)数值范围:-20(最高优先级)到 +19(最低优先级) 默认值:0
worker_shutdown_timeout 5s; # 设置 worker 进程平滑关闭的最大等待时间
timer_resolution 100ms; # 配置事件定时器的系统调用频率,提升性能 适合大多数场景error_log /var/log/nginx/error.log; # Nginx 的错误日志存放目录
include /etc/nginx/modules-enabled/*.conf; # 导入已配置模块# events段配置信息
events {use epoll; # 使用epoll的I/O模型 Linux上最高效 推荐注释掉这行配置 让Nginx自行选择worker_connections 768; # 每个 worker 最大连接数multi_accept on; # 一次性接收多个连接# accept_mutex on; # 开启“互斥锁(Mutex)”机制,确保同一时间只有一个 worker 进程在监听并接受新连接 建议采用默认值关闭 内核级 EPOLLEXCLUSIVE(自 4.5+)已解决惊群问题 在高并发场景下,accept_mutex off 的吞吐量通常更高
}# http段配置信息,是 Nginx 配置文件中最核心的部分,用于定义 所有 HTTP 相关的行为,包括:MIME 类型、日志格式、缓存、上游服务器、虚拟主机等。
http {### Basic Settings##sendfile on; # 启用“零拷贝”文件传输机制,大幅提升静态文件性能 只对 static 文件有效(如 index.html),对反向代理动态内容无效。# tcp_nopush on; # 与 sendfile 配合使用,优化 TCP 传输包的发送策略 这个配置配合sendfile用来优化静态配置types_hash_max_size 2048; # 增大 MIME 类型哈希表大小,避免 MIME 类型冲突或警告# tcp_nodelay on; # 启用 TCP 的 Nagle 算法禁用机制,让小数据包立即发送,而不是等待拼包提升实时性 这个配置和tcp_nopush互斥 专用来优化反向代理keepalive_timeout 65; # 控制 Nginx 与客户端之间 TCP 连接在空闲多久后关闭 单位秒 建议 普通网站 65 高并发 API 30 ~ 60(节省连接资源)长连接应用(如 SSE) 300 ~ 75 可以配合keepalive_requests 使用 100; # 单个连接最多处理 100 个请求# keepalive_requests 100; # 单个连接最多处理 100 个请求server_tokens off; # 隐藏 Nginx 版本号,增强安全性server_names_hash_bucket_size 64; # 控制 Nginx 内部用于存储 server_name 的哈希表的桶(bucket)大小 解决域名过多或过长的警告 必须是 2 的幂次方且不能超过 server_names_hash_max_sizeserver_name_in_redirect off; # 控制重定向时是否使用真实的 server_name 决定在 rewrite ... redirect 或 return 301/302 时,使用哪个域名生成跳转 URL 建议关闭include /etc/nginx/mime.types; # 引入 MIME 类型映射表,让 Nginx 知道不同文件扩展名对应哪种 Content-Type 必须包含,除非你手动定义所有类型default_type application/octet-stream; # 设置当无法识别文件类型时,默认使用的 MIME 类型 推荐值,安全且合理### SSL Settings### ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE# ssl_prefer_server_ciphers on;### Logging Settings### 设置日志模式log_format main '$remote_addr - $remote_user [$time_local] ''"$request" $status $body_bytes_sent ''"$http_referer" "$http_user_agent" ''upstream_response_time $upstream_response_time ''request_time $request_time';access_log /var/log/nginx/access.log main;### Gzip Settings##gzip on; # 开启 Gzip 压缩功能gzip_vary on; # 在响应头中添加 Vary: Accept-Encoding 告诉 CDN、反向代理、浏览器:压缩版和非压缩版是不同的资源gzip_proxied no-cache no-store private expired auth; # 控制代理请求 在包含哪些响应头的时候开启压缩gzip_comp_level 6; # 设置压缩级别(1~9)gzip_buffers 32 4k; # 设置压缩缓冲区大小 表示使用 16 个 8KB 的缓冲区gzip_http_version 1.1; # 指定启用压缩的最小 HTTP 版本gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; # 指定哪些 MIME 类型的响应内容需要压缩### Virtual Host Configs### server段配置信息server {listen 80; # 配置监听的端口server_name localhost; # 定义虚拟主机的域名或通配符,用于匹配客户端请求的 Host 头 支持精确匹配、通配符匹配、正则表达式匹配、泛域名匹配# location段配置信息 定义如何处理匹配特定 URI 的请求,是 server 块中最核心的子块。=:精确匹配(优先级最高)、~:区分大小写的正则匹配、~*:不区分大小写的正则匹配、^~:前缀匹配(匹配成功后不再检查正则)、无修饰符:前缀匹配location / {root /usr/share/nginx/html; # 网站根目录index index.html index.htm; # 默认首页文件deny 192.168.22.11; # 禁止访问的ip地址,可以为allallow 192.168.33.44; # 允许访问的ip地址,可以为all}error_page 500 502 503 504 /50x.html; # 默认50x对应的访问页面error_page 400 404 error.html; # 同上}include /etc/nginx/conf.d/*.conf;include /etc/nginx/sites-enabled/*;
}#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
server
块中的关键指令详解
-
listen
:定义服务器监听的 IP 地址和端口。-
语法:
listen address:port [parameters];
-
常用参数:
default_server
:将此 server 设为默认服务器(当没有匹配的server_name
时使用)。ssl
:启用 SSL/TLS。http2
:启用 HTTP/2。reuseport
:允许多个 worker 进程监听同一端口(提升性能)。
-
示例
listen 80; listen 443 ssl http2; listen 192.168.1.1:80 default_server;
-
-
server_name
:定义虚拟主机的域名或通配符,用于匹配客户端请求的Host
头。
-
语法:
server_name name1 name2 ...;
-
支持类型
- 精确匹配:
example.com
- 通配符:
*.example.com
或www.example.*
- 正则表达式:
~^www\d+\.example\.com$
- 泛域名匹配:
_
(匹配任何 Host)
- 精确匹配:
-
示例
server_name example.com www.example.com; server_name *.example.com; server_name ~^(www|api)\.example\.com$;
-
location
:定义如何处理匹配特定 URI 的请求,是server
块中最核心的子块。-
语法:
location [modifier] pattern { ... }
-
修饰符:
=
:精确匹配(优先级最高)~
:区分大小写的正则匹配~*
:不区分大小写的正则匹配^~
:前缀匹配(匹配成功后不再检查正则)- 无修饰符:前缀匹配
-
示例:
location / {root /var/www/html; }location = /favicon.ico {log_not_found off;access_log off; }location ~ \.php$ {fastcgi_pass 127.0.0.1:9000;include fastcgi_params; }
-
-
root 与 alias
:定义文件系统路径映射。-
root
:指定根目录,请求 URI 会附加到该路径后。location /static/ {root /var/www/html; # /static/file.css → /var/www/html/static/file.css }
-
alias
:直接替换 location 匹配部分location /static/ {alias /var/www/assets/; # /static/file.css → /var/www/assets/file.css }
-
index
:网站默认首页 -
error_page
:定义错误处理error_page 404 /404.html; error_page 500 502 503 504 /50x.html;
-
三、使用Nginx实现静态资源下载
http {# 1. 限制每个 IP 的并发连接数limit_conn_zone $binary_remote_addr zone=perip:64k;# 2. 限制请求频率(可选)# limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
}server {listen 80;server_name download.example.com;# 静态文件根目录root /var/www/downloads;# autoindex off; # 关闭目录浏览(安全)index index.html;# 允许上传大文件(如用于反向代理上传)client_max_body_size 10G;# 读取客户端请求体的超时client_body_timeout 60s;# === 强制下载:常见文件类型 ===location ~* \.(pdf|zip|tar|gz|tar\.gz|rar|7z|doc|docx|xls|xlsx|ppt|pptx|exe|msi|dmg|apk|mp4|avi|mkv|iso)$ {# 强制下载,不预览add_header Content-Disposition "attachment";# 缓存策略:可下载的文件一般不会频繁变,可缓存expires 7d;add_header Cache-Control "public, immutable";# 限速:每连接 1MB/s 注意:limit_rate 是每个连接的限速,多线程下载工具仍可突破。limit_rate 1m;# 限制每个 IP 最多 1 个并发连接limit_conn perip 1;# 限速前允许“突发”下载 5MB(提升体验)limit_rate_after 5m;# 启用 sendfile 提升大文件传输性能sendfile on;tcp_nopush on;# 启用断点续传(非常重要!)add_header Accept-Ranges bytes;# 禁用日志(可选,减少日志量)access_log off;# 防止中文乱码charset utf-8;}# === 可选:开启目录浏览(谨慎!)===location / {autoindex on; # 自动目录列表autoindex_exact_size off; # 控制目录列表中文件大小是否以精确字节数显示。autoindex_localtime on; # 控制目录列表中文件时间是否显示为本地时间。# 防止中文乱码charset utf-8;}# === 安全:禁止访问隐藏文件 ===location ~ /\.(?!well-known) {deny all;}# === 错误页面 ===error_page 404 /404.html;location = /404.html {internal;}
}
四、使用Nginx实现反向代理 、跨域、负载均衡,限流、黑白名单
# 设置日志模式
log_format main '$remote_addr - $remote_user [$time_local] ''"$request" $status $body_bytes_sent ''"$http_referer" "$http_user_agent" ''upstream_response_time $upstream_response_time ''request_time $request_time''cache_status:$upstream_cache_status ''upstream_addr:$upstream_addr';# 在 http 块中定义缓存区
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=10ginactive=60muse_temp_path=off;# nginx.conf 或在 conf.d/ 下的配置文件
upstream backend_servers {# 负载均衡:3 台后端服务器,轮询(默认)server 192.168.10.10:8080 weight=1; # weight配置每台服务器的权重 parameters 可选值:weight=number 权重值,默认为1 max_conns=number 上游服务器的最大并发连接数;fail_timeout=time 服务器不可用的判定时间;max_fails=numer 服务器不可用的检查次数;backup 备份服务器,仅当其他服务器都不可用时才会启用;down 标记服务器长期不可用,离线维护;server 192.168.10.11:8080 weight=1;server 192.168.10.12:8080 weight=1;least_conn; # least_conn 将请求分配给当前连接数最少的服务器 默认 round_robin 还可以配置 ip_hash、hash $request_uri consistent:基于 URL 一致性哈希# 可选:健康检查(需 Nginx Plus 或 OpenResty),开源版可用第三方模块# 在 Nginx 与 后端服务器(如应用服务器)之间,为每个 worker 进程保持最多 32 个空闲的长连接(keepalive 连接),用于后续请求复用,避免频繁建立/断开 TCP 连接。注意需要在location中配置proxy_http_version 1.1;proxy_set_header Connection "";这两行配置 否则keepalive不生效keepalive 32; keepalive_requests 100; # 每个 keep-alive 连接最多处理 50 个请求后关闭。防止单个连接长时间占用 避免内存泄漏、连接老化等问题keepalive_timeout 30s; # 每个空闲 keep-alive 连接保持打开状态最长 30 秒。
}# 限流:定义共享内存区
# 1. 限制每个 IP 的请求频率(防刷)
limit_req_zone $binary_remote_addr zone=api_limit:10m rate=20r/s;
# 2. 限制每个 IP 的并发连接数
limit_conn_zone $binary_remote_addr zone=conn_limit:1m;server {listen 80;server_name api.example.com;# 日志格式(可选增强)access_log /var/log/nginx/api.access.log main;error_log /var/log/nginx/api.error.log warn;# === 黑白名单控制 ===# 方法 1:白名单优先(只允许特定 IP)# allow 192.168.10.0/24;# allow 10.0.0.1;# deny all;# 方法 2:黑名单(禁止某些 IP)include /etc/nginx/blacklist.conf; # 自定义黑名单文件# === 限流规则 ===# 限制每个 IP 最多 10 个并发连接limit_conn conn_limit 10;# 限制每个 IP 每秒最多 20 个请求,突发允许 40 个 # nodelay表示突发请求不延迟处理limit_req zone=api_limit burst=40 nodelay;# 保留客户端真实信息 这四个必须要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; # 构建一个 请求经过的代理链列表,记录整个路径上的 IP。 X-Forwarded-For: client_ip, proxy1_ip, proxy2_ip, ... proxy_set_header X-Forwarded-Proto $scheme; # 告诉后端“原始请求是 HTTP 还是 HTTPS”。# 超时设置 Nginx 与后端服务器通信时的三个超时时间proxy_connect_timeout 30s; # 建立TCP链接的超时时间proxy_send_timeout 30s; # Nginx 向后端服务器发送请求数据的超时时间。注意:不是整个请求发送的总时间,而是“连续写之间的间隔超时”proxy_read_timeout 30s; # Nginx 从后端服务器读取响应的超时时间。这个指令定义:两次读操作之间的等待时间不能超过 30 秒 如果后端处理请求的总时间可能很长,但只要它持续输出响应(比如流式输出),就不会触发 proxy_read_timeout# 强制协议版本(配合 keepalive)proxy_http_version 1.1;proxy_set_header Connection "";# 缓冲区控制proxy_buffering on; # 打开缓冲区proxy_buffer_size 128k; # 用于存储后端响应“头部”和“第一部分响应体”的缓冲区大小。proxy_buffers 4 256k; # 设置 用于存储响应体主体的缓冲区数量和大小。 当响应体超过 proxy_buffer_size 的容量时,剩余数据会存到这些缓冲区中# 错误时重试下一台proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;# === 反向代理 + 负载均衡 ===location /api {# 反向代理到 upstream 注意 http://backend_servers 和 http://backend_servers/有很大的区别 带不/会保留原始 URI,完整转发,带/替换匹配部分为 /,相当于路径重写proxy_pass http://backend_servers;}# === 特殊路径放行或加强限制 ===# 例如:登录接口限得更严# location = /login {# limit_req zone=api_limit burst=5 nodelay; # 更严格# proxy_pass http://backend_servers;# proxy_set_header Host $host;# # ... 其他 proxy 设置# }# 静态资源可缓存location ~* \.(jpg|jpeg|png|gif|css|js|ico|webp)$ {proxy_pass http://backend_servers;# 启用缓存proxy_cache my_cache;proxy_cache_valid 200 302 10m; # 成功响应缓存 10 分钟proxy_cache_valid 404 1m;proxy_cache_use_stale error timeout updating; # 缓存容错机制expires 10m; # 添加 Expires 头# add_header Cache-Control "public, immutable"; # public 表示该资源可以被 任何中间代理、CDN、浏览器缓存,immutable:这是一个性能优化提示,告诉浏览器:“这个资源一旦缓存,内容永远不会改变,你不用再发请求去检查它是否更新!”}# 错误页面error_page 403 /403.html;error_page 500 502 503 504 /50x.html;
}
五、解决跨域问题(CORS)
当前端部署在 https://web.example.com
,后端 API 在 https://api.example.com
时,需配置 CORS。
location /api/ {proxy_pass http://backend_servers/;# 启用 CORSadd_header 'Access-Control-Allow-Origin' 'https://web.example.com' always; # 指定哪个源(Origin) 被允许访问资源 always示无论响应状态码是多少(即使是 500、404),都添加这个头add_header 'Access-Control-Allow-Methods' '*' always; # 声明服务器支持的 HTTP 方法add_header 'Access-Control-Allow-Headers' '*' always; # 声明服务器允许客户端发送的自定义请求头add_header 'Access-Control-Expose-Headers' '*' always; # 声明哪些响应头可以被 JavaScript 读取# 预检请求直接返回 204if ($request_method = 'OPTIONS') {add_header 'Access-Control-Max-Age' 86400; # 告诉浏览器:这个预检结果可以缓存 86400 秒add_header 'Content-Type' 'text/plain; charset=utf-8'; # 设置响应体的 MIME 类型add_header 'Content-Length' 0; # 与 return 204 配合,确保响应正确return 204;}
}
⚠️ 注意:生产环境避免使用 *
通配符,应明确指定域名。
六、配置防盗链保护静态资源
防止其他网站盗用你的图片、视频等资源。
防盗链配置(基于 Referer)
location ~* \.(jpg|jpeg|png|gif|mp4|avi|css|js)$ {valid_referers none blocked *.example.com example.com; # 定义哪些 Referer(来源页)是合法的。 none 表示允许 Referer 为空的请求(直接通过URL请求) blocked 表示 允许 Referer 被防火墙或代理屏蔽/加密的情况if ($invalid_referer) {return 403;# 或重定向到默认图片# rewrite ^/ https://example.com/forbidden.png redirect;}expires 1y;add_header Cache-Control "public, immutable";
}
✅ 说明:
valid_referers
定义合法来源$invalid_referer
为 1 时表示非法引用- 结合缓存提升性能
七、SSL/TLS 证书配置(HTTPS)
使用 Let's Encrypt 免费证书实现 HTTPS。
1. 使用 Certbot 自动获取证书
sudo apt install certbot python3-certbot-nginx -y # 如果安装不了需要换源
sudo certbot --nginx -d test.api.com # 域名需要公网可访问 # certbot 生成的证书默认放在 /etc/letsencrypt/live/your-domain.com/ 下边 包含fullchain.pem证书链(Nginx 的 ssl_certificate) 和 privkey.pem 私钥(Nginx 的 ssl_certificate_key) ,注意certbot会自动修改Nginx配置但也可以手动修改检查
# systemctl list-timers | grep certbot # 检查自动续期是否可用
# sudo certbot renew --dry-run # 手动测试续期 # 证书到期后Nginx需要重新加载证书才可用 可以通过添加钩子的方法自动重载
2. 手动配置 SSL(示例)
# 生成自签名证书
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 \-keyout /etc/ssl/nginx/selfsigned.key \-out /etc/ssl/nginx/selfsigned.crt \-subj "/C=CN/ST=Beijing/L=Beijing/O=MyOrg/CN= api.example.com"
参数 | 含义 |
---|---|
-x509 |
生成自签名证书(不是证书请求) |
-nodes |
不加密私钥(Nginx 启动时不用输密码) |
-days 365 |
有效期 365 天 |
-newkey rsa:2048 |
生成 2048 位 RSA 密钥 |
-keyout |
私钥保存路径 |
-out |
证书保存路径 |
-subj |
证书信息(可自定义)CN=download.example.com 是你的域名 |
server {listen 443 ssl http2; # 启用https 和 http2协议server_name api.example.com;ssl_certificate /etc/ssl/nginx/selfsigned.crt; # ssl公钥位置ssl_certificate_key /etc/ssl/nginx/selfsigned.key; # ssl私钥位置ssl_protocols TLSv1.2 TLSv1.3; # 指定支持的 TLS 协议版本。ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512; # 指定加密套件(Cipher Suites),决定客户端和服务器如何加密通信。ssl_prefer_server_ciphers off; # 控制是否优先使用服务器指定的加密套件。off:客户端优先选择(更兼容)on:服务器优先(更安全)# 安全头add_header Strict-Transport-Security "max-age=63072000" always; # 强制浏览器在未来 63072000 秒(约 2 年)内只能通过 HTTPS 访问add_header X-Frame-Options DENY always; # 防止点击劫持(Clickjacking)DENY:不允许被 <iframe> 嵌入add_header X-Content-Type-Options nosniff always; # 防止 MIME 类型嗅探攻击add_header X-Permitted-Cross-Domain-Policies none always; # 防止旧式 Flash 跨域攻击add_header Referrer-Policy "no-referrer" always; # 浏览器在任何情况下都不会发送 Referer 头。location / {proxy_pass http://backend_servers;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;}
}