Настройки сервера:
server {
listen 80 default_server; listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404; }
location ~ \.php$ { include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
rewrite ^/product/([a-zA-Z0-9_-]+)$ /product.php last;
rewrite ^/ajax/upload/logoshop/ /scroll.php last;
location ~* \.(jpg|jpeg|png|gif|css|js|ico|json|xml|woff|woff2|webp|svg)$
{ expires 2147483647s; add_header Cache-Control "public,max-age=2147483647"; access_log off; }
location ~ /\.ht { deny all;
}
}
Файл отправляю через AJAX:
<script>
$(document).ready(function(){
$(document).on('change', '#file', function(){
var name = document.getElementById("file").files[0].name;
var form_data = new FormData();
var ext = name.split('.').pop().toLowerCase();
if(jQuery.inArray(ext, ['gif','png','jpg','jpeg']) == -1)
{
alert("Invalid Image File");
}
var oFReader = new FileReader();
oFReader.readAsDataURL(document.getElementById("file").files[0]);
var f = document.getElementById("file").files[0];
var fsize = f.size||f.fileSize;
if(fsize > 2000000)
{
alert("Image File Size is very big");
}
else
{
form_data.append("file", document.getElementById('file').files[0]);
$.ajax({
url:"http://localhost/ajax/upload/logoshop/",
method:"POST",
data: form_data,
contentType: false,
cache: false,
processData: false,
beforeSend:function(){
$('#userimage').html("<img style='max-width:70px!important;max-height:70px!important;min-width:70px!important;min-height:70px!important;' src=''>");
},
success:function(data)
{
$('#userimage').html(data);
}
});
}
});
});
</script>
На этот php скрипт:
<?php
$uniquecalue = uniqid();
if($_FILES["file"]["name"] != '')
{
$test = explode('.', $_FILES["file"]["name"]);
$ext = end($test);
$name = $uniquecalue . '.' . $ext;
$location = '/upload/' . $name;
move_uploaded_file($_FILES["file"]["tmp_name"], $location);
echo '<img src="'.$location.'"/>';
}
?>
Код работает на Apache, но не работает на NGINX. В чем может быть проблема?