Files
MES/nginx-vhost-mes2.conf
T
2026-08-08 18:28:49 +08:00

112 lines
5.0 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# ============================================================
# MES2 — Nginx 站点配置(宝塔 / Baota
# 域名:51chip.net
# 站点根目录:/www/wwwroot/51chip.net/mes2 (按服务器实际路径确认)
# 作用:完整等效原 Apache .htaccess 的路由与安全防护
# 用法:宝塔「网站 → 站点 → 设置 → 配置文件」整体替换本内容,
# 并将下面 enable-php-XX.conf 的 XX 改为所选 PHP 版本(需 ≥ 8.0)。
# ============================================================
server {
listen 80;
# 启用 HTTPS 后,宝塔会在本文件自动追加 443 ssl 段;此处保留 80 监听。
server_name 51chip.net www.51chip.net;
# ===== 站点根目录(含 index.php 的目录) =====
root /www/wwwroot/mes2;
index index.php index.html index.htm;
# 默认字符集(等效 Apache AddDefaultCharset UTF-8
charset utf-8;
# 上传大小上限(避免 Nginx 默认 1m 导致 413;需与 php.ini 的
# upload_max_filesize / post_max_size 保持一致)
client_max_body_size 100m;
# ===== 1. 安全响应头(等效 .htaccess 的 mod_headers =====
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
# 仅在已启用 HTTPS / 已强制跳转后,取消下一行注释以开启 HSTS
# add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
# ===== 2. 禁止访问隐藏文件(.htaccess / .env 等) =====
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
# ===== 3. 禁止直接访问敏感文件扩展名(等效 <FilesMatch> ... Deny =====
# 说明:会同时阻断 static/js/utf8-php/php/config.json 直链,这与 Apache 现状一致
# UEditor 通过 PHP 读取该文件,不受影响)。如需公开某 JSON,见迁移指南。
location ~* \.(env|sql|log|md|yml|yaml|json|lock)$ {
deny all;
}
# ===== 4. 禁止直接访问诊断/调试/安装/修复脚本(等效 <FilesMatch> ... Deny =====
# FilesMatch 匹配任意路径下的文件名,故此处不锚定根目录。
location ~* /(diag|debug_|verify_|php83_check|fix_db|fix_db_password|install|logintest|pwdtest|cachecheck|checkpath|check|hexcheck|dbcheck)\.php$ {
deny all;
}
# ===== 5. 禁止访问备份目录(等效 RewriteRule ^backups/ - [F,L] =====
location ~ /(backups|_backup_)/ {
deny all;
}
# ===== 6. 关闭目录列表(等效 Options -Indexes =====
autoindex off;
# ===== 7. 上传文档目录:禁止执行任何脚本(等效 documents/.htaccess =====
# ^~ 前缀优先级高于正则 location,确保该目录下 .php 不会落入第 9 段的 PHP 处理。
location ^~ /static/uploads/documents/ {
location ~ \.(php|phtml|php3|php4|php5|php7|phar|cgi|pl|py|jsp|asp|aspx|sh|exe|bat)$ {
return 403;
}
}
# ===== 8. 前端控制器重写(等效 RewriteRule ^(.*)$ index.php [QSA,L] =====
# 文件/目录不存在则转发 index.php,并保留查询串;框架按 REQUEST_URI 解析路由。
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
# ===== 9. PHP 处理(保留宝塔生成的 include =====
# 方式 A(推荐):保持面板生成的一行,把 XX 改为实际 PHP 版本(如 enable-php-83.conf
include enable-php-XX.conf;
# 方式 B(如需手写,取消下面注释并删除上面那行 include):
# location ~ \.php$ {
# fastcgi_pass unix:/tmp/php-cgi-XX.sock; # 与所选 PHP 版本对应
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# include fastcgi.conf;
# }
# ===== 10. 静态资源缓存(可选优化) =====
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff2?|ttf|eot)$ {
expires 7d;
add_header Cache-Control "public";
}
access_log /www/wwwlogs/51chip.net.log;
error_log /www/wwwlogs/51chip.net.error.log;
}
# ============================================================
# 子目录部署变体(若站点挂在 http://51chip.net/mes2/ 而非域名根)
# 将 root 改为上一级,rewrite 与 php location 加 /mes2 前缀:
# root /www/wwwroot/51chip.net;
# location /mes2/ {
# try_files $uri $uri/ /mes2/index.php$is_args$args;
# }
# location ~ /mes2/.*\.php$ {
# fastcgi_pass unix:/tmp/php-cgi-XX.sock;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# include fastcgi.conf;
# }
# 其余安全 location 同样加 /mes2 前缀即可。index.php 会自动计算 BASE_URL,无需改代码。
# ============================================================