【问题标题】:PHP 7.4, NGINX, Laravel 8 - only shows index page, all routes show 404PHP 7.4、NGINX、Laravel 8 - 仅显示索引页面,所有路由显示 404
【发布时间】:2021-09-26 02:19:18
【问题描述】:

我已将 Laravel 8 应用程序放在 AWS t2.nano Linux AMI ec2 实例上。我想先说我已经在这里待了大约一天。我已经尝试了一些配置。

这是我尝试过的一些配置:

  1. 来自 Laravel 8 文档的默认 nginx 配置文件 https://laravel.com/docs/8.x/deployment#nginx

  2. 此处引用的另一个非常相似的 stackoverflow 问题 Laravel on nginx says 404 for all routes except index

归根结底,我无法让它正常工作。我的索引页面加载,但任何其他路线最终都在 404 页面。您可以在此处查看应用程序。 https://technology.dvemedia.com/

这里有一些技术规格和我的 conf 文件的当前状态。

  • Laravel - 8
  • PHP - 7.4
  • NGINX - 1.12.2
# HTTP
server {
    listen 80;
    listen [::]:80;
    server_name technology;
    return 301 https://$host$request_uri; # Redirect to www
}

server {

     listen 80;
    listen [::]:80;
    server_name technology.dvemedia.com;

    root /var/www/html/technology/public;
    index index.php index.html index.htm;

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

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #fixes timeouts
        fastcgi_read_timeout 600;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }

}

我遗漏了什么或做错了什么,因为我无法让它去拯救我的生命。

【问题讨论】:

    标签: php linux laravel nginx amazon-ec2


    【解决方案1】:

    试试这个:

    ## Nginx php-fpm Upstream
    upstream dvemedia {
        server unix:/var/run/php/php7.4-fpm.sock;
    }
    
    ## Web Server Config
    server
    {
        ## Server Info
        listen 80;
        listen [::]:80;
        server_name technology.dvemedia.com;
        root /var/www/html/technology/public;
        index index.html index.php;
        
        ## DocumentRoot setup
        location / {
            try_files $uri $uri/ @handler;
            expires 30d;
        }
        
        ## Disable .htaccess and other hidden files
        location  /. {
            return 404;
        }
     
        ## Rewrite all request to index
        location @handler {
            rewrite / /index.php;
        }
     
        ## Execute PHP scripts
        location ~ \.php$ {
            try_files $uri = 404;
            expires          off;
            fastcgi_pass     dvemedia;
            fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include          fastcgi_params;
        }
    }
    

    并将所有优化/调整(如fastcgi_buffers ...)放入fastcgi_params 文件中

    【讨论】:

    • 第 11 行有一个轻微的错字,应该是“听”,但不幸的是,现在这只是给我一个 502 错误。我的索引页和其他所有路由都抛出 502 错误。 @Latheesan
    【解决方案2】:

    我做了一个错误的假设,认为我的 php-fpm 套接字会保持不变。查看目录结构后,我的 7.4 的套接字最终在这里。

    fastcgi_pass unix:/var/run/php-fpm/www.sock;

    这实际上修复了它并且一切正常。当我为我的套接字编写路径时,我给出了错误的信息实际上是这样的。

    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;

    如果我提供了正确的信息,@Latheesan 的答案很可能会奏效,当然要减去拼写错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-05
      • 1970-01-01
      • 2016-04-16
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      • 1970-01-01
      • 2019-04-24
      相关资源
      最近更新 更多