首页 > 解决方案 > 如何在 Apache2 中为不同的代理(应用程序)设置不同的日志?

问题描述

我正在使用 Apache2 为“管理”位(例如提供 https、路由等)在服务器上设置多个应用程序,并且当前将它们托管在同一域上的不同路由上,例如

example.com/app1
example.com/app2

对于一个应用程序,我使用过

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

然后,我了解到每个域只能有一个虚拟主机,所以我添加了另一个代理来设置正确的路由:

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

现在我想知道如何为不同的应用程序设置不同的日志。我可以使用Directory代理并做这样的事情?我试过了

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

但运行apachectl configtest给出:

[...] 语法错误 [...]
ProxyPass 不能在
[...]部分中发生

这种失败是可以理解的,但我能做些什么来分隔日志呢?
(或者可能整个 VirtualHost 方法不适合这个,我应该做点别的?)

标签: apache2

解决方案


我只是在头脑风暴。如果这是胡说八道,请随意投反对票:)

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

推荐阅读