微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

API平台自定义操作中如何解决502Bad Gateway?

如何解决API平台自定义操作中如何解决502Bad Gateway?

我遇到了似乎是 Nginx 问题的问题。所以,我创建了 API 平台自定义操作,当我请求路由时,我看到以下输出

7#7: *9 upstream prematurely closed FastCGI stdout while reading response header from upstream,client: 172.31.0.1,server:,request: "GET /i18/device_help_tips HTTP/1.1",upstream: "fastcgi://172.31.0.3:9000",host: "localhost:8080"

我的 API 平台配置如下:

App\Api\Entity\DeviceHelpTip:
  properties:
  attributes:
    order:
      sorting: 'DESC'
  collectionoperations:
    get_i18:
      method: 'GET'
      path: '/i18/device_help_tips'
      controller: 'App\Api\Controller\DeviceHelpTip\PerformI18Request'
      security: "is_granted('ROLE_CONTENT_MANAGER') or is_granted('ROLE_MOBILE') or is_granted('ROLE_ADMIN')"

有趣的事实:如果我在安全部分删除其中一个角色,它将正常工作。我这里也有不记名身份验证。如果我使用令牌尝试请求数据 - 它会正常工作,如果没有令牌 - 上面提到的错误

控制器:

<?PHP

namespace App\Api\Controller\DeviceHelpTip;

use App\Api\Handler\DeviceHelpTipI18RequestHandler;
use Symfony\Component\HttpFoundation\Request;

class PerformI18Request
{ 
    private DeviceHelpTipI18RequestHandler $deviceHelpTipI18RequestHandler;

    public function __construct(DeviceHelpTipI18RequestHandler $deviceHelpTipI18RequestHandler)
    {
        $this->deviceHelpTipI18RequestHandler = $deviceHelpTipI18RequestHandler;
    }

    public function __invoke($data)
    {
        $this->deviceHelpTipI18RequestHandler->handle($data);
    }
}

Nginx 配置:

server {
root /srv/api/public;
client_max_body_size 10M;

location / {
    # try to serve file directly,fallback to index.PHP
    try_files $uri /index.PHP$is_args$args;
}

location ~ ^/index\.PHP(/|$) {
    # Comment the next line and uncomment the next to enable dynamic resolution (incompatible with Kubernetes)
    fastcgi_pass api-backend:9000;
    #resolver 127.0.0.11;
    #set $upstream_host api-backend;
    #fastcgi_pass $upstream_host:9000;

    # Bigger buffer size to handle cache invalidation headers expansion
    fastcgi_buffer_size 32k;
    fastcgi_buffers 8 16k;

    fastcgi_split_path_info ^(.+\.PHP)(/.*)$;
    include fastcgi_params;
    # When you are using symlinks to link the document root to the
    # current version of your application,you should pass the real
    # application path instead of the path to the symlink to PHP
    # FPM.
    # Otherwise,PHP's OPcache may not properly detect changes to
    # your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
    # for more information).
    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    fastcgi_param DOCUMENT_ROOT $realpath_root;
    # Prevents URIs that include the front controller. This will 404:
    # http://domain.tld/index.PHP/some-path
    # Remove the internal directive to allow URIs like this
    internal;
}

# return 404 for all other PHP files not matching the front controller
# this prevents access to other PHP files you don't want to be accessible.
location ~ \.PHP$ {
  return 404;
}
}

我尝试更改 fastcgi_buffer_sizefastcgi_buffers 但没有结果。如果您需要其他配置,请在评论中告诉我。提前致谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。