36.2 Nginx Web 服务器
最后更新于
# pkg install nginx# cd /usr/ports/www/nginx/
# make install clean$ pkg search -o nginx$ ls /usr/ports/www/ | grep nginx# service nginx enable
nginx enabled in /etc/rc.conf# service nginx start
Performing sanity check on nginx configuration:
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
Starting nginx.# sockstat -4 | grep nginx
www nginx 1154 6 tcp4 *:80 *:*
root nginx 1153 6 tcp4 *:80 *:*/usr/local/
├── etc/
│ └── nginx/
│ ├── nginx.conf # Nginx 主配置文件
│ └── mime.types # MIME 类型定义文件
└── www/
└── nginx/ # Nginx 站点根目录root /usr/local/www/nginx;user www; # 指定 Nginx 运行用户(默认使用编译时设置)
worker_processes auto; # 自动根据 CPU 核心数设置工作进程数(现代推荐)
# 错误日志路径(如需开启可取消注释)
#error_log /var/log/nginx/error.log;
# 主进程 PID 文件
#pid logs/nginx.pid;
events { # events 模块配置开始
worker_connections 10240; # 单个工作进程允许的最大连接数(提高并发能力)
} # events 模块结束
http { # http 模块配置开始
include mime.types; # 引入 MIME 类型定义文件
default_type application/octet-stream; # 默认 MIME 类型
sendfile on; # 启用 sendfile 提高文件传输效率
tcp_nopush on; # 优化数据包发送(配合 sendfile)
tcp_nodelay on; # 降低延迟(小数据包立即发送)
keepalive_timeout 65; # keepalive 超时时间(秒)
types_hash_max_size 2048; # MIME 类型哈希表大小优化
server_tokens off; # 隐藏 Nginx 版本号(安全)
# ========================
# HTTP 虚拟主机
# ========================
server {
listen 80; # 监听 80 端口
server_name localhost; # 虚拟主机名
root /usr/local/www/nginx; # 网站根目录
index index.php index.html; # 默认首页文件顺序(优先 PHP)
location / { # 根路径匹配
try_files $uri $uri/ /index.php?$query_string; # 替代 if + rewrite
}
location ~ \.php$ { # 匹配 PHP 请求(更精确)
include fastcgi_params; # 引入 FastCGI 参数
fastcgi_pass 127.0.0.1:9000; # FastCGI 服务地址(PHP-FPM)
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # 正确的 PHP 脚本路径(关键优化)
fastcgi_index index.php; # 默认 FastCGI 索引文件
}
location ~ /\. { # 禁止访问隐藏文件(如 .htaccess)
deny all;
}
error_page 500 502 503 504 /50x.html; # 定义 5xx 错误页面
location = /50x.html { # 精确匹配错误页面
root /usr/local/www/nginx-dist; # 错误页面目录
}
}
# ========================
# HTTPS 虚拟主机
# ========================
server {
listen 443 ssl; # 启用 HTTPS
http2 on; # 启用 HTTP/2(Nginx 1.25.1+ 推荐写法)
# 旧写法 listen 443 ssl http2 已弃用,参见 Nginx. Module ngx_http_v2_module[EB/OL]. [2026-04-16]. <https://nginx.org/en/docs/http/ngx_http_v2_module.html>
server_name localhost; # 虚拟主机名
root /usr/local/www/nginx-dist; # HTTPS 网站根目录
index index.php index.html; # 默认首页文件
ssl_certificate /usr/local/etc/nginx/fbxs.crt; # SSL 证书路径
ssl_certificate_key /usr/local/etc/nginx/fbxs.key; # SSL 私钥路径
ssl_protocols TLSv1.2 TLSv1.3; # 启用现代 TLS 协议
ssl_ciphers HIGH:!aNULL:!MD5; # 使用安全加密套件(避免过时算法)
ssl_session_timeout 1d; # SSL 会话缓存时间
ssl_session_cache shared:SSL:10m; # 共享 SSL 会话缓存
# 基础安全头(现代 Web 推荐)
add_header X-Frame-Options SAMEORIGIN; # 防止被嵌入 iframe(点击劫持)
add_header X-Content-Type-Options nosniff; # 禁止 MIME 嗅探
add_header X-XSS-Protection "1; mode=block"; # 浏览器 XSS 防护
# 可选:启用 HSTS(强制 HTTPS)
#add_header Strict-Transport-Security "max-age=31536000" always;
location / { # 根路径匹配
try_files $uri $uri/ /index.php?$query_string; # 统一入口(现代框架推荐)
}
location ~ \.php$ { # HTTPS 下 PHP 处理
include fastcgi_params; # 引入 FastCGI 参数
fastcgi_pass 127.0.0.1:9000; # FastCGI 服务地址
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # 正确脚本路径
fastcgi_index index.php; # 默认索引文件
}
location ~ /\. { # 禁止访问隐藏文件
deny all;
}
}
} # http 模块结束