【问题标题】:NGINX show default image if not found如果没有找到,NGINX 显示默认图像
【发布时间】:2021-01-12 05:01:57
【问题描述】:

我有这个 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;
        }
}

【问题讨论】:

    标签: nginx http-status-code-404


    【解决方案1】:

    我通过将 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2023-02-05
      • 2013-02-02
      • 1970-01-01
      • 2015-08-07
      • 1970-01-01
      • 2012-06-17
      • 2012-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多