如何为我的Apache 2.4 VirtualHost编写条件CustomLog语句?

huangapple go评论83阅读模式
英文:

How do I write a conditional CustomLog statement for my Apache 2.4 VirtualHost?

问题

我正在使用以下版本的Apache(2.4)...

  1. $ sudo apachectl -v
  2. Server version: Apache/2.4.6 (CentOS)
  3. Server built: Apr 24 2019 13:45:48

我想在我的虚拟主机块中编写自定义日志指令,以便匹配我的域名的主机标头进入一个日志,而所有其他主机标头进入另一个日志...

  1. <VirtualHost *:80>
  2. ServerName mydomain.com
  3. # 使用主机标头确定日志位置
  4. # 使用主机标头确定如何响应。
  5. CustomLog /var/log/httpd/access_log "%h %l %u %t \"%r\" %>s %b" expr="%{HTTP_HOST} == 'mydomain.com'"
  6. CustomLog /var/log/httpd/garbage_access_log "%h %l %u %t \"%r\" %>s %b" expr="%{HTTP_HOST} != 'mydomain.com'"
  7. Alias /static /var/www/html/frontpage/static
  8. <Directory /var/www/html/frontpage/static>
  9. Require all granted
  10. </Directory>
  11. # 接下来,添加以下目录块
  12. <Directory /var/www/html/frontpage>
  13. <Files wsgi.py>
  14. Require all granted
  15. </Files>
  16. </Directory>
  17. WSGIDaemonProcess frontpage python-home=/var/www/html/venv python-path=/var/www/html
  18. WSGIProcessGroup frontpage
  19. WSGIScriptAlias / /var/www/html/frontpage_project/wsgi.py
  20. </VirtualHost>

然而,上述内容导致以下错误

  1. $ sudo apachectl configtest
  2. AH00526: Syntax error on line 6 of /etc/httpd/conf.d/mydomain.conf:
  3. CustomLog takes two or three arguments, a file name, a custom log format string or format name, and an optional "env=" or "expr=" clause (see docs)
  4. 如何正确设置自定义日志语句?
英文:

I'm using the following version of Apache (2.4) ...

  1. $ sudo apachectl -v
  2. Server version: Apache/2.4.6 (CentOS)
  3. Server built: Apr 24 2019 13:45:48

I want to write custom log directives in my virtual host block such that host headers taht match my domain go to one log and all others go to a different log ...

  1. &lt;VirtualHost *:80&gt;
  2. ServerName mydomain.com
  3. # Use the Host header to determine where to log
  4. # Use the Host header to determine how to respond.
  5. CustomLog /var/log/httpd/access_log &quot;%h %l %u %t \&quot;%r\&quot; %&gt;s %b&quot; expr=&quot;%{HTTP_HOST} == &#39;mydomain.com&#39;&quot;
  6. CustomLog /var/log/httpd/garbage_access_log &quot;%h %l %u %t \&quot;%r\&quot; %&gt;s %b&quot; expr=&quot;%{HTTP_HOST} != &#39;mydomain.com&#39;&quot;
  7. Alias /static /var/www/html/frontpage/static
  8. &lt;Directory /var/www/html/frontpage/static&gt;
  9. Require all granted
  10. &lt;/Directory&gt;
  11. # Next, add the following directory block
  12. &lt;Directory /var/www/html/frontpage&gt;
  13. &lt;Files wsgi.py&gt;
  14. Require all granted
  15. &lt;/Files&gt;
  16. &lt;/Directory&gt;
  17. WSGIDaemonProcess frontpage python-home=/var/www/html/venv python-path=/var/www/html
  18. WSGIProcessGroup frontpage
  19. WSGIScriptAlias / /var/www/html/frontpage_project/wsgi.py
  20. &lt;/VirtualHost&gt;

However, the above is producing this error

  1. $ sudo apachectl configtest
  2. AH00526: Syntax error on line 6 of /etc/httpd/conf.d/mydomain.conf:
  3. CustomLog takes two or three arguments, a file name, a custom log format string or format name, and an optional &quot;env=&quot; or &quot;expr=&quot; clause (see docs)

How do I set up the custom log statements properly?

答案1

得分: 0

我认为整个表达式应该被引用,包括"expr=",即:

  1. CustomLog /var/log/httpd/access_log "%h %l %u %t \"%r\" %>s %b" "expr=%{HTTP_HOST} == 'mydomain.com'"
  2. CustomLog /var/log/httpd/garbage_access_log "%h %l %u %t \"%r\" %>s %b" "expr=%{HTTP_HOST} != 'mydomain.com'"

请参阅https://httpd.apache.org/docs/2.4/expr.html#examples,最后一个示例。

英文:

I think that the whole expression should be quoted, together with the "expr=", i.e:

  1. CustomLog /var/log/httpd/access_log &quot;%h %l %u %t \&quot;%r\&quot; %&gt;s %b&quot; &quot;expr=%{HTTP_HOST} == &#39;mydomain.com&#39;&quot;
  2. CustomLog /var/log/httpd/garbage_access_log &quot;%h %l %u %t \&quot;%r\&quot; %&gt;s %b&quot; &quot;expr=%{HTTP_HOST} != &#39;mydomain.com&#39;&quot;

See https://httpd.apache.org/docs/2.4/expr.html#examples, the last example

huangapple
  • 本文由 发表于 2020年1月7日 00:16:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615448.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定