Laravel 应用程序在 Google 云 运行 上挂起,但在家庭设置上运行良好

Laravel application hang on Google Cloud Run but runs fine on home setup

我正在测试 Google 云 运行 作为 运行 新项目的平台。项目是使用 NodeJS 和 Laravel 开发的。基于 php-fpm 创建了一个 docker 图像。此图像 运行 在我的开发环境 运行ning Ubuntu 21.04 和 Docker 20.10.8 上很好。 当 运行 在 Google 云 运行 上部署相同的图像时,应用程序随机挂起。 我已将其缩小到导入的 symfony http 发送函数中的特定行,class 从 laravel index.php 调用。应用程序将挂起对 fast_cgi_finish_request() 的函数调用。它会在 70% 的情况下挂在那条线上,并在 nginx 超时 300 秒后调用超时。

./vendor/symfony/http-foundation/Response.php中的挂线:

    public function send() {
        $this->sendHeaders();
        $this->sendContent();

        if (\function_exists('fastcgi_finish_request')) {
            /* This line hangs 70% of the time. */
            fastcgi_finish_request();
        } elseif (!\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)) {
            static::closeOutputBuffers(0, true);
        }
        return $this;
    }

由于同一图像适用于其他环境,因此这是特定于 Google 云 运行 环境的。我目前的猜测是一些 运行 时间资源,因为应用程序可以在 haning 应用程序开始挂起之前响应 1-10 次。

关于如何在 Google 云 运行 平台中调试这个 运行ning 有什么想法吗?目前,我觉得我的调试选项相当有限。如果设置需要更多详细信息,请告诉我。

传递给 Laravel 的环境:

  APP_LOG: errorlog
  APP_URL: ...
  APP_ENV: testing
  APP_KEY: ....
  APP_DEBUG: 'true'
  LOG_CHANNEL: 'stderr'
  CACHE_DRIVER: 'database'
  SESSION_DRIVER: 'database'
  DB...

Docker文件:

FROM php:7.4-fpm as dev
# https://nginx.org/keys/nginx_signing.key
RUN apt-get update && apt-get install --no-install-recommends -y curl gnupg2 ca-certificates lsb-release && lsb_release && echo "deb http://nginx.org/packages/mainline/debian $(lsb_release -cs) nginx" > /etc/apt/sources.list.d/nginx.list && \
    curl -o /tmp/nginx_signing.key https://nginx.org/keys/nginx_signing.key && \
    mv /tmp/nginx_signing.key /etc/apt/trusted.gpg.d/nginx_signing.asc && \
    apt-get update && apt-get install --no-install-recommends -y libpq-dev && \
    docker-php-ext-install -j$(nproc) pdo_mysql pdo_pgsql && \
    apt-get update && apt-get install --no-install-recommends -y nginx

# ----------------------
# Composer install step
# ----------------------
FROM composer:1.10 as build

WORKDIR /app

COPY ./composer.* ./artisan ./
COPY ./database ./database
COPY ./nova-components ./nova-components
COPY ./nova ./nova

## autoload resources
COPY ./bootstrap ./bootstrap
COPY ./app ./app
COPY ./routes ./routes
COPY ./config ./config

RUN composer install \
        --no-dev \
        --no-progress \
        --no-suggest \
        --no-interaction \
        --optimize-autoloader \
        --prefer-dist && \
    composer dump-autoload

# ----------------------
# npm install step
# ----------------------
FROM node:14-alpine as node
WORKDIR /app

