AWS Elastic Beanstalk + Laravel, Nginx Configuration

谁说胖子不能爱 提交于 2021-01-27 07:22:33

问题


Recently AWS started distributing the Elastic Beanstalk PHP environment with Amazon Linux 2, which has dropped apache in favor of Nginx, I've been trying to correctly configure my Laravel project to work, I used to just have to add some .htaccess configuration and that was it, on Nginx I can't seem to figure out how to make my app to work, my first issue was the reverse proxy port, which I fixed by setting PORT environment variable to 80, but when I try to access any route from the URL aside from /, it gives me an 404 Not Found error.

so I tried to add a .ebextension/Nginx/nginx.conf to my project containing the following:

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;
  }
}

but it didn't work, I tried to check if the configs ware applied on the instance, but /etc/Nginx/Nginx.conf did not change.

How can I configure an Elastic Beanstalk PHP Amazon Linux 2 through .ebextensions to make Nginx work with a stateless Laravel application?

Thank you!


回答1:


I manually added a file to /etc/nginx/conf.d/elasticbeanstalk/ and named it laravel.conf. The laravel.conf file contained:

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

Full solution:

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




回答2:


The correct way would be to add this file inside

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

Here is a link to docs:

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



来源:https://stackoverflow.com/questions/61734222/aws-elastic-beanstalk-laravel-nginx-configuration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!