使用 Apache 2.4 并按照这里的示例进行操作https://httpd.apache.org/docs/2.4/expr.html
这是我的代码
<If "!%{REQUEST_URI} =~ m#files/cache/static/[A-Za-z_\-0-9]+.cache#">
Header set foo-checksum "expr=%{md5:foo}"
</If>
我现在正在玩,但是一旦我使用
Header set foo-checksum "expr=%{md5:foo}"
按照他们的例子 - 我得到
AH00526: Syntax error on line 130 of /etc/httpd/conf/httpd.conf:
httpd[19459]: Unrecognized header format %
httpd.service: main process exited, code=exited, status=1/FAILURE
在 apache 启动时。有什么想法吗?我也在表达式之外尝试过,但还是出现同样的错误
其他表达方式也有效。
答案1
您需要使用 Apache v2.4.10 或更高版本,并且由于 Ubuntu 14.04 LTS 附带 v2.4.7,因此您很可能正在使用这个版本。文档在 Header 指令文档的开头简要提到了这一点:
兼容性:SetIfEmpty 在 2.4.7 及更高版本中可用,expr=value 在 2.4.10 及更高版本中可用
我运行了几个服务器来确认这个功能有效
Apache v2.4.12:
root@ubuntu-server:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 15.10
Release: 15.10
Codename: wily
root@ubuntu-server:~# cat /etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:80>
Header set foo-checksum "expr=%{md5:foo}"
</VirtualHost>
root@ubuntu-server:~# apachectl -v
Server version: Apache/2.4.12 (Ubuntu)
Server built: Jul 24 2015 15:59:00
root@ubuntu-server:~# service apache2 reload
root@ubuntu-server:~# curl -I localhost
HTTP/1.1 200 OK
Date: Mon, 07 Mar 2016 21:15:38 GMT
Server: Apache/2.4.12 (Ubuntu)
Last-Modified: Mon, 07 Mar 2016 20:57:04 GMT
ETag: "2c39-52d7bb502f118"
Accept-Ranges: bytes
Content-Length: 11321
Vary: Accept-Encoding
foo-checksum: acbd18db4cc2f85cedef654fccc4a4d8
Content-Type: text/html
但不适用于 Apache v2.4.7:
root@ubuntu-server:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.4 LTS
Release: 14.04
Codename: trusty
root@ubuntu-server:~# cat /etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:80>
Header set foo-checksum "expr=%{md5:foo}"
</VirtualHost>
root@ubuntu-server:~# apachectl -v
Server version: Apache/2.4.7 (Ubuntu)
Server built: Jan 14 2016 17:45:23
root@ubuntu-server:~# service apache2 reload
* Reloading web server apache2 *
* The apache2 configtest failed. Not doing anything.
Output of config test was:
AH00526: Syntax error on line 2 of /etc/apache2/sites-enabled/000-default.conf:
Unrecognized header format %
Action 'configtest' failed.
The Apache error log may have more information.