简体   繁体   English

提供来自子域 Node.js、Apache 的图像

[英]Serving Images from Subdomain Node.js, Apache

I'm currently storing the images in a format like '/imageserving/date/image.png'我目前以“/imageserving/date/image.png”之类的格式存储图像

When I get an image request it goes to the /imageserving on my backend which returns the image pertaining to the folder.当我收到图像请求时,它会转到我后端的 /imageserving,它会返回与文件夹相关的图像。 I'm now thinking of introducing a subdomain.我现在正在考虑引入一个子域。 My subdomain needs to actually get the request, but how do I route the request to the subdomain?我的子域需要实际获取请求,但是如何将请求路由到子域?

Currently i have the images going as img src={databaseitem}, where the database item is /imageserving/date/image.png.目前,我将图像作为 img src={databaseitem},其中数据库项是 /imageserving/date/image.png。 This populates it with mysite.com/theimage, I need to reroute it to my subdomain so it returns img.mysite.com/theimage这将使用 mysite.com/theimage 填充它,我需要将其重新路由到我的子域,以便它返回 img.mysite.com/theimage

Im currently running my nodejs app on an Apache web server.我目前在 Apache web 服务器上运行我的 nodejs 应用程序。

How would I go about rerouting the request?我将如何 go 关于重新路由请求? Do I have to do it in the backend, or is something I can change on my web server.我是否必须在后端进行,或者我可以在我的 web 服务器上进行更改。

Edit: The question was worded poorly Im basically trying to proxy requests from mysite.com/imageserving/date/xxx to img.mysite.com/date/xxx编辑:这个问题措辞不佳我基本上试图将来自 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);
}

This is obviously sending an image request and going 2 folder back to the subdomain folder, and then getting the filename.这显然是发送图像请求并将 2 文件夹返回到子域文件夹,然后获取文件名。 I want it not only to get the filename from the subdomain folder, but also send the image from the subdomain itself.我不仅希望它从子域文件夹中获取文件名,还希望它从子域本身发送图像。

This is my server configuration这是我的服务器配置

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

how did you configure apache and nodejs?你是如何配置apache和nodejs的?

1) node does everything 1)节点做一切

you can configure apache to route many domains to your nodejs project and let nodejs serve everything.您可以配置 apache 以将许多域路由到您的 nodejs 项目并让 nodejs 为所有内容提供服务。 (no need for subdomain, but here it is) (不需要子域,但在这里)

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

and then in nodejs然后在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');
});

now this works:现在这有效:

2) or the opposite 2) 或相反

tell apache to deal with image serving and nodejs to do processing告诉 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);
});

now this works:现在这有效:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM