大网数据广告活动
服务器运维问答

nginx serve image via php script

· 52CE 我爱测

im not very experienced with nginx so I am a bit lost with vhost set up. Basically my app, when serving some images serves them via php script this is very simple with apache: look for physical image -> if not found push everythi…

问题原文

原帖链接 · Auris · CC BY-SA 3.0

im not very experienced with nginx so I am a bit lost with vhost set up. Basically my app, when serving some images serves them via php script this is very simple with apache: look for physical image -> if not found push everything in to index.php with the request string as params. No I am tryingtorun this app on nginx and everything works except the image serving via script (I just get a 404). Here is my vhost 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;
    }

}

It seems that it is only looking for the image in a physical location, this works for actual physical images, but not for dynamic ones where they are served vua a script. Any help or guidance is greatly appreciated.

Update: ok so i figured out that if I remove the .jpg from cache controll location, it works, but I still want to set cache headers for those dynamic image requests, so how do I make it run via php and then set the cache header after that?

已采纳解答

原帖链接 · Tero Kilkanen · CC BY-SA 3.0

You can use this location block for your images:

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;
}

You might need to modify /index.php?$is_args$args part on the try_files line so that you get the correct parameters for your script, as your initial question didn't show clearly what the parameters are you want.

Then, for rest of caching options, use this location block:

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

I also added the $ to the regex matching strings, so that only requests ending with the extension are processed by that block. For example, with your initial configuration, an URL https://example.com/path/image.jpg75783 would be processed by your location block where you specify your cache directives.

Another alternative is to set your image caching headers in your PHP script.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-07-12)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。