COPY ./*.json ./*.mix.js ./artisan /app/
COPY ./resources ./resources
COPY ./public ./public

RUN npm install && \
    npm run build

# ----------------------
# The FPM production container
# ----------------------
FROM dev

WORKDIR /app

COPY ./docker/www.conf /usr/local/etc/php-fpm.d/www.conf
COPY ./docker/nginx.conf /etc/nginx/nginx.conf
COPY ./docker/entrypoint.sh /
COPY ./docker/php.ini-development ./docker/php.ini-production $PHP_INI_DIR/

COPY ./ /app
COPY --from=build /app/vendor /app/vendor
COPY --from=node /app/public/js/ /app/public/js/
COPY --from=node /app/public/mix-manifest.json /app/public/mix-manifest.json

RUN chmod +x /entrypoint.sh && \
    rm -f /app/storage/logs/* /app/public/storage && \
    php /app/artisan storage:link && \
    mkdir /var/run/nginx && \
    chmod -R 777 /app/storage /app/app /app/public/app && \
    chown -R www-data:www-data /app/storage /app/app /app/public/app && \
    chown -R www-data:www-data /var/log/nginx /var/cache/nginx /var/run/nginx

USER www-data
ENTRYPOINT ["/entrypoint.sh"]

composer show:

64robots/nova-fields              0.18.0   A Laravel Nova field.
armincms/nova-tab                 4.0.2    A Laravel Nova tool.
bernhardh/nova-translation-editor 1.3.1    Laravel Nova translation editor
fideloper/proxy                   4.4.1    Set trusted proxies for Laravel
fruitcake/laravel-cors            v2.0.4   Adds CORS (Cross-Origin Resource Sharing) headers support in your Laravel application
gkermer/nova-text-auto-complete   0.0.5    A Laravel Nova text autocomplete field.
guzzlehttp/guzzle                 7.3.0    Guzzle is a PHP HTTP client library
intervention/image                2.6.0    Image handling and manipulation library with support for Laravel integration
laravel/framework                 v7.30.4  The Laravel Framework.
laravel/nova                      3.16.3   A wonderful administration interface for Laravel.
laravel/tinker                    v2.6.1   Powerful REPL for the Laravel framework.
listen/nova-flexible-content      dev-main Flexible Content & Repeater Fields for Laravel Nova.
listen/tree-view                  dev-main A Laravel Nova tool.
mcamara/laravel-localization      1.6.1    Easy localization for Laravel
ngiraud/nova-translatable-v2      1.0.4    A laravel-translatable extension for Laravel Nova. Inspired by optimistdigital/nova-translatable
optimistdigital/nova-settings     3.2.1    A Laravel Nova tool for editing custom settings using native Nova fields.
orangehill/iseed                  v3.0.1   Generate a new Laravel database seed file based on data from the existing database table.
silvanite/novatoolpermissions     v1.1.3   Laravel Nova Permissions (Roles and Permission based Access Control (ACL))
waynestate/nova-ckeditor4-field   0.7.0    This nova package allows you to use CKEditor 4 for text areas.
yassi/nova-nested-form            v3.0.12  A Laravel Nova package that allows you to create/update/delete nested related fields from a parent form.

nginx.conf:

pid /var/run/nginx/nginx.pid;
worker_processes auto;

events {
  worker_connections 1024;
}

http {
  include mime.types;
  #include fastcgi.conf;
  default_type application/octet-stream;
  sendfile on;
  tcp_nopush on;
  server_tokens off;
  client_max_body_size 10M;
  gzip on;
  gzip_disable "msie6";
  gzip_vary on;
  gzip_proxied any;
  gzip_comp_level 6;
  gzip_buffers 16 8k;
  gzip_http_version 1.1;
  gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

  server {
    listen [::]:8080;
    listen 8080 default_server;
    server_name _;
    root /app/public;
    index  index.php index.html index.htm;
    access_log /dev/stdout;
    error_log /dev/stdout info;
    disable_symlinks off;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;
    charset utf-8;

    include includes/*.conf;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

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

    error_page 404 /index.php;

    location ~ ^/status|^/ping {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    }
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        proxy_intercept_errors on;
        fastcgi_intercept_errors on;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
  }
}

更新

我在云中的设置和执行肯定有问题 运行。在 php-fpm 配置中启用 slowlog 并设置 request_slowlog_timeout。超时后它释放到 nginx 并正确响应。使用此设置我的应用程序确实很慢,但实际上可以运行。 fastcgi/php-fpm 设置中缺少某些内容。或者 Cloud 运行 不适合这个项目。

更新 2

在评论中讨论后,我将应用程序迁移到 GKE 标准,在那里 运行ning 一周以来没有错误。


即使按照@garbetjje 的建议删除机密后,问题仍然存在。我已经迁移到使用内置的 PHP 网络服务器。我已经 运行 在 Cloud 运行 中使用了大约 1.5 个月,现在没有任何问题。我意识到这不是一个长期的解决方案,目前正在寻找其他 运行 适合生产使用的时间。

更新 3

似乎与第一代 Cloud 运行 有关。现在在第二代云 运行 服务中部署了应用程序。所有功能都没有经过彻底测试,但应用程序现在似乎可以正常工作..

我最近遇到了同样的问题(具体来说,使用Laravel护照)。

我们还使用了挂载的秘密,并注意到我们的应用程序每隔 10-30 分钟就会挂起并在 60 秒后超时(我们在云中配置的超时 运行)。我的一位同事注意到,每次我们的应用程序开始挂起时,首先开始失败的事情之一就是读取已安装的秘密。

asked about this behaviour in the Google Cloud Community Slack workspace 和一位 Cloud 运行 PM 回复说他们正在回滚最近的实施更改。

我怀疑您遇到了与我们相同的问题(运行 我们在 GKE 中的应用程序也运行良好)。

如果可能的话,切换到将您的机密安装到您的环境中。这应该可以解决您的应用程序挂起的问题。

我有同样的问题。奇怪的是有时调用少量代码行(从 eloquent 模型获取数据然后发送响应),我得到“上游超时”。

现在 Cloud 运行 有了新的不同执行环境(测试版)https://cloud.google.com/run/docs/about-execution-environments,我正在从最初的第一代切换到第二代。然后我的应用程序运行正常。

我觉得初代有一些小问题:

也许这个 Github 问题是相关的:https://github.com/google/gvisor/issues/158