如何在 Nginx 反向代理后面的 Apache 上使用 WebP 图像

如何在 Nginx 反向代理后面的 Apache 上使用 WebP 图像

我的问题是我有一个 CentOS Web 面板:

我有一个Nginx 反向代理在端口上运行80/443将请求转发至Apache 2.4.52在港口8181Apache 使用的模块有:

Loaded Modules:
core_module (static)
 so_module (static)
 http_module (static)
 mpm_event_module (static)
 authn_file_module (shared)
 authn_core_module (shared)
 authz_host_module (shared)
 authz_groupfile_module (shared)
 authz_user_module (shared)
 authz_core_module (shared)
 access_compat_module (shared)
 auth_basic_module (shared)
 reqtimeout_module (shared)
 filter_module (shared)
 deflate_module (shared)
 mime_module (shared)
 log_config_module (shared)
 env_module (shared)
 expires_module (shared)
 headers_module (shared)
 unique_id_module (shared)
 setenvif_module (shared)
 version_module (shared)
 proxy_module (shared)
 proxy_connect_module (shared)
 proxy_http_module (shared)
 unixd_module (shared)
 status_module (shared)
 autoindex_module (shared)
 dir_module (shared)
 userdir_module (shared)
 alias_module (shared)
 rewrite_module (shared)
 ssl_module (shared)
 security2_module (shared)
 proxy_fcgi_module (shared)
 rpaf_module (shared)
 suphp_module (shared)

Apache 使用目前 PHP-FPM 8.0.20 在这个网络服务器上,我有一个WordPress 的安装,WebP Express。我在其他主机和页面上使用了它,但目前它不起作用,它说我没有安装 mod_headers。我试过了,mod_headers 工作正常,我能够更改缓存和其他标头,但不知何故,它没有将我重定向到 .webp 图像。

目前,在这个例子中,在其他托管上,如果我从服务器请求图像,我会得到以下标头:URL:https://www.workingexampleurl.com/wp-content/uploads/2022/05/test.png

date: Tue, 26 Jul 2022 09:56:25 GMT
server: Apache
last-modified: Thu, 19 May 2022 16:26:32 GMT
etag: "dcf6-5df5fd632cd92"
accept-ranges: bytes
content-length: 56566
vary: Accept
x-webp-express: Redirected directly to existing webp
cache-control: private, max-age=2592000
content-type: image/webp

在此托管设置上,我以某种方式获得了此信息:URL:https://notworkingexampleurl.com/wp-content/uploads/2022/07/test.png

server: nginx/1.22.0
date: Tue, 26 Jul 2022 09:59:37 GMT
content-type: image/png
content-length: 46758
last-modified: Thu, 21 Jul 2022 15:12:12 GMT
connection: keep-alive
keep-alive:timeout=60
vary: Accept-Encoding
etag: "62d96ccc-b6a6"
expires: Thu, 31 Dec 2037 23:55:55 GMT
cache-control: max-age=315360000
x-cache: HIT from Backend
strict-transport-security: max-age=31536000
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
accept-ranges: bytes

当我尝试使用同一个域名时阿帕奇 8181端口我从服务器获得了这些标头:

Date: Tue, 26 Jul 2022 10:27:09 GMT
Server: Apache/2.4.52 (Unix) OpenSSL/1.0.2k-fips
Vary: Accept,User-Agent
Last-Modified: Tue, 26 Jul 2022 08:22:25 GMT
ETag: "7fc6-5e4b1001bf660"
Accept-Ranges: bytes
Content-Length: 32710
X-WebP-Express: Redirected directly to existing webp
Content-Type: image/webp

在里面/etc/nginx/proxy.inc我有这些文件:

#More info here: http://wiki.nginx.org/HttpProxyModule
proxy_buffering off;
proxy_connect_timeout 59s;
proxy_send_timeout 600;
proxy_read_timeout 600;
proxy_buffer_size 64k;
proxy_buffers 16 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_pass_header Set-Cookie;
proxy_redirect off;
#proxy_hide_header Vary;
proxy_hide_header X-Powered-By;
proxy_set_header Accept-Encoding '';
#If you want to get the cache-control and expire headers from apache, comment out 'proxy_ignore_headers' and uncomment 'proxy_pass_header Expires;' and 'proxy_pass_header Cache-Control
proxy_pass_header Expires;
proxy_pass_header Cache-Control;
proxy_pass_header Vary;
#proxy_pass_request_headers on;
#proxy_ignore_headers Cache-Control Expires;
proxy_set_header Referer $http_referer;
proxy_set_header Host $host;
proxy_set_header Cookie $http_cookie;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

这是 nginx 代理上的 vhost 的配置: /etc/nginx/conf.d/vhosts/notworkingexample.hu.ssl.conf

server {
    listen 192.168.1.15:443 ssl ;
    server_name notworkingexample.hu  www.notworkingexample.hu;
    
    access_log /usr/local/apache/domlogs/notworkingexample.hu.bytes bytes;
    access_log /usr/local/apache/domlogs/notworkingexample.hu.log combined;
    error_log /usr/local/apache/domlogs/notworkingexample.hu.error.log error;

    ssl_certificate      /etc/pki/tls/certs/notworkingexample.hu.bundle;
    ssl_certificate_key  /etc/pki/tls/private/notworkingexample.hu.key;
    ssl_protocols TLSv1.2;
    ssl_ciphers EECDH+ECDSA+AESGCM:EECDH+aRSA+AESGCM:EECDH+ECDSA+SHA384:EECDH+ECDSA+SHA256:EECDH+aRSA+SHA384:EECDH+aRSA+SHA256:EECDH+aRSA!RC4:EECDH:!RC4:!aNULL:!eNULL:!LOW:!3DES:!MD5:!EXP:!PSK:!SRP:!DSS;
    ssl_prefer_server_ciphers   on;

    ssl_session_cache   shared:SSL:10m;
    ssl_session_timeout 60m;

    location / {
        location ~.*\.(3gp|gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|html|htm|txt|js|css|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso|woff|ttf|svg|eot|sh|webp)$ {
            root /home/superuser/public_html/notworkingexample.hu;
            expires max;
            try_files $uri $uri/ @backend;
        }
        
        error_page 405 = @backend;
        error_page 500 = @custom;
        add_header X-Cache "HIT from Backend";
        add_header Strict-Transport-Security "max-age=31536000";
        add_header X-XSS-Protection "1; mode=block" always;
        add_header X-Content-Type-Options "nosniff" always;
        proxy_pass http://192.168.1.15:8181;
        include proxy.inc;
    }

    location @backend {
        internal;
        proxy_pass http://192.168.1.15:8181;
        include proxy.inc;
    }

    location @custom {
        internal;
        proxy_pass http://192.168.1.15:8181;
        include proxy.inc;
    }

    location ~ .*\.(php|jsp|cgi|pl|py)?$ {
        proxy_pass http://192.168.1.15:8181;
        include proxy.inc;
    }

    location ~ /\.ht    {deny all;}
    location ~ /\.svn/  {deny all;}
    location ~ /\.git/  {deny all;}
    location ~ /\.hg/   {deny all;}
    location ~ /\.bzr/  {deny all;}

    disable_symlinks if_not_owner from=/home/superuser/public_html/notworkingexample.hu;

    location /.well-known/acme-challenge {
        default_type "text/plain";
        alias /usr/local/apache/autossl_tmp/.well-known/acme-challenge;
    }

    location /.well-known/pki-validation {
        default_type "text/plain";
        alias /usr/local/apache/autossl_tmp/.well-known/acme-challenge;
    }
}

最后这是当前域的配置,/usr/local/apache/conf.d/vhosts/notworkingexample.hu.conf这不起作用:

<VirtualHost 192.168.1.15:8181>
    ServerName notworkingexample.hu
    ServerAlias www.notworkingexample.hu
    ServerAdmin [email protected]
    DocumentRoot /home/superuser/public_html/notworkingexample.hu
    UseCanonicalName Off
    ScriptAlias /cgi-bin/ /home/superuser/public_html/notworkingexample.hu/cgi-bin/

    #CustomLog /usr/local/apache/domlogs/notworkingexample.hu.bytes bytes
    #CustomLog /usr/local/apache/domlogs/notworkingexample.hu.log combined
    ErrorLog /usr/local/apache/domlogs/notworkingexample.hu.error.log

    # Custom settings are loaded below this line (if any exist)
    IncludeOptional "/usr/local/apache/conf/userdata/superuser/notworkingexample.hu/*.conf"

    <IfModule mod_setenvif.c>
        SetEnvIf X-Forwarded-Proto "^https$" HTTPS=on
    </IfModule>

    <IfModule mod_userdir.c>
        UserDir disabled
        UserDir enabled superuser
    </IfModule>

    <IfModule mod_suexec.c>
        SuexecUserGroup superuser superuser
    </IfModule>

    <IfModule mod_suphp.c>
        suPHP_UserGroup superuser superuser
        suPHP_ConfigPath /home/superuser
    </IfModule>

    <IfModule mod_ruid2.c>
        RMode config
        RUidGid superuser superuser 
    </IfModule>

    <IfModule itk.c>
        AssignUserID superuser superuser 
    </IfModule>

    <Directory "/home/superuser/public_html/notworkingexample.hu">
        AllowOverride All
        Require all granted
    </Directory>

    <IfModule proxy_fcgi_module>
        <FilesMatch \.php$>
            SetHandler "proxy:unix:/opt/alt/php-fpm80/usr/var/sockets/superuser.sock|fcgi://localhost"
        </FilesMatch>
    </IfModule>


</VirtualHost>

任何建议都很好!

答案1

经过越来越多的测试,我发现了问题所在。我尝试了另一个 Nginx 代理服务器,它工作正常,所以我搜索并测试了配置。问题出在 vhost nginx 配置中的这一部分,我删除了 jpg、jpeg、png 和 webp 扩展,然后它就可以正常工作了:

(3gp|gif|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|html|htm|txt|js|css|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso|woff|ttf|svg|eot|sh)$ {
            root %docroot%;
            expires max;
            try_files $uri $uri/ @backend;
        }

相关内容