Skip to content

Instantly share code, notes, and snippets.

@LoveDuckie
Last active August 12, 2022 12:27
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
This NGINX configuration can be used with Silverstripe for automatically serving generated .webp image assets. You can read more about that here: https://blog.theloveduckie.codes/automatically-serve-webp-images-with-silverstripe-and-nginx
map $http_accept $webp_suffix
{
default "";
"~*webp" ".webp";
}
server
{
listen 80;
listen [::]:80;
server_name ${WEBSITE_DOMAIN_NAMES};
server_tokens off;
return 301 https://${WEBSITE_DOMAIN_NAME}$request_uri;
}
server
{
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name ${WEBSITE_DOMAIN_NAMES};
server_tokens off;
charset utf-8;
ssl_certificate ${NGINX_CONFIG_CERTS_PATH}/fullchain.pem;
ssl_certificate_key ${NGINX_CONFIG_CERTS_PATH}/privkey.pem;
access_log /var/log/nginx/sites/${WEBSITE_DOMAIN_NAME}.access combined;
error_log /var/log/nginx/sites/${WEBSITE_DOMAIN_NAME}.error warn;
include /etc/nginx/mime.types;
client_max_body_size 0; # Manage this in php.ini (upload_max_filesize & post_max_size)
root /var/www/sites/${PRODUCTION_WEBSITE_DOMAIN_NAME}/public;
# Defend against SS-2015-013 -- http://www.silverstripe.org/software/download/security-releases/ss-2015-013
if ($http_x_forwarded_host) {
return 400;
}
location / {
try_files $uri /index.php?$query_string;
}
error_page 404 /assets/error-404.html;
error_page 500 /assets/error-500.html;
error_page 502 /assets/error-500.html;
error_page 503 /assets/error-500.html;
location ~* /assets/.+\.(?<extension>jpe?g|png|gif|webp)$
{
gzip_static on;
gzip_types image/png image/x-icon image/webp image/svg+xml image/jpeg image/gif;
add_header Vary Accept;
expires max;
sendfile on;
try_files "${request_uri}${webp_suffix}" $uri =404;
}
location ~* ^/assets/.*
{
gzip_static on;
gzip_types text/plain text/xml text/css
text/comma-separated-values application/json
image/png image/jpeg image/x-icon image/webp image/svg+xml image/gif
text/javascript application/x-javascript application/pdf
application/atom+xml;
expires max;
sendfile on;
try_files $uri =404;
}
location ^~ /resources/
{
gzip_static on;
gzip_types text/plain text/xml text/css
text/comma-separated-values application/json
image/png image/x-icon image/webp image/svg+xml image/jpeg image/gif
text/javascript application/x-javascript application/javascript
application/atom+xml;
expires max;
sendfile on;
try_files $uri =404;
}
location /index.php {
# client_header_timeout 10000;
# client_body_timeout 10000;
fastcgi_read_timeout 10000;
fastcgi_buffers 4 65k;
fastcgi_buffer_size 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_keep_conn on;
fastcgi_pass portfolio-php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment