将一个域中的两个虚拟主机与一个子目录组合在一起

时间:2017-11-07 13:12:00

标签: apache url-rewriting virtualhost virtual-hosts

我有一个由Apache提供服务的网站example.comexample2.com已重定向到端口3001(使用NodeJS)。它适用于此配置:

<VirtualHost *:80>
  ServerName www.example.com
  DocumentRoot /home/www/example
  <Directory />
    Options FollowSymLinks
    AllowOverride All
    Order deny,allow
    Allow from all
    Require all granted
  </Directory>
</VirtualHost>

<VirtualHost *:80>
  ServerName www.example2.com
  RewriteEngine On
  RewriteCond %{REQUEST_URI}  ^/socket.io            [NC]
  RewriteCond %{QUERY_STRING} transport=websocket    [NC]
  RewriteRule /(.*)           ws://localhost:3001/$1 [P,L]
  ProxyPass / http://localhost:3001/
  ProxyPassReverse / http://localhost:3001/
</VirtualHost>

现在我想拥有这个(因为我不会续订域example2.com):

  • example.com =&gt;由Apache =&gt;提供服务/home/www/example
  • example.com/website2 =&gt;使用3001(先前在example2.com上提供的那个)
  • 重定向到NodeJS网站

问题:我即将使用以下代码,但它是否正确使用Directory

<VirtualHost *:80>
  ServerName www.example.com
  DocumentRoot /home/www/example
  <Directory />
    Options FollowSymLinks
    AllowOverride All
    Order deny,allow
    Allow from all
    Require all granted
  </Directory>
  <Directory /website2/>
    RewriteEngine On
    RewriteCond %{REQUEST_URI}  ^/socket.io            [NC]
    RewriteCond %{QUERY_STRING} transport=websocket    [NC]
    RewriteRule /(.*)           ws://localhost:3001/$1 [P,L]
    ProxyPass / http://localhost:3001/
    ProxyPassReverse / http://localhost:3001/
  </Directory>
</VirtualHost>

1 个答案:

答案 0 :(得分:0)

我认为你应该使用基于部分的URL的<Location>指令来稳定运行。

每个部分指令都有基于订单和部分的优先级。 (section applied order) 如果同一部分由多个指令分层,<Directory>基于物理文件系统且优先级较低,例如<Location><Directory><File>等。

例如,

<VirtualHost *:80>
  ServerName www.example.com
  DocumentRoot /home/www/example
  <Directory /home/www/example>
    Options FollowSymLinks
    AllowOverride All
    #Order deny,allow
    #Allow from all
    # above access control conf mean same with below conf
    Require all granted
  </Directory>

  # Location is not necessary to mod_rewrite directives.
  # rewrite conf can be coontrol the conditions with RewriteCond
  RewriteEngine On
  RewriteCond %{REQUEST_URI}  ^/website2/socket.io   [NC]
  RewriteCond %{QUERY_STRING} transport=websocket    [NC]
  RewriteRule ^/website2/(.*)           ws://localhost:3001/$1 [P,L]

  # ProxyPass is now allowed into Location as following patterns,
  # but 'ProxyPass http://localhost:3001/' is available into the Location directive but limited in Location URL pattern.
  ProxyPass        / http://localhost:3001/
  ProxyPassReverse / http://localhost:3001/
</VirtualHost>