Home > Back-end >  Avoid Nginx showing Bad Gateway for Laravel's 500 errors in local dev
Avoid Nginx showing Bad Gateway for Laravel's 500 errors in local dev

Time:09-09

When developing locally on this project, I'm having issues where when my PHP Laravel application throws a 500 error I see a 502 Bag Gateway instead of an error page rendered by PHP. I do have the following env vars set:

APP_ENV=local
APP_DEBUG=true
APP_LOG_LEVEL=debug

In prod, I see Laravel resolve the 500.blade.php error page as expected, but locally nothing is shown.

For example, a bad method call can trigger this:

022/09/04 22:19:45 [error] 867#867: *103 FastCGI sent in stderr: "PHP message: [2022-09-04 22:19:45] local.ERROR: Call to undefined method....

I haven't been able to identify any configuration setting that I can tweak within nginx that'll enable it to show errors rather than a Bad Gateway.

Any suggestions on what configuration might need to be changed here?

Nginx configuration:

server {
    listen   80; ## listen for ipv4; this line is default and implied
    #listen   [::]:80 default ipv6only=on; ## listen for ipv6
    server_name  app;

    access_log off;
    error_log /dev/stdout;

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

    charset utf-8;

    # this causes issues with Docker
    sendfile off;

    location = favicon.ico { log_not_found off; access_log off; }
    location = robots.txt  { access_log off; log_not_found off; }

    # look for local files on the container before sending the request to fpm
    location / {
        try_files $uri /index.php?$query_string;
    }

    # nothing local, let fpm handle it
    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(. \.php)(/. )$;
        fastcgi_pass            localhost:9000;
        fastcgi_index           index.php;
        include                 fastcgi_params;
        fastcgi_param           REQUEST_METHOD  $request_method;
        fastcgi_param           SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param           QUERY_STRING    $query_string;
        fastcgi_param           CONTENT_TYPE    $content_type;
        fastcgi_param           CONTENT_LENGTH  $content_length;
        # Httpoxy exploit (https://httpoxy.org/) fix
        fastcgi_param           HTTP_PROXY "";

        # allow larger POSTS for handling oauth tokens
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
   }

    # Deny .htaccess file access
    location ~ /\.ht {
        deny all;
    }
}

CodePudding user response:

I created my nginx virtual-host/code-block by this way and it's working for me, I'm using 8000 port but you can use 80 port. However, it preferable if we don't map port 80 with any of single project in local development because mostly we are working on multiple projects so you should need to enable different ports of every project.

server {
    listen 8000;
    root /var/www/html/<project-path>/public;

    index index.html index.htm index.php;

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

    # pass the PHP scripts to FastCGI server listening on /var/run/php/php7.4-fpm.sock
    location ~ \.php$ {
            try_files $uri /index.php =404;
            fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    }

}

I hope that will help you.

CodePudding user response:

I believe that the behavior is caused by the php configuration, not by nginx. Try setting

display_errors = on;

Unless otherwise instructed nginx passes the exact error code it receives.

There is one other alternative I can think of, perhaps the script is timing out on error for some reason causing the 502.

  • Related