Skip to content

Instantly share code, notes, and snippets.

@apsolut
Created March 15, 2023 19:29
Show Gist options
  • Save apsolut/22b0705d71bb882283ea38c5278348ff to your computer and use it in GitHub Desktop.
Save apsolut/22b0705d71bb882283ea38c5278348ff to your computer and use it in GitHub Desktop.
nginx directives
location ~* ^(/.+)\.(jpg|jpeg|jpe|png|gif)$ {
	add_header Vary Accept;

	if ($http_accept ~* "webp"){
		set $imwebp A;
	}
	if (-f $request_filename.webp) {
		set $imwebp  "${imwebp}B";
	}
	if ($imwebp = AB) {
		rewrite ^(.*) $1.webp;
	}
}

# webp rewrite rules for jpg and png images
# try to load alternative image.png.webp before image.png
location /app/u {
	location ~ \.(png|jpe?g)$ {
		add_header Vary "Accept-Encoding";
		access_log off;
		log_not_found off;
		expires max;
		try_files $uri$webp_suffix $uri =404;
	}
}

# webp rewrite rules for EWWW testing image
location /app/p/ewww-image-optimizer/images {
	location ~ \.(png|jpe?g)$ {
		add_header Vary "Accept-Encoding";
		access_log off;
		log_not_found off;
		expires max;
		try_files $uri$webp_suffix $uri =404;
	}
}

location ~* \.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf|x-html|css|xml|js|woff|woff2|ttf|svg|eot)$ {
	expires 365d;
}

gzip on;
gzip_disable "MSIE [1-6]\\.(?!.*SV1)";
gzip_proxied any;
gzip_comp_level 5;
gzip_types text/plain text/css application/javascript application/x-javascript text/xml application/xml application/rss+xml text/javascript image/x-icon image/bmp image/svg+xml;
gzip_vary on;

location ~* \.(blade\.php)$ {
	deny all;
}

location ~* composer\.(json|lock)$ {
	deny all;
}

location ~* package(-lock)?\.json$ {
	deny all;
}

location ~* yarn\.lock$ {
	deny all;
}

# Rewrites for Yoast SEO XML Sitemap
rewrite ^/sitemap_index.xml$ /index.php?sitemap=1 last;
rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;




Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment