如何构建PHP / Node代理以在https网站上呈现外部http映像? [英] How to build a PHP/Node proxy to render external http images on https website?
问题描述
我有一个在 https
上运行的网站。
我必须从没有https的外部服务器(外部域)加载图像,但是单个 http
协议。
I have a website running on https
.
I have to load images from external server (external domain) which doesn't have https, but single http
protocol.
有没有办法通过PHP或Node处理 http
图像的代理?所以我可以像这样渲染图像:
Is there's any way to handle proxy for http
images via PHP or Node? So I can render images like this:
<img src="https://domain.com/proxy?url=http://externaldomain.com/image.jpg" />
这个想法是避免在本地保存图像,但只显示它们。
The idea is to avoid saving images on local, but only display them.
当我尝试在 https
域内呈现 http
服务图像时,我得到了这个控制台消息:
When I try to render http
served images inside https
domain, I get this console message:
https: //domain.com/ 显示来自 http://externaldomain.com/image.jpg的不安全内容a>。
同样,地址栏内的SSL(/ https)锁定图标变为灰色。
As well, my SSL (/https) lock icon inside address bar becomes grey.
推荐答案
您可以使用node,它只管道图像,而不是在发送到客户端之前将整个图像加载到内存中(如 file_get_contents
会在php中执行)。
为了简化流媒体,请在此示例中使用请求:
You can use node, which will just pipe the image instead of loading the whole image into memory before sending to the client (like file_get_contents
would do in php).
Using request in this example for simplicity in streaming:
var https = require('https');
var url = require('url');
var request = require('request');
var server = https.createServer(function (req, res) {
var queryData = url.parse(req.url, true).query;
if (queryData.url) {
var x = request(queryData.url);
req.pipe(x).pipe(res);
} else {
res.writeHead(400, {"Content-Type": "text/plain"});
res.end("No url");
}
});
// Listen on port 443
server.listen(443);
这篇关于如何构建PHP / Node代理以在https网站上呈现外部http映像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!