plan2net / webp

Create a WebP copy for images (TYPO3 CMS)
GNU General Public License v3.0
60 stars 34 forks source link

Apache rewrite is working, nginX not #12

Closed the-hotmann closed 5 years ago

the-hotmann commented 5 years ago

When I set everything up with Apache (.htaccess) everything is working. As soon as I switch to nginX the provided rewrite rules are not working anymore.

This is my config:

global conf (/etc/nginx/conf.d/webp.conf)

map $http_accept $webp_suffix {
    default   "";
    "~*webp"  ".webp";
}

server conf

location ~* ^/fileadmin/.+\.(png|jpg|jpeg)$ {
        add_header Vary Accept;
        try_files $uri$webp_suffix $uri =404;
}

No errors are shown, just the normal jpg gets fired out. You can clearly see the difference in the size the files got now. I tested this nearly 10 times to not make any mistake but when just serving files from Apache this works nice, as soon as I switch to nginX its not working anymore.

Do I have to add anything else to make it work with nginX

wazum commented 5 years ago

@M4rt1n17 What's the output for cat /etc/nginx/mime.types|grep webp and does your browser send webp in the HTTP Accept header?

the-hotmann commented 5 years ago

My output is:

root@server:~# cat /etc/nginx/mime.types|grep webp
    image/webp                                       webp;

So yes it's sending the right mime type. On 2 Servers I use Plesk, even there it does not work as soon as you use nginX to serve static files.

jacobsenj commented 5 years ago

@M4rt1n17 I just had exactly the same issue. However I found - after a long long long session - that the lines in my server config:

        location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg)$ {
                expires 30d;
        }

where preventing the latter call of

        location ~* ^/(fileadmin|media)/.+\.(?:png|jpe?g)$ {
                expires 30d;
                add_header Vary Accept;
                try_files $uri$webp_suffix $uri =404;
        }

I moved the block above the upper lines and it works now.

the-hotmann commented 5 years ago

@jacobsenj thanks man! I will test this today and report back. If it works this fix/hack will hopefully be mentioned in the documentation.

Thanks again for posting it!

wazum commented 5 years ago

@M4rt1n17 let me know if it works and I'll be happy to add it as advice in case of problems.

the-hotmann commented 5 years ago

Just tested on one of my Plesk Servers. Did not work. All my server conf (vhost conf) is:

location ~* ^/(fileadmin|media)/.+\.(?:png|jpe?g)$ {
    expires 30d;
    add_header Vary Accept;
    try_files $uri$webp_suffix $uri =404;
}

@jacobsenj nice you use the RegEx jpe?g instead of jpg|jpeg already thought about creating an issue for this but as it is not issue I did not, but now you remind me of this :) In my server conf there is no other line above this..

Back2Topic: it does not work. I can clearly see how when I turn on nginX it is delivering the 280kb big pic instead of the 78kb size. (280kb is the original jpg)

Someone else using nginX & Plesk and got it working?

Edit: its working.. in Plesk to be able to use this server conf you have to disable "deliver these extension directly through nginX" !! Otherwise it will not accept any rules at this extensions and just deliver it straight away.

Thanks!

wazum commented 5 years ago

I added a hint to the documentation

the-hotmann commented 5 years ago

@wazum there is something else I noticed

when I using the nginX config from the doc:

location ~* ^/fileadmin/.+\.(png|jpg|jpeg)$ {
        add_header Vary Accept;
        try_files $uri$webp_suffix $uri =404;
}

It did not work aswell. When switching to @jacobsenj version it worked. At me it looks like this:

location ~* ^/(fileadmin|media)/.+\.(?:png|jpe?g)$ {
    add_header Vary Accept;
    try_files $uri$webp_suffix $uri =404;
}

I did not inspect this problem to deep as switching helped. Maybe this also should be changed in the doc.