Nginx配置文件
Nginx 配置对比:精简版 vs phpStudy 版
一、配置全文
写法 A:精简版
server {
# ========== 1. server 块:一组独立的站点配置 ==========
# 告诉 Nginx"这是一个独立网站",下面的 listen/server_name/root 都归属于它
# 2. 监听端口:监听 HTTP 默认 80
listen 80;
# 3. 服务器名称:绑定域名或 IP
# 只有访问 server_name 匹配的地址才会进入这个 server 块
server_name 服务器IP;
# 4. 网站根目录(最关键):Nginx 从这里找文件
root D:/phpstudy_pro/WWW/项目名/public;
# 5. 默认入口文件:访问目录时按顺序尝试 index.php / index.html
index index.php index.html index.htm;
# 6. 核心路由规则
location / {
# 依次尝试:磁盘文件 → 磁盘目录 → 兜底入口
# 请求 /user/loginAjax 时先找磁盘上的同名文件,
# 找不到就把请求交给 /index.php 并透传 query string
try_files $uri $uri/ /index.php?$query_string;
}
# 7. PHP 解析规则:把 .php 请求转发给 PHP-FPM
location ~ \.php$ {
fastcgi_pass 127.0.0.1:FPM_PORT; # FPM 监听地址(按环境调整)
fastcgi_index index.php;
# 告诉 PHP-FPM 实际执行哪个脚本(根目录 + 请求脚本名)
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# 8. 禁止访问 .htaccess 等隐藏文件
location ~ /\.ht {
deny all;
}
}
写法 B:phpStudy 版
全站 404问题
server {
# ========== server 块:phpStudy 面板自动生成的站点配置 ==========
# 监听端口:HTTP 默认 80
listen 80;
# 服务器名称:绑定域名或 IP
server_name 服务器IP;
# 网站根目录:双引号是 phpStudy 生成时的风格,可带可不带
root "D:/phpstudy_pro/WWW/项目名/public";
# / 请求统一在这个 location 里处理
location / {
# 默认入口:注意多了 error/index.html(错误页入口)
index index.php index.html error/index.html;
# 自定义错误页:按状态码指到项目内 error/ 目录
error_page 400 /error/400.html;
error_page 403 /error/403.html;
error_page 404 /error/404.html;
error_page 500 /error/500.html;
error_page 501 /error/501.html;
error_page 502 /error/502.html;
error_page 503 /error/503.html;
error_page 504 /error/504.html;
error_page 505 /error/505.html;
error_page 506 /error/506.html;
error_page 507 /error/507.html;
error_page 509 /error/509.html;
error_page 510 /error/510.html;
# 伪静态规则不写在 nginx 里,而是 include 外部文件
# 该文件若丢失,伪静态静默失效,全站 404
include D:/phpstudy_pro/WWW/项目名/public/nginx.htaccess;
# 关闭目录列表(访问目录时不展示文件清单)
autoindex off;
}
# PHP 解析规则:写法 B 用 .php(.*)$ 且额外处理 PATH_INFO
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:FPM_PORT; # FPM 监听地址(按环境调整)
fastcgi_index index.php;
# 把 /index.php/admin/login 拆成「脚本 index.php」+「路径信息 /admin/login」
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
# 告诉 PHP-FPM 实际执行哪个脚本(根目录 + 请求脚本名)
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# 把拆出来的路径信息透传给 PHP($_SERVER['PATH_INFO'])
fastcgi_param PATH_INFO $fastcgi_path_info;
# PHP 可据此换算成实际磁盘路径
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
配套 nginx.htaccess:
# 伪静态规则(Nginx 语法,只是借 .htaccess 命名存放在项目里)
# 作用:请求的路径在磁盘上不存在时,重写给 index.php 处理
if (!-e $request_filename) { # -e:检查文件/目录是否真实存在
rewrite ^(.*)$ /index.php?s=$1 last; # 重写为 index.php?s=原路径,last 表示按新 URI 重新匹配
}
二、差异对比
| 差异项 | 写法 A | 写法 B |
|---|---|---|
| 路径回退 | try_files $uri $uri/ /index.php?$query_string; |
include nginx.htaccess → if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=$1 last; } |
| PATH_INFO | 不处理 | fastcgi_split_path_info + PATH_INFO/PATH_TRANSLATED 转发 |
| 错误页 | 默认 | 自定义 error 全套 |
| 目录列表 | 默认 off | autoindex off 显式写 |
| 隐藏文件 | location ~ /\.ht { deny all; } |
无显式拦截 |
三、差异说明
1. 路径回退
最终都把请求落到 index.php,但参数风格不一样:
- 写法 A:原 query string 透传
- 写法 B:URL 拆出
s=路径(ThinkPHP 3.x 风格)
写法 B 的 location / 没有 try_files 兜底,include 一旦丢就直接 404。
2. PATH_INFO
phpStudy 版多三行用于拆 /index.php/admin/login 这类 URL:
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
$_SERVER['PATH_INFO'] 拿 /admin/login。精简版不处理,框架自己从 REQUEST_URI 拆。
只在框架依赖 PATH_INFO 路由(如 ThinkPHP 3.x)时才需要写法 B 这三行;TP6 / Laravel 等现代框架不读 PATH_INFO,此差异不生效。
3. 错误页
phpStudy 版固定指向 error/4xx-5xx.html;精简版走 Nginx 默认或框架渲染。
4. 隐藏文件
phpStudy 版无显式 deny;精简版用 location ~ /\.ht { deny all; } 显式拦截 .htaccess、.htpasswd。
如果这篇文章对你有用,可以关注本人微信公众号获取更多ヽ(^ω^)ノ ~


浙公网安备 33010602011771号