skip to Main Content

I have put a Laravel 8 application on a AWS t2.nano Linux AMI ec2 instance. I would like to start up front by saying I have been at this for about a day now. I have tried a few configurations.

Here’s some configurations I have tried:

  1. The default nginx config file from the Laravel 8 documentation
    https://laravel.com/docs/8.x/deployment#nginx

  2. Another very similar stackoverflow question referenced here
    Laravel on nginx says 404 for all routes except index

At the end of the day, I cannot get it to work properly. My index page loads, but any of the other routes end up at a 404 page. You can view the application here.
https://technology.dvemedia.com/

So here are some tech specs and the current state of my conf file.

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

}

What am I missing or doing wrong, because I cannot get it to route to save my life.

2

Answers


  1. Chosen as BEST ANSWER

    I made a bad assumption by thinking my php-fpm socket would stay the same. After looking at the directory structure, my socket for 7.4 ended up being here.

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

    That actually fixed it and everything worked. I gave bad information when I wrote the path for my socket was actually this.

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

    @Latheesan answer would most likely have worked had I had given the correct information, minus the spelling mistake of course.


  2. Try this:

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

    and put all your optimisation/tweaks (like fastcgi_buffers …) in fastcgi_params file

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search