1

I've been replacing SVG images on my web site with SVGZ files, but I'm having trouble getting Pyramid to serve them from a static_url.

__init.py__ has:

config.add_static_view(name='static', path='app:static', cache_max_age=3600)

and the template has:

<img src="{{ 'app:static/img/file.svgz'|static_url }}" />

When the filename ends with .svg Pyramid serves up the file, but when I replace it with .svgz (yes, the SVGZ file is in the same directory with the same permissions as the SVG file), Pyramid doesn't respond to the URL and nginx reports a 404 (because the file isn't at /app/static/img/file.svgz as far as nginx is concerned).

I have a sense this is because Pyramid doesn't know how to set the Content-Type / Content-Encoding for SVGZ so it hands it back to nginx, similar to this question about web fonts: Web fonts always return 404 from static path. Unfortunately, there's no answers in that posting, so I'm hoping someone out there knows how to configure Pyramid to serve up SVGZ (and other less usual types) statically.

Other similar questions (How to set the content type header in response for a particular file type in Pyramid web framework) mention that Pyramid uses the mimetypes library to figure out file type information, but that library seems to grok svgz, so I don't know why it can't figure out how to serve up these files:

Python 3.4.2 (default, Oct  8 2014, 10:45:20) 
[GCC 4.9.1] on linux
>>> import mimetypes
>>> mimetypes.guess_type('file.svgz')
('image/svg+xml', 'gzip')

FWIW, my Nginx config has the following, but I doubt this has much to do with what Pyramid is up to.

location ~ \.svgz$ {
    add_header Content-Encoding gzip;
}

Edit: I think it is a misconfiguration of Nginx that's causing the problem, not anything to do with Pyramid. I'm using a proxy setup identical to what's in the Pyramid cookbook (http://docs.pylonsproject.org/projects/pyramid-cookbook/en/latest/deployment/nginx.html), including the static configuration shown in step 3, which states: "It's somewhat odd that the root doesn't point to the static directory, but it works because Nginx will append the actual URL to the specified path."

It looks to me as though there's some conflict between the location ~ \.svgz$ directive listed above and the static configuration for the Pyramid app. When I remove it, Nginx serves up the SVGZ file from my Pyramid static path, but it fails to set the proper content-encoding so it doesn't load in the browser. When I add the directive back in, Nginx reports a 404. It's as though the match on .svgz somehow short circuits appending the proper URL.

Community
  • 1
  • 1
cswingle
  • 76
  • 1
  • 6
  • `mimetypes` should definitely cut it. You are using the same Python interpreter / installation run your application and try `mimetypes` from command line? E.g. you don't have old mimetypes installation somewhere? – Mikko Ohtamaa Dec 19 '15 at 17:32
  • 1
    @MikkoOhtamaa, yes, I tested `mimetypes` using the same virtualenv that runs the Pyramid apps. – cswingle Dec 21 '15 at 23:25
  • The next step is to plant a debug logging / debug print to Pyramid itself. 1) Start your WSGI server attached to terminal 2) Add print here https://github.com/Pylons/pyramid/blob/master/pyramid/response.py#L57 [because it is definitely using mimetypes] 3) Run download to see what it prints. You should see if it's problem with mimetypes itself or something else fails inside that view. – Mikko Ohtamaa Dec 22 '15 at 07:17

1 Answers1

0

1) Start your WSGI server attached to a terminal

2) Add a print here https://github.com/Pylons/pyramid/blob/master/pyramid/response.py#L57 (because it is definitely using mimetypes, as commented before)

3) Run download view to see what it prints. You should see if it's problem with mimetypes itself or something else fails inside that view.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
  • It must be something about my production setup. Running `pserve production.ini` yields a server (waitress) that will serve SVGZ from the static image directory and the print statement inserted into `response.py` shows `image/svg_xml` and gzip encoding. But when run on 127.0.0.1 and served up via nginx proxy, SVGZ doesn't work. I think my nginx setup is the culprit, not Pyramid. – cswingle Dec 22 '15 at 16:35
  • Please add your Nginx and related configuration to the question if it's relevant. – Mikko Ohtamaa Dec 22 '15 at 17:37
  • I could if you think it'd help, but the configuration for the app is exactly as it is in the Pyramid Cookbook and the only relevant thing that's been added is the `location ~ \.svgz$ {}` configuration so nginx knows to send the correct header. – cswingle Dec 23 '15 at 19:08