简体   繁体   English

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

[英]Timeout with Laravel App in Google Cloud Run

I have an issue that already took me hours and I cannot solve it.我有一个问题已经花了我几个小时,但我无法解决。 Maybe somebody of you has a clue what the problem might be, or how I could find out.也许你们中的某个人知道问题可能是什么,或者我怎么能找到。

What I'm trying to do我正在尝试做的事情

I have a PHP Laravel App packed into a Docker Container to let it run in Google Cloud Run.我有一个 PHP Laravel 应用程序打包到 Docker 容器中,让它在 Google Cloud Run 中运行。 The Docker Container utilizes NGINX and PHP-FPM. Docker 容器使用 NGINX 和 PHP-FPM。

What doesn't work什么不起作用

The Docker Container and app run as expected on my local machine. Docker 容器和应用程序在我的本地计算机上按预期运行。 But in Cloud Run, I get a mysterious timeout.但是在 Cloud Run 中,我得到了一个神秘的超时。 The HTTP request runs for like 300 seconds before the browser gives me a Timeout Error. HTTP 请求在浏览器给我超时错误之前运行了大约 300 秒。 I cannot see any errors in the app logs in Cloud Run.我在 Cloud Run 的应用日志中看不到任何错误。

This seems to be an application error, not one by Cloud Run or NGINX because if I provoke a different error in my app I will not get a timeout.这似乎是一个应用程序错误,而不是 Cloud Run 或 NGINX 的错误,因为如果我在我的应用程序中引发不同的错误,我将不会超时。 Instead, I get an HTTP 500 as expected.相反,我按预期得到了 HTTP 500。

What I already tried我已经尝试过的

As mentioned the app works fine, when I run the Docker Container with the same image on my local machine.如前所述,当我在本地计算机上运行具有相同图像的 Docker 容器时,该应用程序运行良好。 Therefore I have no clue what the error might be.因此,我不知道错误可能是什么。 Sadly the debugging capabilities of PHP in Cloud Run are very limited.遗憾的是,Cloud Run 中 PHP 的调试能力非常有限。 The only idea I had is to sprinkle die() statements in my code at different stages of the execution to see where the timeout occurs.我唯一的想法是在执行的不同阶段在我的代码中添加 die() 语句,以查看超时发生的位置。 As you can think this is very time costly because every try I need to rebuild and redeploy.您可以认为这是非常耗时的,因为每次尝试我都需要重建和重新部署。 I gave this a couple of tries but it didn't give me any new insights so far.我尝试了几次,但到目前为止并没有给我任何新的见解。

Does anybody have a good idea how I could better debug this or even has an idea what the error could be?有没有人知道如何更好地调试它,甚至知道错误可能是什么?

Thank you very much!非常感谢!

I know PHP quite well but need to admit that I have limited knowledge about Docker and NGINX.我非常了解 PHP,但需要承认我对 Docker 和 NGINX 的了解有限。

Here is my Dockerfile:这是我的 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 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 Logs of the mentioned requests:上述请求的 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/

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

I deployed the container as Second Gen Cloud Run Service, and it works now!我将容器部署为 Second Gen Cloud Run Service,它现在可以工作了!

I honestly do not yet understand why this is the case, but it seems to be some incompatibility with Laravel and the First Gen Cloud Run environment.老实说,我还不明白为什么会这样,但它似乎与 Laravel 和第一代 Cloud Run 环境有些不兼容。

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

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