繁体   English   中英

Google Cloud Run 中的 Laravel 应用程序超时

[英]Timeout with Laravel App in Google Cloud Run

我有一个问题已经花了我几个小时,但我无法解决。 也许你们中的某个人知道问题可能是什么,或者我怎么能找到。

我正在尝试做的事情

我有一个 PHP Laravel 应用程序打包到 Docker 容器中,让它在 Google Cloud Run 中运行。 Docker 容器使用 NGINX 和 PHP-FPM。

什么不起作用

Docker 容器和应用程序在我的本地计算机上按预期运行。 但是在 Cloud Run 中,我得到了一个神秘的超时。 HTTP 请求在浏览器给我超时错误之前运行了大约 300 秒。 我在 Cloud Run 的应用日志中看不到任何错误。

这似乎是一个应用程序错误,而不是 Cloud Run 或 NGINX 的错误,因为如果我在我的应用程序中引发不同的错误,我将不会超时。 相反,我按预期得到了 HTTP 500。

我已经尝试过的

如前所述,当我在本地计算机上运行具有相同图像的 Docker 容器时,该应用程序运行良好。 因此,我不知道错误可能是什么。 遗憾的是,Cloud Run 中 PHP 的调试能力非常有限。 我唯一的想法是在执行的不同阶段在我的代码中添加 die() 语句,以查看超时发生的位置。 您可以认为这是非常耗时的,因为每次尝试我都需要重建和重新部署。 我尝试了几次,但到目前为止并没有给我任何新的见解。

有没有人知道如何更好地调试它,甚至知道错误可能是什么?

非常感谢!

我非常了解 PHP,但需要承认我对 Docker 和 NGINX 的了解有限。

这是我的 Dockerfile:

FROM trafex/php-nginx

# install necessary php extensions
USER root
RUN apk add php81-tokenizer

# switch back user to 'nobody'
USER nobody

# copy built app to server dir
COPY --chown=nobody . /var/www/html

# replace default nginx config with custom config
COPY container/prod/nginx.conf /etc/nginx/nginx.conf

nginx.conf

