如何向我的 apache2 服务器添加标头

如何向我的 apache2 服务器添加标头

我有一个仅用于测试的 apache2 Web 服务器(不是生产服务器)。它在 Ubuntu 18.04 上运行。我已使用 TLS 配置了它。我想添加一个标头。所以我导航到这个文件:/etc/apache2/sites-available/default-ssl.conf

现在文件内容是:

<IfModule mod_ssl.c>
    <VirtualHost _default_:443>
        ServerAdmin [email protected]
        ServerName myownsite.com

        DocumentRoot /var/www/myownsite.com/html


        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        #   SSL Engine Switch:
        SSLEngine on

        <FilesMatch "\.(cgi|shtml|phtml|php)$">
                SSLOptions +StdEnvVars
        </FilesMatch>
        <Directory /usr/lib/cgi-bin>
                SSLOptions +StdEnvVars
        </Directory>


        Header always set Expect-CT: max-age=86400, enforce, report-uri="https://myownserver.com/report"
    </VirtualHost>
</IfModule>

如你所见,最后我添加了一个标头,当客户端以 https:// 格式打开网站时,我希望在服务器的响应中看到该标头。

但是,我在 .conf 文件中添加标题后,想要重新加载 apache 以使更改生效,使用:

sudo systemctl reload apache2

我收到此错误:

Job for apache2.service failed because the control process exited with error code.
See "systemctl status apache2.service" and "journalctl -xe" for details.

有人能找出我的配置有什么问题吗?如何解决这个问题?

编辑1:

执行后:systemctl status apache2.service

这是我得到的,但我不知道我该怎么办。

● apache2.service - The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
           └─apache2-systemd.conf
   Active: active (running) (Result: exit-code) since Mon 2019-08-19 12:26:44 BST; 47min ago
  Process: 2632 ExecReload=/usr/sbin/apachectl graceful (code=exited, status=1/FAILURE)
  Process: 660 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
 Main PID: 757 (apache2)
    Tasks: 55 (limit: 3548)
   CGroup: /system.slice/apache2.service
           ├─ 757 /usr/sbin/apache2 -k start
           ├─2369 /usr/sbin/apache2 -k start
           └─2370 /usr/sbin/apache2 -k start

Aug 19 12:59:04 e-VirtualBox apachectl[2502]: The Apache error log may have more information.
Aug 19 12:59:04 e-VirtualBox systemd[1]: apache2.service: Control process exited, code=exited stat
Aug 19 12:59:04 e-VirtualBox systemd[1]: Reload failed for The Apache HTTP Server.
Aug 19 13:08:16 e-VirtualBox systemd[1]: Reloading The Apache HTTP Server.
Aug 19 13:08:16 e-VirtualBox apachectl[2632]: AH00526: Syntax error on line 132 of /etc/apache2/si
Aug 19 13:08:16 e-VirtualBox apachectl[2632]: Too many arguments to directive
Aug 19 13:08:16 e-VirtualBox apachectl[2632]: Action 'graceful' failed.
Aug 19 13:08:16 e-VirtualBox apachectl[2632]: The Apache error log may have more information.
Aug 19 13:08:16 e-VirtualBox systemd[1]: apache2.service: Control process exited, code=exited stat
Aug 19 13:08:16 e-VirtualBox systemd[1]: Reload failed for The Apache HTTP Server.
lines 1-24/24 (END)

编辑2:

通过执行journalctl -xe我得到这个输出:

-- 
-- Unit apache2.service has finished reloading its configuration
-- 
-- The result is RESULT.
Aug 19 13:19:54 e-VirtualBox sudo[2933]: pam_unix(sudo:session): session closed for user root
Aug 19 13:20:46 e-VirtualBox nautilus[2130]: Attempting to read the recently used resources file a
Aug 19 13:20:50 e-VirtualBox sudo[2960]:        e : TTY=pts/1 ; PWD=/etc/apache2/sites-available ;
Aug 19 13:20:50 e-VirtualBox sudo[2960]: pam_unix(sudo:session): session opened for user root by (
Aug 19 13:20:50 e-VirtualBox systemd[1]: Reloading The Apache HTTP Server.
-- Subject: Unit apache2.service has begun reloading its configuration
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
-- 
-- Unit apache2.service has begun reloading its configuration
Aug 19 13:20:50 e-VirtualBox apachectl[2963]: AH00526: Syntax error on line 132 of /etc/apache2/si
Aug 19 13:20:50 e-VirtualBox apachectl[2963]: Too many arguments to directive
Aug 19 13:20:50 e-VirtualBox apachectl[2963]: Action 'graceful' failed.
Aug 19 13:20:50 e-VirtualBox apachectl[2963]: The Apache error log may have more information.
Aug 19 13:20:50 e-VirtualBox systemd[1]: apache2.service: Control process exited, code=exited stat
Aug 19 13:20:50 e-VirtualBox systemd[1]: Reload failed for The Apache HTTP Server.
-- Subject: Unit apache2.service has finished reloading its configuration
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
-- 
-- Unit apache2.service has finished reloading its configuration
-- 
-- The result is RESULT.
Aug 19 13:20:50 e-VirtualBox sudo[2960]: pam_unix(sudo:session): session closed for user root
lines 1835-1862/1862 (END)

答案1

您需要为标题值设置一个值,因此应该使用引号,这样空格就不会被视为不同的参数。

此外,您不应该在标题名称中包含冒号。

所以它看起来应该是这样的:

Header always set Expect-CT 'max-age=86400, enforce, report-uri="https://myownserver.com/report"'

答案2

您遇到的具体错误是:

指令参数过多

根据文档,您的标题可能应该设置为:

Header always merge Expect-CT max-age=86400
Header always merge Expect-CT enforce
Header always merge Expect-CT report-uri="https://myownserver.com/report"

相关内容