状态码404:ZF3和Nginx

3
当我尝试访问Laminas应用程序的基本URL以外的任何地方时,Nginx会在我的应用程序中给我404错误;从我的日志记录中看来,它似乎试图将路由作为文件/目录进行服务。
我使用 Laminas MVC框架的README 提供的信息来配置我的虚拟主机;看起来应该将路由传递到public目录的index.php,但出于某种原因它未能这样做。
这是我的配置(/etc/nginx/sites-available/domain.com):
server {
    listen 80 default_server;
    listen [::]:80 default_server;
 
    root /sites/domain.com;
 
    index index.php
 
    server_name domain.com;
 
    location / {
        index index.php
        try_files $uri $uri/ @php;
    }
 
    location @php {
        # Pass the PHP requests to FastCGI server (php-fpm) on 127.0.0.1:9000
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME /sites/domain.com/index.php;
        include fastcgi_params;
    }
    
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
    
        # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    }
}

拥有多个 fastcgi_pass 似乎是错误的。此外,Laminas 使用一个 public 文件夹,你的 fastcgi_param 中也包含了吗? - Chris Haas
@ChrisHaas /sites/domain.com/ 是指向项目公共目录的符号链接。 - Renze
1个回答

0

试试这个:

  • 一个位置块,用于尝试所有的php、uri和html文件(同时保留查询字符串参数)
  • 一个单独的php块,将请求传递给您的fpm sock
server {
    listen 80 default_server;
    listen [::]:80 default_server;
 
    root /sites/domain.com;
 
    index index.php
 
    server_name domain.com;


    location / {
            try_files $uri $uri/ /index.html /index.php$is_args$args;
    }
         

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接