【问题标题】:Serving Images from Subdomain Node.js, Apache从子域 Node.js、Apache 提供图像
【发布时间】:2021-05-21 00:07:09
【问题描述】:

我目前以“/imageserving/date/image.png”之类的格式存储图像

当我收到图像请求时,它会转到我后端的 /imageserving,它会返回与文件夹相关的图像。我现在正在考虑引入一个子域。我的子域需要实际获取请求,但是如何将请求路由到子域?

目前我的图像是 img src={databaseitem},其中数据库项目是 /imageserving/date/image.png。这将使用 mysite.com/theimage 填充它,我需要将其重新路由到我的子域,以便它返回 img.mysite.com/theimage

我目前在 Apache Web 服务器上运行我的 nodejs 应用程序。

我将如何重新路由请求?我是否必须在后端进行,或者我可以在我的网络服务器上进行更改。

编辑:问题措辞不当 我基本上试图将来自 mysite.com/imageserving/date/xxx 的请求代理到 img.mysite.com/date/xxx

sendImage: async function(req, res) {

    console.log(req.hostname);


    var filepath = path.join(__dirname, '../../img/uploads/' + req.params.year + '/' + req.params.month + '/' + req.params.id);
    //console.log(filepath);
    res.sendFile(filepath);
}

这显然是发送图像请求并将 2 文件夹返回子域文件夹,然后获取文件名。我希望它不仅从子域文件夹中获取文件名,还希望从子域本身发送图像。

这是我的服务器配置

<Location "/">
<IfModule mod_passenger.c>
    PassengerAppEnv "development"

    PassengerEnabled on
    PassengerBaseURI "/"
    PassengerAppRoot "/home/xxx/mainapp"
    PassengerAppGroupName "xxx - mainappname"
    PassengerRuby /opt/cpanel/ea-ruby24/root/usr/libexec/passenger-ruby24
    PassengerPython /usr/bin/python
    PassengerNodejs /opt/cpanel/ea-nodejs10/bin/node
</IfModule>
</Location>
<Directory "/home/xxx/mainapp">
   Allow from all
   Options -MultiViews
   Options -Indexes
   Require all granted
</Directory>

【问题讨论】:

  • 为什么要代理它们?这似乎效率低下。你需要在它们周围加上授权吗?
  • 好吧,我需要通过子域提供图像,所以当我将 api 请求发送到 mysite.com/imageserving/date/xxx 时,我认为我需要从 img.mysite.com 返回文件名mysite.com,否则它不会从子域发送文件。我想另一种选择是在生产中硬编码从子域发送文件名,但与将 Apache 配置为仅将所有 /imageserving 请求发送到子域相比,这似乎是一种创可贴的解决方案。

标签: node.js apache express subdomain


【解决方案1】:

你是如何配置 apache 和 nodejs 的?

1) 节点做所有事情

您可以配置 apache 以将许多域路由到您的 nodejs 项目,并让 nodejs 为所有内容提供服务。 (不需要子域,但在这里)

<VirtualHost *:80>
    ServerName exemple.com
    ServerAlias img.exemple.com

    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/socket.io [NC]
    RewriteCond %{QUERY_STRING} transport=websocket [NC]
    RewriteRule /(.*) ws://localhost:10000/$1 [P,L]

    ProxyRequests Off
    ProxyPass / http://localhost:10000/
    ProxyPassReverse / http://localhost:10000/
    ProxyPreserveHost On
</VirtualHost>

然后在nodejs中

const express = require("express");
const app = express();

app.get("/imageserving/date/:name", (req, res, next) => {
    if (req.hostname != 'img.exemple.com') return next(new Error('nothing here'))

    res.sendFile('/home/xxx/imageserving/date/'+req.params.name);
});

app.get("/whatever", (req, res) => {
    if (req.hostname != 'exemple.com') return next(new Error('nothing here'))

    res.send('ok');
});

现在可以了:

2) 或相反

告诉 apache 处理图像服务和 nodejs 进行处理

<VirtualHost *:80>
    ServerName exemple.com

    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/socket.io [NC]
    RewriteCond %{QUERY_STRING} transport=websocket [NC]
    RewriteRule /(.*) ws://localhost:10000/$1 [P,L]

    ProxyRequests Off
    ProxyPass / http://localhost:10000/
    ProxyPassReverse / http://localhost:10000/
    ProxyPreserveHost On
</VirtualHost>


<VirtualHost *:80>
    ServerName img.exemple.com
    DocumentRoot /xxx/xxx
</VirtualHost>
const express = require("express");
const app = express();

app.get("/whatever", (req, res) => {
    res.send(req.hostname);
});

现在可以了:

【讨论】:

  • 除了更改乘客让我运行 server.js 而不是 app.js 之外,我还没有配置我的 Apache。所以我必须用 site.conf 文件中的端口创建一个虚拟主机?对于选项 1,您是说我什至不需要子域,我可以让 apache 将 img.site.com 识别为主域?另外,根据您在 conf 中所做的更改,它会将所有请求重新路由到子域?如果我只希望 /imageserving api 请求转到子域怎么办?
  • 如果没有您当前的配置,我无能为力。你能在你的帖子中粘贴相关信息吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多