Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony and nginx - 502 Bad Gateway error when calling APIs

I use docker (php7.4 and nginx) for running my Symfony 5 application, I implemented Bearer authentication using firebase/php-jwt, and can get a token successfully, but when I try to call the APIs using the token in the header, I get 502 Bad Gateway (See the Error) from nginx.

I am sure that the problem comes from my nginx configs, because when I use Symfony Local Web Server, it works.

nginx configuration:

server {
    listen 80;
    server_name my.local;
    root /home/my/public;

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

    location ~ ^/index\.php(/|$) {
        fastcgi_pass my-app:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        internal;
    }

    location ~ \.php$ {
        return 404;
    }

    location ~ \.php$ {
        return 404;
    }

    error_log /var/log/nginx/my_error.log;
    access_log /var/log/nginx/my_access.log;
}
like image 961
Servin Avatar asked Nov 06 '25 17:11

Servin


1 Answers

I increased the buffer size and solved the problem:

server {
    server_name my.local;
    root /home/my/public;;

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

    location ~ ^/index\.php(/|$) {
        fastcgi_pass my-app:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        internal;
    }

    error_log /var/log/nginx/my_error.log;
    access_log /var/log/nginx/my_access.log;
}
like image 115
Servin Avatar answered Nov 09 '25 10:11

Servin