大网数据广告活动
服务器运维问答

Set "Secure Flag" on Cookies for Only One (of many) Virtual Host on Apache

· 52CE 我爱测

I'm hosting a number of sites on a single VPS (Debian Jessie, Apache 2.4). One of these sites forces HTTPS. On this and only this site, I would like to set the "Secure Flag" for cookies. I've found loads of resources explaining h…

问题原文

原帖链接 · kittykittybangbang · CC BY-SA 3.0

I'm hosting a number of sites on a single VPS (Debian Jessie, Apache 2.4). One of these sites forces HTTPS. On this and only this site, I would like to set the "Secure Flag" for cookies. I've found loads of resources explaining how to do this for all sites hosted on a server via the apache2.conf file, like this:

LoadModule headers_module /usr/lib/apache2/modules/mod_headers.so
Header edit Set-Cookie ^(.*)$ $1;HttpOnly;Secure

But I want Apache to apply this header rewrite only to the one HTTPS site. How do I do that?

已采纳解答

原帖链接 · kittykittybangbang · CC BY-SA 3.0

Thanks to @JayMcTee's comments, I was able to stumble upon the answer.

To apply the settings to one specific virtual host, simply add the same lines you would to your apache2.conf file:

LoadModule headers_module /usr/lib/apache2/modules/mod_headers.so
Header edit Set-Cookie ^(.*)$ $1;HttpOnly;Secure

...to within your virtual host block. For example:

<IfModule mod_ssl.c>
<VirtualHost *:443>
  ServerAdmin [email omitted]
  ServerName  domain.com
  ServerAlias www.domain.com

  DirectoryIndex index.html index.php
  DocumentRoot /var/www/domain.com/public_html

  ...

  LoadModule headers_module /usr/lib/apache2/modules/mod_headers.so
  Header edit Set-Cookie ^(.*)$ $1;HttpOnly;Secure

</VirtualHost>
</IfModule>

Then restart Apache (service apache2 restart).

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-06-06)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。