我有一个简单的上传 php 脚本,它接受图像 jpg 文件并将它们保存到屏幕截图目录,然后返回链接给客户端。在部署前测试中,我遇到了 nginx 服务,该服务导致其他每个请求都出错,如屏幕截图所示。我的环境在 Docker 容器中运行,我已检查配置和文件权限,一切正常。
这是我的 Nginx.conf 文件,显示 FastCGI 配置...
user nginx;
worker_processes 8;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
multi_accept on;
use epoll;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$proxy_protocol_addr - $remote_user [$time_local] [$host] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"';
access_log /var/log/nginx/access.log main;
charset utf-8;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 15;
port_in_redirect off;
server_name_in_redirect off;
index index.php index.html;
autoindex off;
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 text/xml application/xml application/javascript application/atom+xml application/rss+xml application/json;
client_body_buffer_size 200m;
client_max_body_size 25m;
server_tokens off;
map $http_x_forwarded_proto $fe_https {
default off;
https on;
}
server {
listen 0.0.0.0:80;
server_name _;
root /www/;
location / {
location ~* \.php$ {
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/$fastcgi_script_name;
include fastcgi_params;
fastcgi_pass php:9000;
try_files $uri =404;
}
}
}
}
处理图像上传到服务器的 upload.php 脚本:
<?php
header("Content-Type: text/text");
$secret_key = "";
$domain_url = "http://images.sonoranrp.com/";
$sharexdir = "screenshots/";
$safe_types = ["jpg", "jpeg"];
#Comment this next line if you want Robots to index this site.
if ($_SERVER["REQUEST_URI"] == "/robot.txt") { die("User-agent: *\nDisallow: /"); }
#Don't edit past this point!
if(isset($_FILES['files']))
{
$target_file = $_FILES["files"]["name"][0];
$fileType = pathinfo($target_file, PATHINFO_EXTENSION);
$filename = GetFilename($fileType);
//not sure how save that is, but you can use this instead:
//$fileType = strtolower(end(explode(".", $target_file)));
if (in_array($fileType, $safe_types))
{
if (move_uploaded_file($_FILES["files"]["tmp_name"][0], $sharexdir.$filename.'.'.$fileType))
{
echo "{\"url\": \"$domain_url$sharexdir$filename.$fileType\"}";
}
else
{
echo 'File upload failed - CHMOD/Folder doesn\'t exist? ';
}
}
}
else
{
echo 'No post data recieved';
}
function GetFilename($fileType) {
srand(time());
$filename = null;
while (true) {
$filename = uniqid(rand(), true);
if (!file_exists("screenshots/".$filename)) break;
}
return $filename;
}
?>
答案1
嗯,我很笨……
发现在我的 docker-compose/docker 网络上,我在该特定内部网络上重复了容器名称。这导致 docker DNS 服务让两个容器争夺 php.network_default 和 nginx.network_default 的 DNS 名称...
这似乎是怪异行为背后的主要问题......