0

I have a website example.com served by Apache, and example2.com redirected to port 3001 (using NodeJS). It works with this config:

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

Now I would like to have this (because I won't renew the domain example2.com):

  • example.com => served by Apache => /home/www/example
  • example.com/website2 => redirected to the NodeJS website using 3001 (the one previously served on example2.com)

Question: I'm about to use the following code, but is it a correct use of 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>
Basj
  • 41,386
  • 99
  • 383
  • 673

1 Answers1

0

I think you should use the <Location> directives for section based URL for stable operation.

Each section directives have priorities by order and section based. (section applied order) <Directory> is based physical file system and low priority if the same section layered by multiple directives, such as <Location>, <Directory>, <File> and so on.

For example,

<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>
Daein Park
  • 4,393
  • 2
  • 12
  • 21