【问题标题】:Any AWS EB Laravel route getting 404 Not Found nginx/1.16.1任何获得 404 Not Found nginx/1.16.1 的 AWS EB Laravel 路由
【发布时间】:2020-08-21 17:52:20
【问题描述】:

我刚刚在 AWS Elastic beanstalk 上部署了一个新的 laravel 7 应用程序。我注意到他们将 Apache 服务器更改为 Nginx 服务器。

https://docs.aws.amazon.com/elasticbeanstalk/latest/platforms/platform-history-php.html

这是我的 api 后端 URL:http://mappab-api-staging.mappab.com.br/

这是登录路径:http://mappab-api-staging.mappab.com.br/login - 404 状态。

你遇到过同样的问题吗?我该如何解决?

我放在 /etc/nginx/conf.d/elasticbeanstalk/ 的 php.conf 是:

root /var/www/html/public;

index index.php index.html index.htm;

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

location ~ /\.ht {
   deny all;
}

location ~ /.well-known {
   allow all;
}

location ~ \.(php|phar)(/.*)?$ {
    fastcgi_split_path_info ^(.+\.(?:php|phar))(/.*)$;

    fastcgi_intercept_errors on;
    fastcgi_index  index.php;

    fastcgi_param  QUERY_STRING       $query_string;
    fastcgi_param  REQUEST_METHOD     $request_method;
    fastcgi_param  CONTENT_TYPE       $content_type;
    fastcgi_param  CONTENT_LENGTH     $content_length;

    fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
    fastcgi_param  REQUEST_URI        $request_uri;
    fastcgi_param  DOCUMENT_URI       $document_uri;
    fastcgi_param  DOCUMENT_ROOT      $document_root;
    fastcgi_param  SERVER_PROTOCOL    $server_protocol;
    fastcgi_param  REQUEST_SCHEME     $scheme;
    fastcgi_param  HTTPS              $https if_not_empty;

    fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
    fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

    fastcgi_param  REMOTE_ADDR        $remote_addr;
    fastcgi_param  REMOTE_PORT        $remote_port;
    fastcgi_param  SERVER_ADDR        $server_addr;
    fastcgi_param  SERVER_PORT        $server_port;
    fastcgi_param  SERVER_NAME        $server_name;

    # PHP only, required if PHP was built with --enable-force-cgi-redirect
    fastcgi_param  REDIRECT_STATUS    200;

    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param  PATH_INFO $fastcgi_path_info;
    fastcgi_pass   php-fpm;
}

【问题讨论】:

  • 如果我猜的话,你还没有编辑 nginx 配置来使用 Laravel 并且仍然依赖 htaccess? htaccess 是一个 Apache 配置文件。 Nginx 根本不阅读或使用它。如果您搜索“nginx laravel setup”,您会发现很多如何设置默认配置的示例。如果您的 htaccess 中有任何自定义规则,您也需要将它们迁移到 nginx 配置文件中。

标签: php amazon-web-services nginx laravel-7


【解决方案1】:

所以我刚刚遇到了这个问题,不得不解决这个问题。默认的 php 实现不考虑 laravel 导致的文件夹差异(文档中的任何地方都没有提到这一点)

您需要在您的 ec2 服务器上跟踪您的 nginx 站点配置。对我来说是:

/etc/nginx/conf.d/elasticbeanstalk/php.conf

sudo nano php.conf

root 默认是 /var/www/html; 这是不对的,这里添加了laravel文件夹,需要添加到:

root /var/www/html/your-laravel-app-name/public;

为此,您需要在您的 ec2 服务器中,因此您可以随时导航到该文件夹​​并检查它。

我相信其他人将能够提供在弹性 beanstalk 环境中自动配置所需的内容。

我还在基础 laravel 配置的 nginx 配置中添加了以下几行:

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

location ~ /\.ht {
   deny all;
}

location ~ /.well-known {
   allow all;
}

【讨论】:

  • 谢谢你们!我会尝试应用这些技巧。非常感谢!
  • 不用担心,如果此答案有帮助,请将其标记为已选择。如果您有不同的配置,请为其他搜索相同内容的人提供您的配置。 :)
  • 嗨。我使用eb ssh 访问我的php.conf。它是否正确?有一个文件正是你告诉我的。 php.conf 并且它接缝是正确的。 root /var/www/html/public; index index.php index.html index.htm; 我已经添加了你关于位置的额外信息。但它不起作用。我应该访问我的 EC2 实例吗?不会是eb ssh 命令访问 EC2 实例吗?谢谢。
  • 在这种情况下应该是 ec2 实例,root /var/www/html/public 将不正确,因为它没有考虑到 laravel 文件夹 --- /html/laravel-folder-here /公共。
  • 好的,我会检查我的 EC2 实例。谢谢!我已经编辑了我的问题,添加了完整的 php.conf。
【解决方案2】:

