【问题标题】:NodeJS beside ApacheApache 旁边的 NodeJS
【发布时间】:2016-09-07 20:09:03
【问题描述】:

我知道这种主题已经问了很多次了,但我没有找到任何符合我需要的答案。首先,我为我的英语道歉。

我想在 VPS 上运行一个节点应用程序和一个(或许多)其他常用网站。 所以你在同一个vps上托管了几个网站,我必须使用apache的virtualhost对吗?这就是我所做的:在 /var/www 我有 2 个目录:test1test2。我想要的是 test1 将是我的节点应用程序,可以通过 test1.my_domain 访问,而 test2 将是一个随机的其他网站,可以通过 test2.my_domain 访问。为此,我已经像这样配置 apache:

/etc/apache2/sites_available/默认:

<VirtualHost *:80>
ServerAdmin contact@my_domain
ServerName my_domain
ServerAlias www.my_domain

DocumentRoot /var/www
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
<Directory /var/www>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
</Directory>

ErrorLog /var/log/apache2/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog /var/log/apache2/access.log combined

Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
    Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>

</VirtualHost>

/etc/apache2/sites_available/test1.conf:

<VirtualHost *:80>
ServerAdmin contact@my_domain
ServerName my_domain
ServerAlias test1.my_domain

DocumentRoot /var/www/test1
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
<Directory /var/www/test1>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
</Directory>

ErrorLog /var/log/apache2/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog /var/log/apache2/access.log combined

Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
    Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>

</VirtualHost>

/etc/apache2/sites_available/test2.conf:

<VirtualHost *:80>
ServerAdmin contact@my_domain
ServerName my_domain
ServerAlias test2.my_domain

DocumentRoot /var/www/test2
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
<Directory /var/www/test2>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
</Directory>

ErrorLog /var/log/apache2/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog /var/log/apache2/access.log combined

Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
    Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>

</VirtualHost>

如果 test1 不是节点应用程序,此配置可以完美运行。我的意思是当我去 test1.my_domain 时,我会得到 /var/www/test1 的内容,如果我去 test2.my_domain >,我得到了/var/www/test2的内容。

但是如果我把一个节点应用程序放在 test1 中,节点进入与 apache 冲突,因为它们都使用端口 80。

我看过很多教程通过使用 apache 的代理来解决这个问题,但我不知道如何很好地使用它。 我确切地说我可以让节点监听 80 以外的其他端口,但我不想这样做,因为如果我这样做,url 将是 test1.my_domain:7777 (例如),它是丑对不对?

我也是 vps 配置领域的初学者,所以请尽可能多地描述你的答案;)。

谢谢大家!

【问题讨论】:

    标签: node.js apache proxy virtualhost vps


    【解决方案1】:

    在端口 7777 上运行节点应用程序,并将其用作 test1.conf

    <VirtualHost *:80>
    ServerAdmin contact@my_domain
    ServerName test1.my_domain
    
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:7777/
    ProxyPassReverse / http://127.0.0.1:7777/
    
    </VirtualHost>
    

    这样,您的节点应用程序应该可以通过http://test1.my_domain 访问

    【讨论】:

    • 好的,感谢您的帮助,我稍后会尝试。但是通过这种配置,服务器如何知道 test1.my_domain 在 /var/www/test1 中?
    • apache 不关心节点 files 在哪里(或者它是节点应用程序、tomcat 服务器还是其他),它只会将任何对 test1.my_domain 的请求转发到应用程序在端口 7777 上运行
    • 顺便说一句,这是非常常见的设置,你不是第一个有这个要求的:)
    • 我已经尝试过您的解决方案,但仍然无法正常工作。我的节点应用程序使用 socket.io :&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta charset="utf-8" /&gt; &lt;title&gt;Socket.io&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Communication avec socket.io !&lt;/h1&gt; &lt;script src="/socket.io/socket.io.js"&gt;&lt;/script&gt; &lt;script&gt; var socket = io.connect('test1.my_domain'); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; 而不是 test1.my_domain 我已经尝试过 test1.localhost:7777 和 test1.my_domain:7777 但它没有用。
    • 嗯,首先你需要让你的节点应用程序在端口 7777 上运行。你可以在浏览器中使用test1.my_domain:7777 打开它吗?
    猜你喜欢
    • 1970-01-01
    • 2020-06-22
    • 2015-08-11
    • 1970-01-01
    • 2012-09-24
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 2013-06-30
    相关资源
    最近更新 更多