Score:0

Redirect causes 404 error on nginx

cn flag

How do I redirect if a user tries to direct access image files in browser only? I want to still keep the ability to allow social media sites to embed our images by hotlinking. I just want only if a user direct access image by browser to redirect.

This is my nginx conf

proxy_cache_path /var/www/img.example.com/htdocs/cache-store levels=1:2 keys_zone=pixstore:10m max_size=5g inactive=7d use_temp_path=off;
server {

    server_name img.example.com www.img.example.com;

    access_log /var/log/nginx/img.example.com.access.log ;
    error_log /var/log/nginx/img.example.com.error.log;

    add_header X-Proxy-Cache $upstream_cache_status;
    location / {
        proxy_cache pixstore;
        proxy_cache_revalidate on;
        proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
        proxy_cache_lock on;
        add_header X-Cache-Status $upstream_cache_status;
        proxy_pass http://xxx.xxx.xxx.xxx:8090;
        proxy_redirect off;
        include proxy_params;
        proxy_cache_valid 200 7d;
        proxy_cache_valid 404 5m;
    }

    location ~ "^/c/600x1200_90_webp/img-master/img/\d+/\d+/\d+/\d+/\d+/\d+/((?<filenum>\d+)[^/]+\.(jpg|png|webp))$" {
    valid_referers server_names;
    if ($invalid_referer = "0") {
    return 301 http://view.example.com/artwork/$filenum; }
    }

}

nginx error log

2022/01/19 12:09:23 [error] 7426#7426: *30 open() "/usr/share/nginx/html/c/600x1200_90_webp/img-master/img/2021/06/09/00/00/05/90423484_p0_master1200.jpg" failed (2: No such file or directory), client: xxx.xxx.xxx.xxx, server: img.example.com, request: "GET /c/600x1200_90_webp/img-master/img/2021/06/09/00/00/05/90423484_p0_master1200.jpg HTTP/1.1", host: "img.example.com"

The redirect isn't working but it gives me a 404 error if I try to access any image files. I setup this as a reverse proxy site so I didn't define a root path. How can I fix this?

us flag
Please include the full nginx configuration as shown by `nginx -T`.
jp flag
Don't edit your question to make it a new question when you already have an answer. Post another question.
Score:0
jp flag

As you didn't define a root path for image location nginx uses the default one - /usr/share/nginx/html/. If you want these files to be fetched with proxy_pass you need to copy the configuration for proxy_pass to this location too.

TravelWhere avatar
cn flag
I've added proxy_pass to the image location which fixed the 404 error but it doesn't redirect.
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.