A
A
Alexander2019-10-26 13:22:48
Nginx
Alexander, 2019-10-26 13:22:48

How to redirect to a file on a subdomain via nginx?

Good day.
Help, please solve a simple (for those who know) problem, because I myself am almost not friends with nginx, but they didn’t take the time to figure it out.
The bottom line is, there is a site, files are uploaded in batches to a certain section, and the files are available at approximately the following address: site.ru/files/10-23/aaabbbccc/222.txt
Once a day at night, the files uploaded for this day are sent to the cloud . The cloud is tied to the site as a subdomain, and that file is already becoming available at images.site.ru/files/10-23/aaabbbccc/222.txt
The files folder on the site has .htaccess with a redirect rule for such a case.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://images.site.ru/files/$1

Here it would be desirable that nginx and not Apache would be engaged in the redirection. Please tell me how to fix this, where and what rule should be written in the nginx configs?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2019-10-26
@alexToster

location ^~ /files/ {
    if (!-e $request_filename){
        return 301 http://images.site.ru$uri;
    }
}

Or
location ^~ /files/ {
    try_files $uri =404;
    error_page 404 =301 http://images.site.ru$uri;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question