我遇到了同样的问题,因为 2020 年 4 月 30 日 EB 自动配置为 nginx 而不是 apache,因此 htaccess 文件不再有效。因此,当我上传我的代码时,首页(index.php)正在工作,但 api 却没有。这是我为解决问题所做的 - 取自 AWS 开发人员组:https://forums.aws.amazon.com/thread.jspa?messageID=942914&#942914

  1. SSH 进入弹性 beanstalk 环境(您可以在 EC2 实例中生成一个密钥对,这将下载一个 pem(私钥文件),然后在弹性 beanstalk 环境的配置/安全中附加密钥对)。
  2. 在终端(在 Mac 上)中,导航到您的私钥的存储位置并使用:
ssh -i "your_private_key_name.pem" ec2-user@your_server

您可能会收到您的 PEM 太开放的错误消息,在这种情况下使用以下命令:

chmod 400 your_private_key_name.pem
  1. 使用以下命令导航到 php.conf 文件:
cd /etc/nginx/conf.d/elasticbeanstalk
  1. 用这个输入文件:
sudo nano php.conf
  1. 这将打开 php.conf 文件,如下所示
# This file is managed by Elastic Beanstalk
#
# pass the PHP scripts to FastCGI server
#
# See conf.d/php-fpm.conf for socket configuration
#
root /var/www/html/public;

index index.php index.html index.htm;

location ~ \.(php|phar)(/.*)?$ {
    fastcgi_split_path_info ^(.+\.(?:php|phar))(/.*)$;

    fastcgi_intercept_errors on;
    fastcgi_index  index.php;

    fastcgi_param  QUERY_STRING       $query_string;
    fastcgi_param  REQUEST_METHOD     $request_method;
    fastcgi_param  CONTENT_TYPE       $content_type;
    fastcgi_param  CONTENT_LENGTH     $content_length;

    fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
    fastcgi_param  REQUEST_URI        $request_uri;
    fastcgi_param  DOCUMENT_URI       $document_uri;
    fastcgi_param  DOCUMENT_ROOT      $document_root;
    fastcgi_param  SERVER_PROTOCOL    $server_protocol;
    fastcgi_param  REQUEST_SCHEME     $scheme;
    fastcgi_param  HTTPS              $https if_not_empty;

    fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
    fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

    fastcgi_param  REMOTE_ADDR        $remote_addr;
    fastcgi_param  REMOTE_PORT        $remote_port;
    fastcgi_param  SERVER_ADDR        $server_addr;
    fastcgi_param  SERVER_PORT        $server_port;
    fastcgi_param  SERVER_NAME        $server_name;

    # PHP only, required if PHP was built with --enable-force-cgi-redirect
    fastcgi_param  REDIRECT_STATUS    200;

    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param  PATH_INFO $fastcgi_path_info;
    fastcgi_pass   php-fpm;
}
  1. 在索引之后和位置之前插入以下内容
location / {
    try_files $uri $uri/ /index.php?$query_string;
    gzip_static on;
}
  1. 保存文档

  2. 运行这个命令重启nginx

sudo nginx -s reload
  1. 退出环境,你的 api 现在应该可以工作了

【讨论】:

  • 当 ssh 进入 EC2 实例时,您可能会收到错误消息“未受保护的私钥文件”,在这种情况下运行命令“sudo chmod 600 your_private_key_name.pem”并输入您的密码。再次尝试 ssh'ing,这应该可以工作。
  • 我们可以将这些命令添加到 .ebextensions 文件夹中,以便它在启动时自动运行吗?
  • 嘿,为什么每次重新启动 ec2 实例时我都必须不断更改我的 php.conf?是预期的吗?
  • 我已经尝试了 3 次这种方法,每次都会导致整个网站失败...
  • @SijanBhattarai 要保留更改,请在项目的根目录中创建以下目录结构: .platform/nginx/conf.d/elasticbeanstalk 并使用 conf.php 的内容添加 php.conf文件在里面。这对我有用;重新启动后更改仍然存在。
【解决方案3】:

如果您将 Amazon Linux 2 与 Elastic Beanstalk 与 NGINX 服务器一起使用,那么您需要关注 this documentation

基本上,您需要做的就是创建一个文件夹结构,映射您想要放置配置文件的 NGINX 配置位置,并将 .platform 作为最顶层文件夹 这应该在您的应用程序根目录中

即在 laravel 应用程序中创建一个名为 .platform 的文件夹,与应用程序文件夹处于同一级别。在这个 .platform 文件夹中创建以下内容

/nginx/conf.d/elasticbeanstalk

所以你有一个像 .platform/nginx/conf.d/elasticbeanstalk 这样的文件夹结构

在最后一个文件夹 elasticbeanstalk 中放置您的配置文件

例如 laravel.conf

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

将应用程序上传到 EB 后,它会将文件复制到服务器上的相同位置并重新启动 NGINX。


额外

在某些情况下,这可能还不够,当您尝试前往任何特定路线时,您会得到一个

419 |页已过期

要解决这个简单的 SSH 到您的实例的问题,请转到项目位置并清除缓存

php artisan cache:clear

