C
C
cat_crash2014-09-23 10:51:30
Nginx
cat_crash, 2014-09-23 10:51:30

How to check if a file exists in NGINX and return it if it exists?

The architecture of the application is such that when a conditional request for a picture
/uploads/cache/100x100/bla-bla.jpg
executes a php script, and if the file does not exist, then it "resizes" it to the desired size and puts it in the appropriate folder
. Now the attendance has increased and I would like to use Nginx check if there is a file and if there is, then immediately give it without requests to php-fcgi
Google found me this example:

location ^~ /uploads/cache/{

    if (-f $request_filename) {
        break;
    }
    include fastcgi_params;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_NAME  /resize.php;
    fastcgi_param DOCUMENT_URI /resize.php;
    fastcgi_param SCRIPT_FILENAME  /home/site/www/resize.php;
    fastcgi_param HTTPS off;
    fastcgi_pass  php5;
 }

the essence is that we check whether there is a file, and if there is, we call a break. In practice, the condition works, but for some reason it still sends php-fcgi for processing.
Actually, the question is - how to write it correctly?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Puma Thailand, 2014-09-23
@opium

Nginx has long had a trifile for this,
something like this
location / {
gzip on;
index index.php; ## Allow a static html file to be shown first
try_files $uri $uri/ @handler; ## If missing pass the URI to Magento's front handler
}
location @handler { ## Magento uses a common front handler
rewrite / /index.php;
}
location ~ .php$ { ## Execute PHP scripts
if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss
expires off; ## Do not cache dynamic content
fastcgi_pass 127.0.0.1:9000;
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
fastcgi_connect_timeout 3000s;
fastcgi_read_timeout 3000s;
fastcgi_send_timeout 3000s;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param MAGE_RUN_CODE default; ## Store code is defined in administration > Configuration > Manage Stores
fastcgi_param MAGE_RUN_TYPE store;
include fastcgi_params; ## See /etc/nginx/fastcgi_params
}
Not the best example, but I think that's enough to understand

K
kompi, 2014-09-23
@kompi

Why wrap in if? Oil is obtained. One try construct is enough.

#проверка на файлы
try $uri handler;
#проверка на файлы и директории
try $uri $uri/ handler;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question