ThinkPHP 5.1 部署后接口访问 404,通常是由于 Nginx 配置不正确导致的。ThinkPHP 5.1 使用了 URL 重写规则(如 index.php 作为入口文件),而 Nginx 默认不支持 .htaccess 文件,因此需要手动配置 Nginx 的 URL 重写规则。

以下是解决 ThinkPHP 5.1 部署后接口访问 404 的 Nginx 配置步骤:


1. 确认项目路径

确保你的项目部署在正确的路径下。例如,假设项目部署在 /var/www/your-project 目录下。


2. Nginx 配置文件

找到你的 Nginx 配置文件(通常位于 /etc/nginx/nginx.conf/etc/nginx/sites-available/default),并修改或添加以下配置:

server {
    listen 80;
    server_name your-domain.com;  # 替换为你的域名或 IP 地址
    root /var/www/your-project/public;  # 替换为你的项目 public 目录路径
    index index.php index.html index.htm;

    # 开启 URL 重写
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # 处理 PHP 文件
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;  # 替换为你的 PHP-FPM 版本
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # 禁止访问敏感文件
    location ~ /\.ht {
        deny all;
    }

    # 禁止访问日志文件
    location ~ ^/(runtime|application)/ {
        deny all;
    }

    # 其他静态文件处理
    location ~* \.(jpg|jpeg|png|gif|ico|css|js|pdf|woff|woff2|ttf|svg)$ {
        expires 30d;
        access_log off;
    }

    # 错误页面配置
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.

3. 关键配置说明
  • root: 指向项目的 public 目录,因为 ThinkPHP 5.1 的入口文件是 public/index.php
  • try_files: 用于 URL 重写,将所有请求重定向到 index.php
  • fastcgi_pass: 确保与你的 PHP-FPM 配置一致。可以通过以下命令找到 PHP-FPM 的 socket 文件路径:
ps aux | grep php-fpm
  • 1.
  • 或者查看 PHP-FPM 配置文件(如 /etc/php/7.4/fpm/pool.d/www.conf)。

4. 检查 PHP-FPM 配置

确保 PHP-FPM 已正确安装并运行。可以通过以下命令检查:

systemctl status php7.4-fpm  # 替换为你的 PHP-FPM 版本
  • 1.

如果未启动,可以使用以下命令启动:

sudo systemctl start php7.4-fpm
sudo systemctl enable php7.4-fpm
  • 1.
  • 2.

5. 重启 Nginx

修改完 Nginx 配置后,重启 Nginx 服务以使配置生效:

sudo systemctl restart nginx
  • 1.

6. 检查文件权限

确保项目文件和目录的权限正确。通常需要将 runtime 目录设置为可写:

sudo chown -R www-data:www-data /var/www/your-project
sudo chmod -R 755 /var/www/your-project
sudo chmod -R 777 /var/www/your-project/runtime
  • 1.
  • 2.
  • 3.

7. 测试访问

在浏览器中访问你的域名或 IP 地址,例如:

http://your-domain.com
  • 1.

如果配置正确,应该可以正常访问 ThinkPHP 5.1 项目。


8. 常见问题排查
  • 404 错误
  • 检查 root 是否指向 public 目录。
  • 检查 try_files 配置是否正确。
  • 检查 PHP-FPM 是否正常运行。
  • 502 错误
  • 检查 fastcgi_pass 配置是否正确。
  • 检查 PHP-FPM 是否正常运行。
  • 403 错误
  • 检查文件和目录权限是否正确。

9. 示例完整配置

以下是一个完整的 Nginx 配置示例:

server {
    listen 80;
    server_name your-domain.com;
    root /var/www/your-project/public;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }

    location ~ ^/(runtime|application)/ {
        deny all;
    }

    location ~* \.(jpg|jpeg|png|gif|ico|css|js|pdf|woff|woff2|ttf|svg)$ {
        expires 30d;
        access_log off;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.

总结

通过以上步骤配置 Nginx,应该可以解决 ThinkPHP 5.1 部署后接口访问 404 的问题。如果仍然有问题,可以提供更多错误日志或配置信息,我会进一步帮助你排查问题!

thinkphp5.1部署后接口访问404,nginx如何配置_php