简体   繁体   中英

Nginx - redirect specific requests on root folder to subdomain

I currently have an hosting website that previously hosted images locally. Before the current system, image links were referenced from the root folder

EG:

https://example.com/image.jpg

We have since moved to AWS S3 storage and in order to direct legacy image requests to S3, we added the following to our nginx conf file.

location ~* ^/.*\.(jpg|gif|png|jpeg) {
        rewrite ^/(.*)$ "$scheme://i.example.com/$1" permanent;
}

However this captures all image files on the domain.

Is there a way to only redirect requests for image requests on the root domain and ignore other folders?

For example, users avatars are stored locally in

https://example.com/content/images/user/qwerty.jpg

These get redirected to the subdomain due to the regex.

I also attempted this with the S3 proxy_pass. However results were the same.

Thanks for your help

To select only image files in the top level directory, you should replace the .* element of your regex with something that does not match / . Such as:

location ~* ^/[^/]*\.(jpg|gif|png|jpeg)$ {
    return 301 $scheme://i.example.com/$request_uri;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM