在使用nginx 的时候,默认是主要配置放在/etc/nginx/ 下面的nginx.conf 文件,然后在/etc/nginx/conf.d/ 文件夹里面再进行分域名配置,例如example.com.conf,然后在nginx。conf 里面引入各个分域名配置,在实践中,我喜欢把一般配置和安全配置再提取出来,放在inclueds文件夹里面,然后再根据需要引入。形成类似这样的目录结构:
/etc/nginx/
├── nginx.conf
├── conf.d/
│ ├── example.com.conf
│ ├── another-domain.com.conf
│ └── ...
├── includes/
│ ├── general.conf
│ └── security.conf
└── ...
如果你需要更加细分,可以把general和security替换为文件夹, 再细分为gzip,logg,security_headers,access_control等。 下面是我的nginx配置,示范域名www.example.com
主配置nginx.conf
#运行用户
user www-data;
#启动进程,auto 表示自动检测可用 CPU 核心数
worker_processes auto;
#PID文件,记录当前启动的nginx的进程ID
pid /var/run/nginx.pid;
# 全局错误日志配置,日志级别为 notice
error_log /var/log/nginx/error.log notice;
#工作模式及连接数上限
events {
#单个后台worker process进程的最大并发链接数
worker_connections 1024;
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
#设定mime类型,类型由mime.types文件定义
include /etc/nginx/mime.types;
# 默认 mime 类型,如果文件类型未知则使用该类型
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;
# 设定字符集
charset utf-8;
# 开启 sendfile 以提高文件传输效率
sendfile on;
# 减少网络包的发送数量
tcp_nopush on;
# 减少网络延迟
tcp_nodelay on;
# 隐藏 nginx 版本号
server_tokens off;
# 关闭未找到资源的日志记录
log_not_found off;
# 设置 types 哈希表的最大尺寸
types_hash_max_size 2048;
# 设置 types 哈希表的桶大小
types_hash_bucket_size 64;
# 客户端请求体的最大大小
client_max_body_size 16M;
# SSL 基础配置
# SSL 会话超时时间
ssl_session_timeout 1d;
# SSL 会话缓存大小
ssl_session_cache shared:SSL:10m;
# 禁用 SSL 会话票据
ssl_session_tickets off;
# OCSP Stapling 配置,用于提升 SSL 证书验证速度
ssl_stapling on;
ssl_stapling_verify on;
# DNS 解析器配置,用于 OCSP
resolver 1.1.1.1 1.0.0.1 8.8.8.8 8.8.4.4 208.67.222.222 208.67.220.220 valid=60s;
# DNS 解析超时时间
resolver_timeout 2s;
# 连接超时时间
keepalive_timeout 65;
# 启用 gzip 压缩 更多选项在一般配置项 里面配置
gzip on;
# 加载其他配置模块
include /etc/nginx/conf.d/*.conf;
}
分域名配置 位置/etc/nginx/conf.d/example.com.conf
# 主服务器配置
server {
# 监听端口 443 并启用 SSL 和 HTTP/2
listen 443 ssl http2;
listen [::]:443 ssl http2;
# 服务器名称
server_name example.com;
# 设置 base 变量为网站根目录
set $base /usr/share/nginx/html;
# 指定网站根目录
root $base/public;
# SSL 证书和密钥文件路径
# 下面这个配置是使用acme.sh ssl目录为自己新建;使用certbot 的默认目录为/etc/letsencrypt/live/
ssl_certificate /etc/nginx/ssl/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/privkey.pem;
# 引入安全配置文件
include includes/security.conf;
# 日志配置
access_log /var/log/nginx/access.log combined buffer=512k flush=1m;
error_log /var/log/nginx/error.log warn;
# 默认首页文件
index index.php;
# index.php 回退配置
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# 其他配置文件
include includes/general.conf;
# wordpress 优化配置
include includes/example.com.wordpress.conf;
# 处理 .php 文件 主要更换php 实际版本
location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
include includes/php_fastcgi.conf;
}
}
# HTTP 重定向配置
server {
listen 80;
listen [::]:80;
server_name .example.com;
# 301 重定向到 HTTPS
return 301 https://example.com$request_uri;
}
一般配置 general.conf
# 处理 /favicon.ico 请求
location = /favicon.ico {
# 关闭未找到 favicon.ico 文件的日志记录
log_not_found off;
}
# 处理 /robots.txt 请求,禁止爬虫错误日志
location = /robots.txt {
# 关闭未找到 robots.txt 文件的日志记录
log_not_found off;
}
# 处理静态资源文件(css, js, 图片, 音频, 视频等)
location ~* \.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
# 设置缓存过期时间为 7 天
expires 7d;
}
# 处理 SVG 和字体文件
location ~* \.(?:svgz?|ttf|ttc|otf|eot|woff2?)$ {
# 添加跨域访问控制头,允许任何来源访问
add_header Access-Control-Allow-Origin "*";
# 设置缓存过期时间为 7 天
expires 7d;
}
# 启用 gzip 压缩
gzip on;
# 根据客户端的 HTTP 请求头中的 Accept-Encoding 自动判断是否使用 gzip 压缩
gzip_vary on;
# 允许对任何请求进行压缩
gzip_proxied any;
# 设置 gzip 压缩级别,范围是 1-9,数字越大压缩比越高但 CPU 使用率也越高
gzip_comp_level 6;
# 指定需要进行 gzip 压缩的 MIME 类型
gzip_types text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;
安全配置 security
# 安全头部配置
# 防止跨站脚本攻击 (XSS)
add_header X-XSS-Protection "1; mode=block" always;
# 禁止浏览器 MIME 类型嗅探
add_header X-Content-Type-Options "nosniff" always;
# 设置引用策略,防止泄露敏感信息
add_header Referrer-Policy "no-referrer-when-downgrade" always;
# 内容安全策略,防止跨站脚本攻击和数据注入攻击
add_header Content-Security-Policy "default-src 'self' http: https: ws: wss: data: blob: 'unsafe-inline'; frame-ancestors 'self';" always;
# 禁用 FLoC (隐私沙箱)
add_header Permissions-Policy "interest-cohort=()" always;
# 强制客户端与服务器之间使用 HTTPS 协议传输数据,有效期一年,包括子域名
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
文件配置
# . files
# 匹配以点 (.) 开头的文件和目录,排除 .well-known 目录
location ~ /\.(?!well-known) {
# 拒绝访问这些文件和目录
deny all;
}
php 配置 (可选)php_fastcgi.conf
# 404
try_files $fastcgi_script_name =404;
# default fastcgi_params
include fastcgi_params;
# fastcgi settings
fastcgi_index index.php;
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
# fastcgi params
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/:/usr/lib/php/:/tmp/";
wordpress 配置 example.com.wordpress.conf
# WordPress: 允许 TinyMCE
location = /wp-includes/js/tinymce/wp-tinymce.php {
include includes/php_fastcgi.conf; # 包含 PHP FastCGI 配置文件
}
# WordPress: 拒绝访问 wp-content 和 wp-includes 目录中的 PHP 文件
location ~* ^/(?:wp-content|wp-includes)/.*\.php$ {
deny all; # 拒绝所有匹配的请求
}
# WordPress: 拒绝 wp-content/uploads 目录中潜在有害文件
location ~* ^/wp-content/uploads/.*\.(?:s?html?|php|js|swf)$ {
deny all; # 拒绝所有匹配的请求
}
# WordPress: SEO 插件
location ~* ^/wp-content/plugins/wordpress-seo(?:-premium)?/css/main-sitemap\.xsl$ {
# 此处为空,表示允许访问
}
# WordPress: 拒绝访问 wp-content/plugins 目录 (除了之前的规则)
location ~ ^/wp-content/plugins {
deny all; # 拒绝所有匹配的请求
}
# WordPress: 拒绝访问通用敏感文件
location ~* ^/(?:xmlrpc\.php|wp-links-opml\.php|wp-config\.php|wp-config-sample\.php|readme\.html|license\.txt)$ {
deny all; # 拒绝所有匹配的请求
}
关注我获取更多资讯

📢 公众号

💬 个人号