You can use the following Nginx configuration to automatically fetch images from a backup site when a 404 error is encountered for an image on this site:
http {
upstream backup_images {
server backup.example.com;
}
server {
listen 80;
server_name example.com;
root /var/www/html;
location /images/ {
try_files $uri @backup;
}
location @backup {
proxy_pass http://backup_images;
}
}
}This configuration willexample.com的80Listen on the port and forward all requests to/var/www/htmlIn the directory. All/images/The initial request will be processed. If the image requested by this site does not exist,NginxThe system will automatically forward the request to the backup site.backup.example.comRetrieve images from a backup site.
Note that this configuration is being used.upstreamUse a module to define a backup site. If yourNginxThe version does not come with built-in functionality.upstreamThe module requires manual compilation.NginxTurn onupstreamModule. Additionally, due to the presence of a proxy, the backup site must be configured.CORSStrategy – Allowexample.comOtherwise, the browser will reject the request for images from the backup site.
My Usage
I have two websites: one is the main site, and the other is a backup image site!
The images for the two stations are identical, and the image paths are also the same!
Since there were too many images when backing up the main server and I didn't want to back them up, I decided to separate the images out!
The path for this site is /d/file/ – images are stored there!
The backup route follows the same principle; at this point, the pseudo-static rule is written as follows: