nginx通过php脚本提供图片

即时通讯不是非常有经验的Nginx,所以我有点与虚拟主机设置丢失。 基本上我的应用程序,当服务一些图像服务他们通过PHP脚本这是非常简单的与Apache:寻找物理形象 – >如果没有发现推送一切到index.php与请求string作为参数。 不,我正在尝试这个应用程序在Nginx上,除了通过脚本服务的图像(我只是得到一个404),一切工作。 这是我的vgin for nginx

server { listen 80; server_name ~^(www\.)?(?<sname>.+?).subdomain.domain.com$; root /var/www/$sname/current/public; index index.html index.htm index.php; location / { try_files $uri $uri/ /index.php$is_args$args; } location ~* \.(jpg|jpeg|gif|png|bmp|ico|pdf|flv|swf|exe|html|htm|txt|css|js) { add_header Cache-Control public; add_header Cache-Control must-revalidate; expires 7d; } location ~ \.php$ { fastcgi_pass unix:/var/run/php/php7.1-fpm.sock; include fastcgi_params; fastcgi_index index.php; } location ~ /\.ht { deny all; } 

}

看起来它只是在物理位置上寻找图像,这对于实际的物理图像是有效的,但是对于dynamic的图像来说,它们是用脚本来服务的。 任何帮助或指导,非常感谢。

更新:好的,所以我想,如果我从caching控制位置删除.jpg,它的工作原理,但我仍然想为这些dynamic图像请求设置caching标头,所以我怎么让它通过PHP运行,然后设置caching之后的标题?

您可以使用这个location块为您的图像:

 location ~* \.(jpg|jpeg|gif|png|bmp)$ { try_files $uri $uri/ /index.php$is_args$args; add_header Cache-Control public; add_header Cache-Control must-revalidate; expires 7d; } 

您可能需要修改try_files行中的/index.php?$is_args$args部分,以便为脚本获取正确的参数,因为您最初的问题没有清楚地显示出您想要的参数。

然后,对于其余的caching选项,请使用以下location块:

 location ~* \.(ico|pdf|flv|swf|exe|html|htm|txt|css|js)$ { add_header Cache-Control public; add_header Cache-Control must-revalidate; expires 7d; } 

我还将$添加到正则expression式匹配string中,以便只有以扩展名结尾的请求才被该块处理。 例如,在初始configuration的情况下,URL地址https://example.com/path/image.jpg75783将由您指定caching指令的location块处理。

另一种方法是在PHP脚本中设置图像caching标题。

为了将标题和过期添加到所有URI,您需要将语句放在server块中。 在这种情况下,它们被每个位置块inheritance。 例如:

 server { ... add_header Cache-Control public; add_header Cache-Control must-revalidate; expires 7d; location / { try_files $uri $uri/ /index.php$is_args$args; } location ~ \.php$ { ... } location ~ /\.ht { deny all; } }