-
-
Save niladam/50decb7533b163daa71b62452cad88f9 to your computer and use it in GitHub Desktop.
Serving WEBP with nginx conditionally.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user www-data; | |
http { | |
## | |
# Basic Settings | |
## | |
sendfile on; | |
tcp_nopush on; | |
tcp_nodelay on; | |
# IMPORTANT!!! Make sure that mime.types below lists WebP like that: | |
# image/webp webp; | |
include /etc/nginx/mime.types; | |
default_type application/octet-stream; | |
gzip on; | |
gzip_disable "msie6"; | |
## | |
# Conditional variables | |
## | |
map $http_accept $webp_suffix { | |
default ""; | |
"~*webp" ".webp"; | |
} | |
## | |
# Minimal server | |
## | |
server { | |
listen 80 default_server; | |
listen [::]:80 default_server ipv6only=on; | |
root /usr/share/nginx/html; | |
index index.html; | |
# Make site accessible from http://localhost/ or whatever you like | |
server_name localhost; | |
location ~* ^/images/.+\.(png|jpg)$ { | |
root /home/www-data; | |
add_header Vary Accept; | |
try_files $uri$webp_suffix $uri =404; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment