skip to Main Content

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

4

Answers


  1. 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.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
  3. What is the actual header response you getting from your request?

    I’d suggest you do some test and try to isolate the issue if this is a problem with nginx config, php config or your laravel environment and error handling instead.

    you can create a test.php file in your public folder

    i.e.

    <?php
    
    http_response_code(500);
    TEST;
    

    now if you open site/test.php are you getting 500 error or 502 error? and is the error displaying something like undefine TEST constant.

    or how about you edit public/index.php and just break the code like adding . somewhere, are you also getting 502 response in your laravel app?

    502 errors usually happens when you set nginx as a proxy or does not get a valid response, you can enable debug mode to see what happens with your request.

    also post your nginx.conf

    and maybe try adding fastcgi_intercept_errors off; on your php or main location block

    EDIT

    Another possible cause of this is the upstream too big and more than your nginx config buffer_size

    You can try increasing the buffer size,

    add inside http block in whatever-environment-you-have/nginx/nginx.conf

    proxy_buffer_size   128k;
    proxy_buffers   4 256k;
    proxy_busy_buffers_size   256k;
    

    then on your ~php block add the following

    fastcgi_buffer_size 128k;
    fastcgi_buffers 4 256k;
    fastcgi_busy_buffers_size 256k;
    

    if still doesnt work try increasing all to 4096k

    Login or Signup to reply.
  4. You need to simply route the error codes to your index.php file so Laravel can deal with them.

    This has been covered in multiple other questions on StackOverflow. Here’s one –

    Allow Laravel to respond to 403 instead of nginx

    Just use 500 (502) instead of 403.

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