Score:0

Nginx redirect not working if using cloudflare

cn flag

I'm using nginx to do a redirect if a user direct access image by browser. The problem is it doesn't work when you use cloudflare.

nginx config

    location ~ "^/c/600x1200_90_webp/img-master/img/\d+/\d+/\d+/\d+/\d+/\d+/((?<filenum>\d+)[^/]+\.(jpg|png|webp))$" {
    proxy_pass http://xxx.xxx.xxx.xxx:8081;
    valid_referers server_names example.com;
    if ($invalid_referer = "1") {
    add_header Cache-Control "no-cache";
    return 301 https://example.com/detail?id=$filenum; 
}
    }

Is there anyway to fix this without disabling cloudflare? I want to still able to cache and redirect

Score:0
us flag

Cloudflare caches the response from your nginx, and serves it directly to visitors without accessing your nginx.

This means that your nginx configuration cannot be used to selectively serve different content based on referer header.

Cloudflare has its own Hotlink protection feature, which might do what you need.

If that is not enough, then you could use Cloudflare Workers to implement the policy you want. However, that requires knowledge on how HTTP requests are prcessed and careful work to keep things running smoothly.

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.