Skip to content

suse 上apache 启用 module_headers 并配置 cache-control

  • by

启用 headers_moule

在 centos 配置比较简单,只需要 loadmodules 即可:

cat /etc/httpd/conf.modules.d/00-base.conf | grep headers
LoadModule headers_module modules/mod_headers.so

但在 suse 上,却是通过 sysconfig 来配置的:


cat /etc/sysconfig/apache2 | grep MODULES | grep -v "^#"
APACHE_MODULES="actions alias auth_basic authn_core authn_file authz_core authz_groupfile authz_host authz_user autoindex cgi dir env expires include log_config mime negotiation perl reqtimeout setenvif socache_shmcb ssl userdir headers"

需要编辑这行内容,并且如果 /usr/lib64/apache2 下有 so 文件即可。查看模块方式如下:

/usr/sbin/apache2ctl -t -D DUMP_MODULES | grep shared
AH00557: httpd-prefork: apr_sockaddr_info_get() failed for mirrors
AH00558: httpd-prefork: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
actions_module (shared)
alias_module (shared)
auth_basic_module (shared)
authn_core_module (shared)
authn_file_module (shared)
authz_core_module (shared)
authz_groupfile_module (shared)
authz_host_module (shared)
authz_user_module (shared)
autoindex_module (shared)
cgi_module (shared)
dir_module (shared)
env_module (shared)
expires_module (shared)
include_module (shared)
log_config_module (shared)
mime_module (shared)
negotiation_module (shared)
perl_module (shared)
reqtimeout_module (shared)
setenvif_module (shared)
socache_shmcb_module (shared)
ssl_module (shared)
userdir_module (shared)
headers_module (shared)

其中静态和动态加载的都显示出来了。

## 配置header中的cache-control

比如其中的一个配置为:


PerlRequire "/etc/apache2/smt-mod_perl-startup.pl"

Alias "/SUSE" "/srv/www/htdocs/repo/SUSE"
Alias repo "/srv/www/htdocs/repo"

<Directory "/srv/www/htdocs/repo">
    Options +Indexes +FollowSymLinks
    IndexOptions +NameWidth=*
    PerlAuthenHandler NU::SMTAuth
    AuthName SMTAuth
    AuthType Basic
    Require valid-user

    <IfModule mod_headers.c>
            Header set Via "host-name"
            Header set Cache-Control "max-age=43200"
            <FilesMatch "\.(xml|xm_|gz|sh|conf|tar|repo|bz2)$">
                    Header set Cache-Control "max-age=300"
            </FilesMatch>
    </IfModule>

    # Allow unauthenticated access to /repo/tools/ directory
    Require expr %{REQUEST_URI} =~ m#^/repo/tools/.*#
</Directory>



设置了两个 header, 并且根据文件后缀来设置不同的值。

验证效果

xml 文件


curl -I http://localhost/repo/SUSE/Updates/SLE-SAP/12-SP2/x86_64/update/repodata/repomd.xml
HTTP/1.1 200 OK
Date: Thu, 27 Sep 2018 03:41:23 GMT
Server: Apache
Last-Modified: Tue, 25 Sep 2018 16:06:01 GMT
ETag: "c22-576b448324c40"
Accept-Ranges: bytes
Content-Length: 3106
Via: smt-root-b
Cache-Control: max-age=300
Content-Type: text/xml

其他文件


curl -I http://localhost/repo/SUSE/Updates/SLE-SAP/12-SP2/x86_64/update/x86_64/MozillaFirefox-52.8.0esr-109.31.2.x86_64.rpm
HTTP/1.1 200 OK
Date: Thu, 27 Sep 2018 03:41:01 GMT
Server: Apache
Last-Modified: Fri, 18 May 2018 06:05:07 GMT
ETag: "2bfe4cd-56c74bbd72ac0"
Accept-Ranges: bytes
Content-Length: 46130381
Via: smt-root-b
Cache-Control: max-age=43200
Content-Type: application/x-rpm