2

I'm setting up multiple apps on a server using Apache2 for "administrative" bits (like providing https, routing etc) and currently hosting them on different routes on the same domain, like

example.com/app1
example.com/app2

For one app, I've used

<VirtualHost *:443>
  ServerName example.com   # my domain here
  ProxyPass /app1/ http://127.0.0.1:1234/   # app's custom port here
  ProxyPassReverse /app1/ http://127.0.0.1:1234/   # and here

  ErrorLog ${APACHE_LOG_DIR}/app1_error.log
  CustomLog ${APACHE_LOG_DIR}/app1_access.log combined

  # ssh/https settings here
</VirtualHost>

Then, I've learned that there can be only one virtual host per domain, so I've added another proxy to set the correct routing:

<VirtualHost *:443>
  ServerName example.com
  ProxyPass /app1/ http://127.0.0.1:1234/
  ProxyPassReverse /app1/ http://127.0.0.1:1234/
  ProxyPass /app2/ http://127.0.0.1:2234/
  ProxyPassReverse /app2/ http://127.0.0.1:2234/

  ErrorLog ${APACHE_LOG_DIR}/app1_error.log
  CustomLog ${APACHE_LOG_DIR}/app1_access.log combined

  # ssh/https settings here
</VirtualHost>

Now I wonder how do I set different logs for different apps. Can I use Directory for proxies and do something like this? I've tried

<VirtualHost *:443>
  ServerName example.com

  <Directory /app1/>
    SetEnv app1
    ProxyPass /app1/ http://127.0.0.1:1234/
    ProxyPassReverse /app1/ http://127.0.0.1:1234/
  </Directory>
  <Directory /app2/>
    SetEnv app2
    ProxyPass /app2/ http://127.0.0.1:2234/
    ProxyPassReverse /app2/ http://127.0.0.1:2234/
  </Directory>

  ErrorLog ${APACHE_LOG_DIR}/app1_error.log env=app1
  CustomLog ${APACHE_LOG_DIR}/app1_access.log combined env=app1
  ErrorLog ${APACHE_LOG_DIR}/app2_error.log env=app2
  CustomLog ${APACHE_LOG_DIR}/app2_access.log combined env=app2

  # ssh/https settings here
</VirtualHost>

but running apachectl configtest gives:

[...] Syntax error [...]
ProxyPass cannot occur within section
[...]

This fail is kinda understandable, but what can I do instead to separate logs?
(Or may be the whole VirtualHost approach is not suitable for this and I should do something else?)

YakovL
  • 7,557
  • 12
  • 62
  • 102

1 Answers1

0
<VirtualHost *:443>
  ServerName example.com
  ProxyPass /app1/ http://127.0.0.1:4431/
  ProxyPassReverse /app1/ http://127.0.0.1:4431/
  ProxyPass /app2/ http://127.0.0.1:4432/
  ProxyPassReverse /app2/ http://127.0.0.1:4432/
</VirtualHost>

<VirtualHost *:4431>
  ServerName example.com
  ProxyPass /app1/ http://127.0.0.1:1234/
  ProxyPassReverse /app1/ http://127.0.0.1:1234/

  # Or
  # ProxyPass / http://127.0.0.1:1234/
  # ProxyPassReverse / http://127.0.0.1:1234/

  ErrorLog ${APACHE_LOG_DIR}/app1_error.log
  CustomLog ${APACHE_LOG_DIR}/app1_access.log combined

  # ssh/https settings here
</VirtualHost>

<VirtualHost *:4432>
  ServerName example.com
  ProxyPass /app2/ http://127.0.0.1:2234/
  ProxyPassReverse /app2/ http://127.0.0.1:2234/

  # Or
  # ProxyPass / http://127.0.0.1:2234/
  # ProxyPassReverse / http://127.0.0.1:2234/

  ErrorLog ${APACHE_LOG_DIR}/app2_error.log
  CustomLog ${APACHE_LOG_DIR}/app2_access.log combined

  # ssh/https settings here
</VirtualHost>
miken32
  • 42,008
  • 16
  • 111
  • 154
palindrom
  • 18,033
  • 1
  • 21
  • 37
  • thanks for the idea; I've tested this approach, unfortunately, this doesn't work: once I switch `/app2/` to port 4432 with VirtualHost for that port, I get "Service Unavailable" error (with both `/app2/` and `/` in routing of that VirtualHost) – YakovL Sep 05 '21 at 19:54