如何将前端图像代理到后端图像? [英] How to proxy Frontend images into Backend images?

查看:73
本文介绍了如何将前端图像代理到后端图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理图像时遇到了一个小问题.我有一台前端服务器(site.com)和一台后端服务器(api.site.com),两者均由NFS存储器链接以获取图像. 原始图像存储在Back服务器中. 前端服务器仅获得Nginx&节点 后台只有Nginx& PHP

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

当我要显示图像时:site.com/img/path-to-img-s250x250.jpg,它将在NFS中搜索它,如果存在,它将被显示,如果不存在,则获取原始图像,调整其大小并将其保存在NFS中我们在API服务器中的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.

您向我推荐的重写规则仅在我想通过API显示图像(api.site.com/img/path-to-img-s250x250.jpg)时有效,但是我需要通过前端(site.com/img/path-to-img-s250x250.jpg)显示图像,您有什么主意吗?我怎样才能做到这一点?

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

推荐答案

参考上一个问题(

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.

您可以通过指定一个名为location作为try_files语句的最后一个参数来实现外部重定向.

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

例如:

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

这篇关于如何将前端图像代理到后端图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