Nginx отправляет запросы на отсутствующие изображения на сервер

Я работаю над сайтом, на котором есть копии одних и тех же изображений разных размеров, например.

/images/200×100/someimage.jpg

/images/400×200/someimage.jpg

и т.п.

Изображения обслуживаются непосредственно Nginx, и только запросы php передаются в fastcgi.

Если изображение не может быть найдено, я хотел бы передать запрос fastcgi, чтобы я мог посмотреть, сможем ли мы сгенерировать версию изображения с правильным размером, а затем вернуть ее.

Я просто не могу заставить его работать — если изображение отсутствует, я могу заставить его вызывать нужный мне скрипт (вместо того, чтобы просто возвращать 404), НО это просто возвращает источник скрипта php.

Это соответствующая часть моего файла conf:

location ~ (^|/)\. {
return 404;
}

location /imgs {
location ~ \.php$ {return 403;}
}

#Static Contents
location ~* ^.+.(jpg|jpeg|png|gif|bmp)$ {
try_files $uri  /$uri @backend;
add_header Pragma "public";
add_header Cache-Control "public";
expires     1y;
access_log  off;
log_not_found off;
}

# Static Contents
location ~* ^.+.(ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|eot|woff|svg|htc)$ {
add_header Pragma "public";
add_header Cache-Control "public";
expires     1y;
access_log  off;
log_not_found off;
}

location / {
# Check if a file exists, or route it to index.php.
try_files $uri $uri/ /index.php?$query_string;
}

location ~\.php$ {
try_files $uri =404;
fastcgi_pass unix:/dev/shm/apache-php.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}

location @backend {
#return 410;
rewrite  ^(.*)$ /image.php?url=$1;
fastcgi_pass unix:/dev/shm/apache-php.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}

Отсутствующее изображение затем перейдет в location @backend но я не могу передать $ uri скрипту.

Я прошел через все виды вариаций, что я делаю не так? Любые предложения с благодарностью приветствуются! Благодарю.

У меня это отлично работает в другой виртуальной машине (я скопировал бэкэнд-блок и блоки изображений в другой conf-файл) — единственное отличие состоит в том, что тот, который работает, использует Apache вместо fastcgi.

2

Решение

Проблема была просто опечатка.

Я пытался переписать на /image.php это должно было быть /images.php

Рабочая версия вышеуказанного файла конфигурации выглядит следующим образом:

location ~ (^|/)\. {
return 404;
}

location /imgs {
location ~ \.php$ {return 403;}
}

#Static Contents
location ~* ^.+.(jpg|jpeg|png|gif|bmp)$ {
try_files $uri  /$uri @backend;
add_header Pragma "public";
add_header Cache-Control "public";
expires     1y;
access_log  off;
log_not_found off;
}

# Static Contents
location ~* ^.+.(ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|eot|woff|svg|htc)$ {
add_header Pragma "public";
add_header Cache-Control "public";
expires     1y;
access_log  off;
log_not_found off;
}

location / {
# Check if a file exists, or route it to index.php.
try_files $uri $uri/ /index.php?$query_string;
}

location ~\.php$ {
try_files $uri =404;
fastcgi_pass unix:/dev/shm/apache-php.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}

location @backend {

rewrite  ^(.*)$ /images.php?url=$1;
}
3

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector