-
-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better rules for Nginx #166
Comments
After a lot of tweaking, I have come up with a solution. # WebP Express rules
# --------------------
location ~* ^/wp-content/.*\.(png|jpe?g)$ {
add_header Vary Accept;
expires 365d;
}
location ~* ^/wp-content/.*\.webp$ {
expires 365d;
if ($whattodo = AB) {
add_header Vary Accept;
}
}
if ($http_accept ~* "webp"){
set $whattodo A;
}
if (-f $request_filename.webp) {
set $whattodo "${whattodo}B";
}
if ($whattodo = AB) {
rewrite ^(.*) $1.webp last;
}
if ($whattodo = A) {
rewrite ^/wp-content/.*\.(jpe?g|png)$ /wp-content/plugins/webp-express/wod/webp-on-demand.php?xsource=x$request_filename&wp-content=wp-content break;
}
# ------------------- (WebP Express rules ends here) Beware when copy/pasting: You might get html-encoded characters. Verify that the ampersand before "wp-content" isn't encoded I'm using this hack for checking multiple The "expires 365d;" lines set caching to one year. You can remove these lines if you wish. |
Here is another way to achieve the same: In # WebP Express rules
# --------------------
location ~* ^/?wp-content/.*\.(png|jpe?g)$ {
add_header Vary Accept;
# expires 365d;
if ($http_accept !~* "webp"){
break;
}
try_files
$uri.webp
/wp-content/plugins/webp-express/wod/webp-on-demand.php?xsource=x$request_filename&wp-content=wp-content
;
}
# ------------------- (WebP Express rules ends here) Beware when copy/pasting: You might get html-encoded characters. Verify that the ampersand before "wp-content" isn't encoded Again, in WebP Express settings, you must choose Destination folder: mingled and File extension: Append ".webp" The solution is based on this recipe. But I have added the converter script in the try_files. Also, in order for the php to be processed, I have removed "$uri =404;" from If you only want the rules to apply to the uploads folder, change the first line to: Beware that if you haven't enabled png conversion, you should replace "(png|jpe?g)" with "jpe?g". |
Here is a third solution. # WebP Express rules
# --------------------
location ~* ^/wp-content/.*\.(png|jpe?g)$ {
add_header Vary Accept;
expires 365d;
if ($http_accept ~* "webp"){
set $whattodo A;
}
if (-f $request_filename.webp) {
set $whattodo "${whattodo}B";
}
if ($whattodo = AB) {
rewrite ^(.*) $1.webp last;
}
if ($whattodo = A) {
rewrite ^/wp-content/.*\.(jpe?g|png)$ /wp-content/plugins/webp-express/wod/webp-on-demand.php?xsource=x$request_filename&wp-content=wp-content last;
}
}
location ~* ^/wp-content/.*\.webp$ {
expires 365d;
if ($whattodo = AB) {
add_header Vary Accept;
}
}
# ------------------- (WebP Express rules ends here) For reference, the rules presented in the blog post by keycdn looks like this: location ~ ^(/path/to/your/images.+)\.(jpe?g|png)$ {
if ( $http_accept ~* webp ) {
set $webp "A";
}
if ( $request_filename ~ (.+)\.(png|jpe?g)$ ) {
set $file_without_ext $1;
}
if ( -f $file_without_ext.webp ) {
set $webp "${webp}E";
}
if ( $webp = AE ) {
add_header Vary Accept;
rewrite ^(.+)\.(png|jpe?g)$ $1.webp break;
}
} |
Here are rules for routing non-existing webp requests to converter: location ~* ^/?wp-content/.*\.(png|jpe?g)\.webp$ {
try_files
$uri
/wp-content/plugins/webp-express/wod/webp-realizer.php?wp-content=wp-content
;
} I'm getting the hang of it :) |
Your config always run php.
PS. I checked it with |
@koekaverna: You can configure the plugin to store images in the same folder as the originals on the plugin settings screen. However, images outside of the upload folder (ie theme images) are always stored in a separate folder so your setup is better. Thanks |
When destination is set to "mingled", your modified line is needed in addition to the original (for theme images etc). So that solution becomes: # WebP Express rules
# --------------------
location ~* ^/?wp-content/.*\.(png|jpe?g)$ {
add_header Vary Accept;
# expires 365d;
if ($http_accept !~* "webp"){
break;
}
try_files
/wp-content/webp-express/webp-images/doc-root/$uri.webp
$uri.webp
/wp-content/plugins/webp-express/wod/webp-on-demand.php?xsource=x$request_filename&wp-content=wp-content
;
}
# ------------------- (WebP Express rules ends here) When configured to store in separate folder, you can remove the line containing "$uri.webp" |
…needed even in mingled mode). Thanks, @koekaverna. #166
i tried above all things but still it shows error for Nginx.
|
problem is solved but it not generate for lazyload images. |
How was the problem solved? |
We should provide better rules for Nginx.
The rules should redirect directly to existing webp files, and set Vary: Accept header when it does
Current rule is simply:
The text was updated successfully, but these errors were encountered: