大网数据广告活动
服务器运维问答

Do I need to configure SVG images showing in nginx?

· 52CE 我爱测

I have SVG images in a folder and also a single PNG file to make sure these images are shown on the page. My nginx config is basically this: server { listen 80; location / { root www; } } The images are in www/images folder. What…

问题原文

原帖链接 · Sergei Basharov · CC BY-SA 3.0

I have SVG images in a folder and also a single PNG file to make sure these images are shown on the page.

My nginx config is basically this:

server {
    listen 80;

    location / {
        root www;
    }

}

The images are in www/images folder.

What I see is with the same structure in the page:

<img src="images/logo.svg">
<img src="images/logo.png">

PNG file is shown on the page just as expected, but not SVG. It shows in network tab of the browser code 200, though, but empty body.

When I try to open it directly, like http://localhost/images/logo.svg, it just downloads it as a file.

Do I need to add some special config to make SVG files showable on a page like PNG/JPG files?

社区高分解答

原帖链接 · Ananda Masri · CC BY-SA 4.0

When Nginx doesn't recognize the mime type for .svg files, it serves them with content type header content-type: text/plain and your browser may not render them or may issue a warning to the web console.

  1. Ensure that /etc/nginx/mime.types defines a mime type for svg images:

    types {
      ...
      image/svg+xml   svg svgz;
      ...
    }
    
  2. Ensure that /etc/nginx/nginx.conf includes the mime.types file in the http block:

    http {
      include mime.types;
      ...
    }
    
  3. In the terminal check that your new nginx configuration has no errors:

    sudo nginx -t
    
  4. Restart NGINX for the changes to take effect

    sudo systemctl restart nginx   // e.g.: ubuntu
    
  5. In your browser dev tools under network headers, check that svg files are served with header Content-Type: image/svg+xml and possibly hard refresh your web page to update the browser cache.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-09-20)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。