worker_processes auto;
error_log stderr warn;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include mime.types;
    default_type application/octet-stream;

    # Define custom log format to include reponse times
    log_format main_timed '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for" '
                          '$request_time $upstream_response_time $pipe $upstream_cache_status';

    access_log /dev/stdout main_timed;
    error_log /dev/stderr notice;

    keepalive_timeout 65;

    # Write temporary files to /tmp so they can be created as a non-privileged user
    client_body_temp_path /tmp/client_temp;
    proxy_temp_path /tmp/proxy_temp_path;
    fastcgi_temp_path /tmp/fastcgi_temp;
    uwsgi_temp_path /tmp/uwsgi_temp;
    scgi_temp_path /tmp/scgi_temp;

    # Default server definition
    server {
        listen [::]:8080 default_server;
        listen 8080 default_server;
        server_name _;

        sendfile off;
        tcp_nodelay on;
        absolute_redirect off;

        root /var/www/html/public;
        index index.php;

        location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to index.php
            try_files $uri $uri/ /index.php?q=$uri&$args;
        }

        # Redirect server error pages to the static page /50x.html
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root /var/lib/nginx/html;
        }

        # Pass the PHP scripts to PHP-FPM listening on php-fpm.sock
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/run/php-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param SCRIPT_NAME $fastcgi_script_name;
            fastcgi_index index.php;
            include fastcgi_params;
        }

        location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
            expires 5d;
        }

        # Deny access to . files, for security
        location ~ /\. {
            log_not_found off;
            deny all;
        }

        # Allow fpm ping and status from localhost
        location ~ ^/(fpm-status|fpm-ping)$ {
            access_log off;
            allow 127.0.0.1;
            deny all;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
            fastcgi_pass unix:/run/php-fpm.sock;
        }
    }
    
    gzip on;
    gzip_proxied any;
    gzip_types text/plain application/xml text/css text/js text/xml application/x-javascript text/javascript application/json application/xml+rss;
    gzip_vary on;
    gzip_disable "msie6";
    
    # Include other server configs
    include /etc/nginx/conf.d/*.conf;
}

上述请求的 Cloud Run 日志:

2022-08-13 04:58:56.021 MESZ2022-08-13 02:58:56,021 INFO supervisord started with pid 1
Standard
2022-08-13 04:58:57.046 MESZ2022-08-13 02:58:57,045 INFO spawned: 'nginx' with pid 2
Standard
2022-08-13 04:58:57.077 MESZ2022-08-13 02:58:57,077 INFO spawned: 'php-fpm' with pid 3
Standard
2022-08-13 04:58:57.199 MESZ169.254.1.1 - - [13/Aug/2022:02:58:57 +0000] "GET / HTTP/1.1" 502 497 "https://console.cloud.google.com/" "Mozilla/5.0 (X11; CrOS x86_64 14989.10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36" "103.44.34.133" 0.000 0.000 . -
Standard
2022-08-13 04:58:57.199 MESZ2022/08/13 02:58:57 [crit] 4#4: *5 connect() to unix:/run/php-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 169.254.1.1, server: _, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/run/php-fpm.sock:", host: "xxx.run.app", referrer: "https://console.cloud.google.com/"
Fehler
2022-08-13 04:58:57.210 MESZGET5021,19 KB2,6 sChrome 105 https://xxx.run.app/
Standard
2022-08-13 04:58:58.217 MESZ2022-08-13 02:58:58,200 INFO success: nginx entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
Standard
2022-08-13 04:58:58.217 MESZ2022-08-13 02:58:58,217 INFO success: php-fpm entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
Standard
2022-08-13 04:58:59.520 MESZ169.254.1.1 - - [13/Aug/2022:02:58:59 +0000] "GET / HTTP/1.1" 502 497 "https://console.cloud.google.com/" "Mozilla/5.0 (X11; CrOS x86_64 14989.10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36" "103.44.34.133" 0.000 0.000 . -
Standard
2022-08-13 04:58:59.520 MESZ2022/08/13 02:58:59 [crit] 4#4: *7 connect() to unix:/run/php-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 169.254.1.1, server: _, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/run/php-fpm.sock:", host: "xxx.run.app", referrer: "https://console.cloud.google.com/"
Fehler
2022-08-13 04:58:59.522 MESZGET5021,13 KB2 msChrome 105 https://xxx.run.app/
Standard
2022-08-13 04:59:00.461 MESZ2022/08/13 02:59:00 [crit] 4#4: *9 connect() to unix:/run/php-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 169.254.1.1, server: _, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/run/php-fpm.sock:", host: "xxx.run.app", referrer: "https://console.cloud.google.com/"
Fehler
2022-08-13 04:59:00.464 MESZGET5021,13 KB3 msChrome 105 https://xxx.run.app/
Standard
2022-08-13 04:59:01.248 MESZ169.254.1.1 - - [13/Aug/2022:02:59:01 +0000] "GET / HTTP/1.1" 502 497 "https://console.cloud.google.com/" "Mozilla/5.0 (X11; CrOS x86_64 14989.10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36" "103.44.34.133" 0.000 0.000 . -
Standard
2022-08-13 04:59:01.248 MESZ2022/08/13 02:59:01 [crit] 4#4: *11 connect() to unix:/run/php-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: 169.254.1.1, server: _, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/run/php-fpm.sock:", host: "xxx.run.app", referrer: "https://console.cloud.google.com/"
Fehler
2022-08-13 04:59:01.250 MESZGET5021,13 KB2 msChrome 105 https://xxx.run.app/
Standard
2022-08-13 04:59:01.331 MESZ[13-Aug-2022 02:59:01] NOTICE: fpm is running, pid 3
Standard
2022-08-13 04:59:01.331 MESZ[13-Aug-2022 02:59:01] NOTICE: ready to handle connections
Standard
2022-08-13 04:59:07.001 MESZ2022-08-13 02:59:07,001 INFO supervisord started with pid 1
Standard
2022-08-13 04:59:08.025 MESZ2022-08-13 02:59:08,025 INFO spawned: 'nginx' with pid 2
Standard
2022-08-13 04:59:08.057 MESZ2022-08-13 02:59:08,057 INFO spawned: 'php-fpm' with pid 3
Standard
2022-08-13 04:59:09.059 MESZ2022-08-13 02:59:09,059 INFO success: nginx entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
Standard
2022-08-13 04:59:09.059 MESZ2022-08-13 02:59:09,059 INFO success: php-fpm entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
Standard
2022-08-13 04:59:26.090 MESZ[13-Aug-2022 02:59:26] NOTICE: fpm is running, pid 3
2022-08-13 04:59:26.090 MESZ[13-Aug-2022 02:59:26] NOTICE: ready to handle connections
Standard
2022-08-13 05:04:02.071 MESZ169.254.1.1 - - [13/Aug/2022:03:04:02 +0000] "GET / HTTP/1.1" 500 3922 "https://console.cloud.google.com/" "Mozilla/5.0 (X11; CrOS x86_64 14989.10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36" "103.44.34.133" 300.000 300.000 . -
Warnung
2022-08-13 05:04:04.595 MESZTruncated response body. Usually implies that the request timed out or the application exited before the response was finished.
Fehler
2022-08-13 05:04:04.596 MESZGET500721 B301 sChrome 105 https://xxx.run.app/
Standard
2022-08-13 05:04:15.118 MESZ[13-Aug-2022 03:04:15] WARNING: [pool www] child 6 exited on signal 9 (SIGKILL) after 312.945712 seconds from start
Standard
2022-08-13 05:04:16.018 MESZ[13-Aug-2022 03:04:16] NOTICE: [pool www] child 7 started
Standard
2022-08-13 05:04:30.217 MESZ[13-Aug-2022 03:04:29] WARNING: [pool www] child 7 exited on signal 9 (SIGKILL) after 14.100816 seconds from start
Standard
2022-08-13 05:04:31.418 MESZ[13-Aug-2022 03:04:31] NOTICE: [pool www] child 8 started
Standard
2022-08-13 05:04:44.417 MESZ[13-Aug-2022 03:04:44] WARNING: [pool www] child 8 exited on signal 9 (SIGKILL) after 13.111450 seconds from start
Standard
2022-08-13 05:04:45.118 MESZ[13-Aug-2022 03:04:44] NOTICE: [pool www] child 9 started
Standard
2022-08-13 05:04:57.518 MESZ[13-Aug-2022 03:04:57] WARNING: [pool www] child 9 exited on signal 9 (SIGKILL) after 12.699204 seconds from start
Standard

[...]

2022-08-13 05:16:34.417 MESZ[13-Aug-2022 03:16:34] NOTICE: [pool www] child 60 started
Standard
2022-08-13 05:16:48.217 MESZ[13-Aug-2022 03:16:47] WARNING: [pool www] child 60 exited on signal 9 (SIGKILL) after 13.500399 seconds from start
Standard
2022-08-13 05:16:48.818 MESZ[13-Aug-2022 03:16:48] NOTICE: [pool www] child 61 started
Standard
2022-08-13 05:17:00.147 MESZ169.254.1.1 - - [13/Aug/2022:03:17:00 +0000] "GET / HTTP/1.1" 500 3922 "https://console.cloud.google.com/" "Mozilla/5.0 (X11; CrOS x86_64 14989.10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36" "103.44.34.133" 299.999 300.000 . -
Standard
2022-08-13 05:17:01.538 MESZ[13-Aug-2022 03:17:01] WARNING: [pool www] child 61 exited on signal 9 (SIGKILL) after 13.299661 seconds from start
Standard
2022-08-13 05:17:01.818 MESZ[13-Aug-2022 03:17:01] NOTICE: [pool www] child 62 started

2022-08-13 05:17:02.674 MESZ Truncated response body. Usually implies that the request timed out or the application exited before the response was finished.
Fehler
2022-08-13 05:17:02.675 MESZ GET 500 721 B 301 s Chrome 105 https://xxx.run.app/

经过数小时的尝试和谷歌搜索,我在这里得到了一个提示: https://www.anycodings.com/questions/laravel-application-hang-on-google-cloud-run-but-runs-fine-on-home-setup

我将容器部署为 Second Gen Cloud Run Service,它现在可以工作了!

老实说,我还不明白为什么会这样,但它似乎与 Laravel 和第一代 Cloud Run 环境有些不兼容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM