Skip to main content

Nginx配置逻辑

配置文件

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;
}
}
}
  • charset utf-8;:设置响应头的 Content-Type 为 UTF-8 编码,确保中文等字符正确显示。重复定义无害,但只需一次。
  • client_max_body_size 128M;:允许上传最大 128MB 的请求体(默认 1MB),适合文件上传或大表单场景。重复同上。
  • listen 8087;:监听 TCP 端口 8087(非标准 HTTP 80),用于本地开发测试。访问方式:
  • server_name localhost;:匹配请求的 Host 头为 localhost,确保只处理本地请求(生产可改为域名)。
  • index index.php index.html;:默认首页文件优先级:先找 index.php,后 index.html。适合 PHP 项目的主入口。
  • access_log:记录所有访问请求(IP、URI、状态码等),路径为项目 runtime/logs/access.log。Nginx 会自动创建文件(前提目录存在)
  • error_log:记录错误日志(如 404、500),级别为 error(默认 warn,可选 debug/notice)。路径同上。
正则符号含义示例匹配说明
^从字符串开头开始匹配^/api/匹配 /api/ 开头的路径
$匹配字符串结尾/api$只匹配 /api,不匹配 /api/test
.匹配任意单个字符/a.b/匹配 /acb/, /a0b/
*前一个字符重复0次或多次/ab*c/匹配 /ac/, /abc/, /abbc/
+前一个字符重复1次或多次/ab+c/匹配 /abc/, /abbc/,但不匹配 /ac/
?前一个字符出现0或1次/ab?c/匹配 /ac//abc/
``逻辑“或”^/(api l admin)/匹配 /api//admin/
()分组/pages/(yxzx l tszh)/可捕获或控制匹配范围
[]字符集合/file\.[a-z]+$/匹配 /file.txt, /file.js