events {
accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
#use epoll; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 1024; #最大连接数,默认为512
}
http {
include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream; #默认文件类型,默认为text/plain
#access_log off; #取消服务日志
log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
server {
charset utf-8;
client_max_body_size 128M;
# 监听端口号
listen 8087;
server_name localhost;
index index.php index.html;
access_log /mnt/d/Demo/test/kc_api/api/runtime/logs/access.log;
# 级别为debug|info|notice|warn|error|crit|alert|emerg
error_log /mnt/d/Demo/test/kc_api/api/runtime/logs/error.log error;
# / 匹配所有请求(根路径)。
location / {
# 1. $uri:是否存在静态文件(如 /style.css)
# 2. $uri/:是否存在目录(自动加 /)
# 3. 若无,重定向到 /index.php,并附加查询参数(如 ?id=1 变为 /index.php?id=1)。
try_files $uri $uri/ /index.php$is_args$args;
}
# 正则匹配以 /assets/ 开头、末尾 .php 的 URI(如 /assets/script.php)。
location ~ ^/assets/.*\.php$ {
# 拒绝所有访问,返回 403 Forbidden。
deny all;
}
# 后端 PHP
location ~ \.php(.*)$ {
root /mnt/d/Demo/test/kc_api/api/web;
# 转发到本地 PHP-FPM 套接字(需 PHP 7.4-FPM 服务运行)。
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
# 引入 Nginx 标准 FastCGI 参数(如 REQUEST_URI)
include fastcgi_params;
# 默认 PHP 入口为 index.php。
fastcgi_index index.php;
# 设置 PHP 执行脚本的完整路径(e.g., /path/to/web/index.php)。
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# 先检查 PHP 文件是否存在,无则 404(防止执行不存在的 PHP)。
try_files $uri =404;
}
# ^/pages/:以 /pages/ 开头。
# ([^/]+):捕获第一个非 / 段作为 $1(e.g., 项目名如 projectA)。
# (.*)?:可选的剩余路径作为 $2(e.g., /sub/path)。
# $:URI 结尾。
# 示例:/pages/projectA/sub/file.html → $1=projectA, $2=/sub/file.html。
location ~ ^/pages/([^/]+)(/.*)?$ {
# 将匹配路径映射到物理目录(e.g., /pages/projectA/sub/file.html → /mnt/d/demo/test/dist/projectA/sub/file.html)。
# alias 比 root 更灵活,用于子路径重定向。
alias /mnt/d/demo/test/dist/$1$2;
# 默认文件为 index.html(适合 SPA 前端)
index index.html;
}
# 禁止访问隐藏文件
# 不区分大小写正则,匹配以 . 开头的 URI(如 /.git、/config.php)。
location ~* /\. {
deny all;
}
}
}