如果找不到,NGINX 显示默认图像

NGINX show default image if not found

我有这个 NGINX 配置文件, 如果我加载任何根 url,它将显示我的 default.jpg 图像, 但我所有的邮件图像都存储在子文件夹中,例如:

/missing.jpg // works
/images/mising.jpg // shows nginx 404 not /default.jpg

这是我的 nginx 配置文件。 注意:我可以使用:/default.jpg 或 /index.php 输出相同的图像..

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html;

        index index.php;

        server_name _;

        location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
                add_header Cache-control "public";
                access_log   off;
                expires 365d;
        }

        location / {
                #try_files $uri $uri/ default.jpg;
                try_files $uri /index.php$is_args$args;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        }
}

我通过将 try_files 添加到图像缓存块来解决这个问题。

location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        add_header Cache-control "public";
        access_log   off;
        expires 365d;
        try_files $uri /index.php$is_args$args;
}