PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 16, 2022

[FIXED] Symfony and nginx - 502 Bad Gateway error when calling APIs

 January 16, 2022     authentication, nginx, php, symfony     No comments   

Issue

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

Solution

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


Answered By - Servin
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing