douchai7891 2019-04-01 12:29
浏览 158

如何将前端图像代理到后端图像?

I'm facing a little problem to handle images. I have a Front server (site.com) and Back server (api.site.com), both linked by an NFS Storage for images. The original images are stored in the Back server. The Front server got only Nginx & NodeJs The back office got only Nginx & PHP

When I want to display an image: site.com/img/path-to-img-s250x250.jpg, it will search it in the NFS, if it exists, it's gonna be displayed, if not, then get the original image, resize it and save it in the NFS through our PHP script in the API server.

The rewriting rules you recommended me, will work only if i want to display images through the API (api.site.com/img/path-to-img-s250x250.jpg), but i'll need to display images trough the Front (site.com/img/path-to-img-s250x250.jpg), do you have any idea how I can achieve this?

# site.com/img/prods/10002/filename-w200h200-bgF00.12345.jpg
location ~ "^/img/(.*)/([a-z0-9-]+)-w([0-9]+)h([0-9]+)(-bg([0-9A-Fa-f]{3,6}))?\.[0-9]{5}\.(jpg|jpeg|png|gif|ico|webp)$" {
    try_files $uri /img/$1/$2-w$3h$4$5.$7 @redirect;
}

location @redirect {
    proxy_pass http://api.site.com;
    proxy_set_header X-ORIGIN http://api.site.com;
    proxy_set_header X-Requested-With XMLHttpRequest;
}
  • 写回答

1条回答 默认 最新

  • doujiunai2169 2019-04-01 13:03
    关注

    Referring to your previous question (How can I check if file exists in nginx? otherwise run a rewrite rule) you need the final redirection to be external rather than internal.

    You can achieve an external redirection by specifying a named location as the last parameter of the try_files statement.

    For example:

    location ~ "^/img/(.*/[a-z0-9]+-[0-9]+x[0-9]+)\.[0-9]{5}\.(jpg|jpeg|png|gif|ico)$" {
        try_files $uri /img/$1.$2 @resize;
    }
    location @resize {
        rewrite "^/img/(.*)/([a-z0-9]+)-([0-9]+)x([0-9]+)\.[0-9]{5}\.(jpg|jpeg|png|gif|ico)$" https://api.example.com/image?path=$1&file=$2&ext=$5&w=$3&h=$4 redirect;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题