Apache 旁边的 NodeJS

NodeJS beside Apache

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

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

/etc/apache2/sites_available/default :

<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 配置领域的初学者,所以请尽可能多地描述您的答案;)。

谢谢你们!

运行 端口 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

访问