在云运行的单个容器中运行Nginx+PHP有问题。我的容器是基于阿尔卑斯和管理Nginx和PHP启动由主管。总的来说,它工作得很好,但从Nginx开始监听HTTP端口到PHP派生的时间间隔很短。这将导致出现502个HTTP错误,其中包含以下日志消息:
6#6: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 169.254.8.129, server: _, request: "POST / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000"这里的问题是,Cloud决定容器在打开8080端口时已准备好处理请求。在端口打开后,Cloud立即发送一个请求,该请求在第一次尝试时总是失败,因为FPM还没有准备好。日志消息NOTICE: fpm is running, pid 4在第一个请求到达并失败后出现。
当PHP准备就绪时,如何管理Nginx来打开它的端口?
主管配置:
[supervisord]
nodaemon=true
logfile=/dev/null
logfile_maxbytes=0
pidfile=/run/supervisord.pid
[program:php-fpm]
command=php-fpm -F
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
autorestart=false
startretries=0
[program:nginx]
command=nginx -g 'daemon off;'
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
autorestart=false
startretries=0Nginx配置:
# 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;
access_log /dev/stdout;
error_log /dev/stderr notice;
server {
listen 8080 default_server;
index index.php;
keepalive_requests 10;
keepalive_timeout 60 60;
root /var/www/html/app/public;
charset utf-8;
server_name _;
# Deny hidden files (.htaccess, .htpasswd, .DS_Store).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
########################
# mappings #
########################
location ~ \.(js|css|png|jpg|ico) {
expires 5d;
try_files $uri $uri/ =404;
return 404;
}
# 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 127.0.0.1:9000;
}
location / {
client_max_body_size 100m;
try_files $uri @fpm;
}
location @fpm {
# worker may take long time to finish (max 1 hour)
fastcgi_read_timeout 3600s;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param HTTP_PROXY "";
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/app/public/index.php;
fastcgi_param SCRIPT_NAME index.php;
}
}我启用了FPM ping/status页面。我能用它们触发Nginx端口打开吗?
Update 1:
我试着调整监督的优先级,然后开始几秒钟:
...
[program:php-fpm]
...
priority=100
startsecs=3
[program:nginx]
...
priority=200但没有成功:
[18-Dec-2020 00:31:04] NOTICE: ready to handle connections
[18-Dec-2020 00:31:04] NOTICE: fpm is running, pid 3
2020-12-18 00:30:30,689 INFO success: php-fpm entered RUNNING state, process has stayed up for > than 3 seconds (startsecs)
2020-12-18 00:30:28,388 INFO success: nginx entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
Error POST 502 549 B 3.286 s Google-Cloud-Scheduler https://***.run.app/
169.254.8.129 - - [18/Dec/2020:00:30:27 +0000] "POST / HTTP/1.1" 502 150 "-" "Google-Cloud-Scheduler"
169.254.8.129 - - [18/Dec/2020:00:30:27 +0000] "POST / HTTP/1.1" 502 150 "-" "Google-Cloud-Scheduler" "35.187.131.214"
2020/12/18 00:30:27 [error] 6#6: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 169.254.8.129, server: _, request: "POST / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "***.run.app"
2020-12-18 00:30:26,937 INFO spawned: 'nginx' with pid 4
2020-12-18 00:30:26,829 INFO spawned: 'php-fpm' with pid 3
2020-12-18 00:30:25,730 INFO supervisord started with pid 1
2020-12-18 00:30:25,704 CRIT Supervisor is running as root. Privileges were not dropped because no user这两个应用程序仍然由主管同时启动,nginx首先初始化。主管应用于应用程序的RUNNING状态对于云运行没有任何意义。
发布于 2020-12-18 03:31:51
现在,我得到了以下入口点脚本,该脚本确保在启动nginx之前运行PHP:
#!/usr/bin/env sh
set -e
# Start PHP-FPM as a daemon in the background
php-fpm -D
# Wait until PHP-FPM is up and accepts connections. Fail if not started in 10 secs.
for run in $(seq 20)
do
if [ "$run" -gt "1" ]; then
echo "Retrying..."
fi
RESPONSE=$(
SCRIPT_NAME=/fpm-ping \
SCRIPT_FILENAME=/fpm-ping \
REQUEST_METHOD=GET \
cgi-fcgi -bind -connect 127.0.0.1:9000 || true)
case $RESPONSE in
*"pong"*)
echo "FPM is running and ready. Starting nginx."
# Run nginx without exiting to keep the container running
nginx -g 'daemon off;'
exit 0
;;
esac
sleep .5
done
echo "FPM has failed to start on-time, exiting"
exit 1apk add fcgi命令是必需的(对于Alpine )。
我还假设php-fpm -D命令总是在FPM准备好之后退出,所以不需要循环,只需一个接一个地运行命令。但我还没试过呢。
发布于 2020-12-17 18:09:58
主管确实可以使用startdelay选项,但目前还没有。要延迟nginx的启动,请更改指定给Supervisor (相关螺纹)的命令:
delayed-nginx.sh:#!/bin/bash休眠5 exec -g 'daemon;我没有专门使用Supervisor,但是这个答案可能会为您工作。这将导致Supervisor只在php-fpm运行一次就启动nginx。
[program:php-fpm]
command=php-fpm -F
...
priority=100
[program:nginx]
command=nginx -g 'daemon off;'
...
priority=200您还可能需要将startsecs设置为更高的值(比默认的1秒),以便主管只在php-fpm运行更长时间(例如,5秒)之后才考虑启动它:
[program:php-fpm]
command=php-fpm -F
...
priority=100
startsecs=5请参阅主管文档中的更多内容。
https://serverfault.com/questions/1046515
复制相似问题