【讨论】:

  • 这是最终修复和正确的方法,特别是因为在 ebs 上运行命令不是一个很好的做法。
  • 这应该被接受为正确答案——ElasticBeanstalk 配置应该通过 .ebextensions/.platforms 等来完成;而不是通过手动调整服务器
  • 虽然这解决了我的问题,但我每次更新 ec2 实例中的内容时都必须这样做。知道为什么会这样吗?
  • Sijan Bhattarai 如果您使用的是弹性 beanstalk,则不需要直接更改 ec2 实例,所有设置和配置都是使用 EBS 完成的,无论是使用控制台还是使用上述配置文件。直接更改 ec2 实例只是暂时的更改。
  • 花了很多时间后,终于,这对我有用。我在 laravel 8 项目中创建了文件“/.platform/nginx/conf.d/elasticbeanstalk/laravel.conf”,然后推送到 Github,Elastic Beanstalk 识别更改并完成。
【解决方案4】:

我遇到了同样的问题并通过意识到我误解了 AWS 文档来解决它。

https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/php-laravel-tutorial.html#php-laravel-tutorial-generate

上面写着“安装 Laravel 并生成网站”

这并不意味着要在 AWS 上执行此操作。您将在本地执行此操作。然后通过 Source Bundle 将其部署到 AWS,如下所述。

这样做之后,它起作用了。

【讨论】:

    【解决方案5】:

    我遇到了同样的情况,我准备了一个如下所示的配置文件来更新 Elastic Beanstalk 自动提供的默认 nginx 设置。

    1. 在 Laravel 项目中准备一个您自己的 nginx 设置的文件 (nginx.conf)。
    ~/workspace/your-laravel-app/
    |-- .platform
    |   -- nginx
    |      -- nginx.conf
    |-- other source files
    
    1. 编辑您自己的 nginx.conf,如下所示。
    user                    nginx;
    error_log               /var/log/nginx/error.log warn;
    pid                     /var/run/nginx.pid;
    worker_processes        auto;
    worker_rlimit_nofile    32153;
    
    events {
        worker_connections  1024;
    }
    
    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;
    
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
    
        include       conf.d/*.conf;
    
        map $http_upgrade $connection_upgrade {
            default     "upgrade";
        }
    
        server {
            listen        80 default_server;
            access_log    /var/log/nginx/access.log main;
    
            client_header_timeout 60;
            client_body_timeout   60;
            keepalive_timeout     60;
            gzip                  off;
            gzip_comp_level       4;
            gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    
            # Do not include the Elastic Beanstalk generated locations
            # include conf.d/elasticbeanstalk/*.conf;
    
            # Move Elastic Beanstalk healthd.conf content here
            if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
                set $year $1;
                set $month $2;
                set $day $3;
                set $hour $4;
            }
    
            access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;
    
            # Move Elastic Beanstalk php.conf content here
            root /var/www/html/public;
    
            index index.php index.html index.htm;
    
            # This is an additional configuration
            location / {
                try_files $uri $uri/ /index.php?$query_string;
                gzip_static on;
            }
    
            location ~ \.(php|phar)(/.*)?$ {
                fastcgi_split_path_info ^(.+\.(?:php|phar))(/.*)$;
    
                fastcgi_intercept_errors on;
                fastcgi_index  index.php;
    
                fastcgi_param  QUERY_STRING       $query_string;
                fastcgi_param  REQUEST_METHOD     $request_method;
                fastcgi_param  CONTENT_TYPE       $content_type;
                fastcgi_param  CONTENT_LENGTH     $content_length;
    
                fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
                fastcgi_param  REQUEST_URI        $request_uri;
                fastcgi_param  DOCUMENT_URI       $document_uri;
                fastcgi_param  DOCUMENT_ROOT      $document_root;
                fastcgi_param  SERVER_PROTOCOL    $server_protocol;
                fastcgi_param  REQUEST_SCHEME     $scheme;
                fastcgi_param  HTTPS              $https if_not_empty;
    
                fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
                fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
    
                fastcgi_param  REMOTE_ADDR        $remote_addr;
                fastcgi_param  REMOTE_PORT        $remote_port;
                fastcgi_param  SERVER_ADDR        $server_addr;
                fastcgi_param  SERVER_PORT        $server_port;
                fastcgi_param  SERVER_NAME        $server_name;
    
                # PHP only, required if PHP was built with --enable-force-cgi-redirect
                fastcgi_param  REDIRECT_STATUS    200;
    
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                fastcgi_param  PATH_INFO $fastcgi_path_info;
                fastcgi_pass   php-fpm;
            }
        }
    }
    
    1. 部署您的更改并加载您的新配置。
    $ eb deploy
    

    【讨论】:

    • 你是最棒的!!谢谢=)
    • 谢谢,它有效。但是您知道如何将非 www 重定向到 www 吗?
    【解决方案6】:

    我遇到了同样的问题,就我而言,它是通过从 laravel.conf 文件中删除 error_page 指令来解决的

    这是我的配置文件:

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";
    
    client_max_body_size 10M;
    
    index index.php;
    
    charset utf-8;
    
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
    
    #error_page 404 index.php;
    
    location ~ /\.(?!well-known).* {
        deny all;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-12
      • 2017-09-28
      • 1970-01-01
      • 1970-01-01
      • 2015-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多