在使用 Nginx 作为 ThinkPHP 的 Web 服务器时,可以通过配置 Nginx 实现 ThinkPHP 的伪静态功能。下面是一个示例配置:
server {
listen 80;
server_name your_domain.com;
root /path/to/your/project/public;
location / {
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
}
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # 根据实际 PHP 版本和配置进行修改
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
在上述示例中,将 your_domain.com
替换为你的域名,并将 /path/to/your/project/public
替换为你的项目根目录路径。
该配置中使用了 if (!-e $request_filename)
条件判断来实现对非存在文件的请求进行重写到 index.php
文件。然后通过 FastCGI 将请求转发给 PHP-FPM 进程来处理。
完成配置后,保存并重新加载 Nginx 配置即可使伪静态生效。请确保已经安装并正确配置了 PHP 和相关依赖,以及 Nginx 启动正常。
内容由零声教学AI助手提供,问题来源于学员提问