Score:0

Nginx prevent hotlinking and redirect not work with new Nginx version

sb flag

In the past, this works perfectly:

#domain1.com
location /files/ {
    valid_referers none blocked domain1.com domain2.com domain3.com;
    if ($invalid_referer) {
        return 403;
    }
    if (!-e $request_filename) {
        rewrite ^(.*)$ https://domain2.com/files/$1 redirect; 
    }
}

but doesn't work now after I upgraded nginx

I found this, seems if not working with new nginx version: https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/

I also found this hack to solve problem: https://agentzh.blogspot.com/2011/03/how-nginx-location-if-works.html

But I don't know how to make it works.

This is my .htaccess file:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !domain1\.com [NC]
RewriteCond %{HTTP_REFERER} !domain2\.com [NC]
RewriteCond %{HTTP_REFERER} !domain3\.com [NC]
RewriteRule \.rar$ - [F,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ http://domain2.com/file/$1 [L,R,NC]
Michael Hampton avatar
cz flag
What do you mean by "doesn't work"? That really gives no useful information. Exactly what is happening?
sb flag
@Michael Hampton 1. Last, before update nginx, if i browse directly or click on link from other sites (except domain1.com domain2.com domain3.com) it show Forbiden page. Now not, it can show normally 2. Last if http://domain1.com/file.html not exist it redirect to http://domain2.com/file.html Now, not redirect
Michael Hampton avatar
cz flag
That is strange. Your `location` block looks like it should do this successfully. Maybe there is something else in your nginx configuration that causes the problem. Please post the complete output from the command `nginx -T`
sb flag
Thanks for your help Michael Hampton. I think I must re-install server without nginx :)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.