I'm using nginx as a reverse proxy for my web application with proxy_pass for the api.
This is my current config, where I've nginx directive location /data/
where images are being displayed from the /data directory of my docker container.
location /data/ {
alias /data/;
error_page 404 = @api;
recursive_error_pages on;
log_not_found off;
autoindex off;
include cache.conf;
}
location /api/ {
error_page 418 = @api;
return 418;
}
location @api {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-NginX-Proxy true;
proxy_buffering off;
proxy_request_buffering off;
proxy_http_version 1.1;
proxy_pass http://api:1338;
proxy_cache_bypass $http_upgrade;
}
How to add fallback location to the same named location nginx directive /data/
where I need to add public s3 bucket's url in proxy_pass
and at first tries to search for that image inside that s3 bucket using proxy_pass, if found then serves that image from there, if returns 403 or 404 then use alias directive /data/
where it will serve that image from the /data directory of container.
I've tried with this config file, but it fails the nginx config rules check when I validate the nginx configuration using nginx -t
command, and shows error nginx: [emerg] the "alias" directive cannot be used inside the named location
server {
location /data/ {
proxy_pass http://bucket_url;
error_page 404 500 502 503 504 = @fallback;
}
location @fallback {
alias /data/;
}
}
/data/
fromlocation /data/ { ... }
andalias /data/
two equal strings? Or those are different?alias /data/
as a fallback where it will search for that same image inside the/data/
directory on the host.