【问题标题】:AWS Elastic Beanstalk + Laravel, Nginx ConfigurationAWS Elastic Beanstalk + Laravel、Nginx 配置
【发布时间】:2020-08-27 06:15:40
【问题描述】:

最近 AWS 开始使用 Amazon Linux 2 分发 Elastic Beanstalk PHP 环境,它放弃了 apache 转而支持 Nginx,我一直在尝试正确配置我的 Laravel 项目以使其正常工作,我以前只需要添加一些 .htaccess配置就是这样,在 Nginx 上我似乎无法弄清楚如何使我的应用程序工作,我的第一个问题是反向代理端口,我通过将 PORT 环境变量设置为 80 来修复它,但是当我尝试访问时除了 / 之外的任何来自 URL 的路由,它都会给我一个 404 Not Found 错误。

所以我尝试将 .ebextension/Nginx/nginx.conf 添加到我的项目中,其中包含以下内容:

user                    nginx;
error_log               /var/log/nginx/error.log warn;
pid                     /var/run/nginx.pid;
worker_processes        auto;
worker_rlimit_nofile    33282;

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;
      root /var/app/current/public;

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

      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 ~ \.php$ {
        fastcgi_pass unix:/var/run/php-fpm/www.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
      }

      location ~ /\.(?!well-known).* {
         deny all;
      }

      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;

      # Include the Elastic Beanstalk generated locations
      include conf.d/elasticbeanstalk/01_static.conf;
      include conf.d/elasticbeanstalk/healthd.conf;
  }
}

但它不起作用,我试图检查配置是否应用于实例,但 /etc/Nginx/Nginx.conf 没有改变。

如何通过 .ebextensions 配置 Elastic Beanstalk PHP Amazon Linux 2 以使 Nginx 与无状态 Laravel 应用程序一起使用?

谢谢!

【问题讨论】:

    标签: php laravel amazon-web-services nginx amazon-elastic-beanstalk


    【解决方案1】:

    我手动将一个文件添加到 /etc/nginx/conf.d/elasticbeanstalk/ 并将其命名为 laravel.conf。 laravel.conf 文件包含:

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

    完整解决方案:

    https://forums.aws.amazon.com/thread.jspa?threadID=321787&tstart=0

    【讨论】:

    • 您发送的链接帮助很大,将文件添加到 .plataform/nginx/conf.d/elasticbeanstalk 确实将文件放置在正确的位置,现在我正在调整其余的配置,谢谢!
    【解决方案2】:

    正确的方法是在里面添加这个文件

    .platform/nginx/conf.d/elasticbeanstalk/laravel.conf
    

    这是一个文档链接:

    https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/platforms-linux-extend.html

    【讨论】:

    • 有趣的是,我读了几次那个页面,它从来没有说过要把它放到 /elasticbeanstalk/ 文件夹中。这解决了我的问题,谢谢@lex064。
    【解决方案3】:

    正确的做法是在你的 laravel 项目的根目录下创建如下文件夹结构

    .platform/nginx/conf.d/elasticbeanstalk/laravel.conf(或任何其他以.conf结尾的文件名)并输入以下代码:

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

    【讨论】:

      猜你喜欢
      • 2021-08-22
      • 2021-11-24
      • 2017-04-01
      • 2015-02-23
      • 2015-03-27
      • 2015-11-27
      • 2017-02-28
      • 2017-05-21
      • 2016-12-08
      相关资源
      最近更新 更多