Reputation: 37904
I finally switched to amazon s3. but there are some items from my site which are posted in facebook back in times where I didnot integrate s3 yet and thats why they still have old media file urls in those posts.
And these images do not exist in webserver anymore. All of them are now in s3.
I am so ashamed to ask without showing any attemt, after posting this question, I will dive into google.
this is my media config part in nginx, all of you know it:
location /media {
alias /var/www/mysite/application/poll/media;
}
example problem case:
facebook bot visits the page with image url: www.mysite.com/media/lala.jp
. Now I want nginx to permanently redirect these (missing image) urls to image.mysite.com/media/lala.jpg
which is a s3 url for this image.
any ideas how I can do it?
Upvotes: 1
Views: 716
Reputation: 20569
This works for me:
root /home/myuser/myproject/public/;
location @media {
return 301 $scheme://image.example.com$request_uri;
}
location /media/ {
try_files $uri @media;
}
I'm holding collected static and media files in public directory inside django project, so MEDIA_ROOT
will be in this example /home/myuser/myproject/public/media/
, but I'm pretty sure that you can just use your approach with alias inside /media/
location.
Quick explanation: @media
is an named location, which by default is not used anywhere in nginx, but you can use it by yourself somewhere. For example, last parameter of try_files can be an named location.
Nginx will try to fetch file from /media/
url, when it fails, it will fallback to @media
named location, and that is an redirect to subdomain.
You can also use that fallback in 404 handler instead of try_files
:
location /media/ {
error_page 404 = @media;
}
Upvotes: 2