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

qni6mghb  于 2022-11-02  发布在  Nginx
关注(0)|答案(1)|浏览(260)

我有一个问题,已经花了我几个小时,我不能解决它。也许你们中有人知道问题可能是什么,或者我如何才能找到。
我想做的是
我有一个PHP Laravel应用程序打包到一个Docker容器,让它在谷歌云运行。Docker容器利用NGINX和PHP-FPM。

不起作用的地方

Docker Container和应用程序在我的本地机器上按预期运行。但是在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;
}

上述请求的云运行日志:

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/
kokeuurv

kokeuurv1#

经过几个小时的尝试和谷歌搜索,我得到了一个提示:Laravel application hang on Google Cloud Run but runs fine on home setup
我将容器部署为第二代云运行服务,它现在可以工作了!
老实说,我还不明白为什么会这样,但它似乎与Laravel和第一代Cloud Run环境有些不兼容。

相关问题