I have a simple upload php script that accepts image jpg files and saves them to a screenshots directory then returns the link to the client. In pre-deployment testing, I am running into the nginx service erroring out every other request as shown in my screenshot. My environment is running in Docker containers and I have checked the config and file permissions and all is in order.
Here is my Nginx.conf file to show the FastCGI configuration...
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;
}
}
}
}
The upload.php script that handles image uploads to the server:
<?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;
}
?>