Apache:如何为多个应用程序设置虚拟主机?

时间:2019-04-15 22:56:11

标签: apache2

我正在尝试将Apache设置为具有用于多个应用程序的多个路由/链接。

例如,

mydomain.net/projects/app1

mydomain.net/projects/app2

这些应用程序将在单个端口上启动:app1在端口81上运行,app2在端口82上运行,等等。

有可能做到吗?

我做了一些研究,发现guide是以这种方式进行的:

example

我试图做与指南相同的事情

并且当我在端口81上启动我的app1(node-js-sample)时,由于端口81已被使用,因此它不允许我使用。

/etc/apache2/sites-available/projectRoute.conf

:/etc/apache2/sites-available$ cat projectRoute.conf
<VirtualHost *:81>
        DocumentRoot "/home/rex/node-js-sample/"
        ServerName node-js-sample
</VirtualHost>

/etc/apache2/ports.conf

:/etc/apache2$ cat ports.conf
# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf

Listen 80
Listen 81

<IfModule ssl_module>
        Listen 443
</IfModule>

<IfModule mod_gnutls.c>
        Listen 443
</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

/etc/apache2/apache2.conf

:/etc/apache2$ cat apache2.conf
ServerName node-js-sample
# This is the main Apache server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See http://httpd.apache.org/docs/2.4/ for detailed information about
# the directives and /usr/share/doc/apache2/README.Debian about Debian specific
# hints.
#
...

/home/rex/node-js-sample/index.js

:~/node-js-sample$ ls
Procfile   app.json  node_modules       package.json
README.md  index.js  package-lock.json  public

:~/node-js-sample$ cat index.js
var express = require('express')
var app = express()

app.set('port', (process.env.PORT || 81 ))
app.use(express.static(__dirname + '/public'))

app.get('/', function(request, response) {
  response.send('Hello World!')
})

app.listen(app.get('port'), function() {
  console.log("Node app is running at localhost:" + app.get('port'))
})

此外,我有一个运行在端口3000上的React Application Homepage,并拥有一个使用反向代理连接到端口3000的VistualHost(端口80)。简而言之,浏览mydomain.net是React Application Homepage。 / p>

/etc/Apache2/sites_available/newRoute.conf

<VirtualHost *:80>

        ServerName domainname.net
    ServerAlias www.domainname.net
    ServerAdmin myname@hotmail.com

        RewriteEngine On
    RewriteCond %{HTTP:Upgrade} =websocket [NC]
    RewriteRule /(.*)       ws://localhost:3000/$1 [P,L]
    RewriteCond %{HTTP:Upgrade} !=websocket [NC]
    RewriteRule /(.*)       http://localhost:3000/$1 [P,L]

    ProxyPassReverse / http://localhost:3000/

</VirtualHost>

我希望在端口81上运行node-js-sample 并浏览到mydomain.net/node-js-sample

将显示“ Hello-world”

谢谢

0 个答案:

没有答案