无法使用 Laravel 使用 Nginx 将图像转发到 webp
Can't forward images to webp with Nginx using Laravel
我正在尝试将所有 jpeg 和 png 图像转发到 webp。这是一个 Laravel 项目。 /storage 文件夹中的大多数图像作为 public 文件夹中的符号链接。我遵循了指南,我知道一点正则表达式,但我找不到任何问题的线索。
我在每个文件夹中都有 webp 图片。
path/to/image.jpg
path/to/image.jpg.webp
ngnix.conf 文件:
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
map $http_accept $webp_suffix {
default "";
"~*webp" ".webp";
}
服务器配置:
server{
root /var/www/site/public;
server_name example.com www.example.org;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
location ~* \.(jpg|jpeg|png|webp|gif|ico|css|js)$ {
expires 365d;
}
location ~* ^.+\.(png|jpe?g)$ {
add_header Vary Accept;
try_files $uri$webp_suffix $uri =404;
}
error_page 404 /index.php;
if ($host = example.org) {
return 301 https://www.example.org$request_uri;
} # managed by Certbot
location ~ \.php$ {
include snippets/fastcgi-php.conf;
}
location ~ /\.ht {
deny all;
}
location ~ /\.(?!well-known).* {
deny all;
}
listen 443 ssl http2;
listen [::]:443 ssl http2;
ssl_certificate /etc/letsencrypt/live/example.org/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.org/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server{
if ($host = www.example.org) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = example.org) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name example.org www.example.org;
return 404; # managed by Certbot
}
我不知道我做错了什么。任何帮助将不胜感激。
正则表达式 location
块按顺序求值,直到找到匹配项。有关详细信息,请参阅 this document。
您有两个 location
表达式匹配 .png
、.jpg
和 .jpeg
。您需要从第一个表达式中删除这些,第二个表达式才能成功。
例如:
location ~* \.(gif|ico|css|js)$ {
expires 365d;
}
location ~* \.(png|jpe?g)$ {
expires 365d;
add_header Vary Accept;
try_files $uri$webp_suffix $uri =404;
}
我正在尝试将所有 jpeg 和 png 图像转发到 webp。这是一个 Laravel 项目。 /storage 文件夹中的大多数图像作为 public 文件夹中的符号链接。我遵循了指南,我知道一点正则表达式,但我找不到任何问题的线索。
我在每个文件夹中都有 webp 图片。
path/to/image.jpg
path/to/image.jpg.webp
ngnix.conf 文件:
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
map $http_accept $webp_suffix {
default "";
"~*webp" ".webp";
}
服务器配置:
server{
root /var/www/site/public;
server_name example.com www.example.org;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
location ~* \.(jpg|jpeg|png|webp|gif|ico|css|js)$ {
expires 365d;
}
location ~* ^.+\.(png|jpe?g)$ {
add_header Vary Accept;
try_files $uri$webp_suffix $uri =404;
}
error_page 404 /index.php;
if ($host = example.org) {
return 301 https://www.example.org$request_uri;
} # managed by Certbot
location ~ \.php$ {
include snippets/fastcgi-php.conf;
}
location ~ /\.ht {
deny all;
}
location ~ /\.(?!well-known).* {
deny all;
}
listen 443 ssl http2;
listen [::]:443 ssl http2;
ssl_certificate /etc/letsencrypt/live/example.org/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.org/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server{
if ($host = www.example.org) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = example.org) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name example.org www.example.org;
return 404; # managed by Certbot
}
我不知道我做错了什么。任何帮助将不胜感激。
正则表达式 location
块按顺序求值,直到找到匹配项。有关详细信息,请参阅 this document。
您有两个 location
表达式匹配 .png
、.jpg
和 .jpeg
。您需要从第一个表达式中删除这些,第二个表达式才能成功。
例如:
location ~* \.(gif|ico|css|js)$ {
expires 365d;
}
location ~* \.(png|jpe?g)$ {
expires 365d;
add_header Vary Accept;
try_files $uri$webp_suffix $uri =404;
}