httpd服务自定义日志文件、配置https访问以及强制https跳转

1、建立httpd服务,要求:
(1) 提供两个基于名称的虚拟主机:
www1.stuX.com,页面文件目录为/web/vhosts/www1;错误日志为/var/log/httpd/www1/error_log,访问日志为/var/log/httpd/www1/access_log;
www2.stuX.com,页面文件目录为/web/vhosts/www2;错误日志为/var/log/httpd/www2/error_log,访问日志为/var/log/httpd/www2/access_log;
(2) 通过www1.stuX.com/server-status输出其状态信息,且要求只允许提供账号的用户访问;
(3) www1不允许192.168.0.88主机访问;

2、为上面的第2个虚拟主机提供https服务,使得用户可以通过https安全的访问此web站点;
(1) 要求使用证书认证,证书中要求使用国家(CN),州(Beijing),城市(Beijing),组织为(MageEdu);
(2) 设置部门为Ops, 主机名为www2.stuX.com

3、为https访问配置强制跳转,访问http://www2.stuX.com会跳转到https://www2.stuX.com上面去。

在Centos 7 基于httpd-2.4实现

在进行配置前,首先安装httpd服务及mod_ssl:

[root@localhost ~]# yum install -y mod_ssl httpd
1、建立httpd服务

首先创建页面文件目录及日志文件目录:

[root@localhost ~]# mkdir -pv /web/vhosts/www1  #创建www1web目录
mkdir: created directory ‘/web’
mkdir: created directory ‘/web/vhosts’
mkdir: created directory ‘/web/vhosts/www1’
[root@localhost ~]# mkdir /var/log/httpd/www1  #创建www1 log目录
  
[root@localhost ~]# mkdir -pv /web/vhosts/www2  #创建www2 web目录
mkdir: created directory ‘/web/vhosts/www2’
[root@localhost ~]# mkdir /var/log/httpd/www2  ##创建www2 log目录

[root@localhost ~]# chcon -R --reference /var/www/ /web/  #设置安全上下文

随后编辑配置配置文件:

[root@localhost ~]# vim /etc/httpd/conf.d/vhosts.conf
LoadModule  status_module  modules/mod_status.so  #加载status模块
<virtualhost *:80>    #定义基于域名www1.stuX.com的虚拟主机
        ServerName      www1.stuX.com 
        Documentroot /web/vhosts/www1
        CustomLog "/var/log/httpd/www1/access_log" combined  #定义access_log
        ErrorLog "/var/log/httpd/www1/error_log"  #定义error_log
        <Directory "/web/vhosts/www1">
                Options none
                AllowOverride none
                <RequireAll>
                        Require all granted
                        Require not ip 192.168.0.88  #禁止192.168.0.88访问www1目录
                </RequireAll>
        </Directory>
        <Location /server-status>  #配置server-status页面
                SetHandler server-status  #启动服务器的status信息
                Options none
                AllowOverride none
                AuthType basic
                AuthName "welcome to www1.stuX.com" 
                AuthUserFile "/web/vhosts/www1passwd"
                Require user charlie wch  #限制只允许指定的账号认证访问
        </Location>
</virtualhost>

<virtualhost *:80>  #定义基于域名www2.stuX.com的虚拟主机
        ServerName www2.stuX.com
        Documentroot /web/vhosts/www2
        CustomLog "/var/log/httpd/www2/access_log" combined
        ErrorLog "/var/log/httpd/www2/error_log"
        <Directory "/web/vhosts/www2">
                Options none
                AllowOverride   none
                Require all granted
        </Directory>
</virtualhost>

之后配置用户认证文件:

[root@localhost ~]# htpasswd -cb /web/vhosts/www1passwd charlie 123456
Adding password for user charlie
[root@localhost ~]# htpasswd -b /web/vhosts/www1passwd wch magedu
Adding password for user wch

使用httpd -t检查配置,如无报错后启动服务:

[root@localhost ~]# httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
Syntax OK

测试认证访问:
我这边用windows测试,本地Ip为192.168.0.38,修改保存C:\Windows\System32\drivers\etc\hosts文件:

127.0.0.1 localhost
127.0.0.1 steamcommunity.com
192.168.0.109 www1.stuX.com
192.168.0.109 www2.stuX.com

然后测试访问:


能正常访问www1目录

IP192.168.0.88无法访问www1目录

访问server-status页面需要账号认证

用指定的用户账号完成认证后能正常访问

虚拟主机www2也能正常访问

查看相应的日志文件:

[root@localhost ~]# tail -5 /var/log/httpd/www1/access_log 
192.168.0.88 - - [01/May/2018:19:18:00 +0800] "GET /noindex/css/fonts/Light/OpenSans-Light.ttf HTTP/1.1" 403 244 "http://www1.stux.com/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0"
192.168.0.88 - - [01/May/2018:19:18:06 +0800] "GET /server-status HTTP/1.1" 401 381 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0"
192.168.0.88 - - [01/May/2018:19:18:06 +0800] "GET /server-status HTTP/1.1" 401 381 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0"
192.168.0.88 - charlie [01/May/2018:19:18:40 +0800] "GET /server-status HTTP/1.1" 200 4315 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0"
192.168.0.88 - - [01/May/2018:19:20:06 +0800] "GET / HTTP/1.1" 403 4897 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0"
[root@localhost ~]# tail -5 /var/log/httpd/www1/error_log
[Tue May 01 19:22:51.202586 2018] [auth_basic:error] [pid 11446] [client 192.168.0.88:50872] AH01618: user asdasda not found: /server-status
[Tue May 01 19:22:51.445776 2018] [auth_basic:error] [pid 11446] [client 192.168.0.88:50872] AH01618: user  not found: /server-status
[Tue May 01 19:22:52.552326 2018] [auth_basic:error] [pid 11446] [client 192.168.0.88:50872] AH01618: user asdasda not found: /server-status
[Tue May 01 19:22:53.682249 2018] [auth_basic:error] [pid 11446] [client 192.168.0.88:50872] AH01618: user adasd not found: /server-status
[Tue May 01 19:22:55.105525 2018] [authz_core:error] [pid 11446] [client 192.168.0.88:50872] AH01630: client denied by server configuration: /web/vhosts/www1/favicon.ico, referer: http://www1.stux.com/server-status

[root@localhost ~]# tail -5 /var/log/httpd/www2/access_log 
192.168.0.38 - - [01/May/2018:18:54:40 +0800] "GET / HTTP/1.1" 200 13 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0"
192.168.0.88 - - [01/May/2018:19:20:13 +0800] "GET / HTTP/1.1" 200 13 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0"
[root@localhost ~]# tail -5 /var/log/httpd/www2/error_log
空

相关日志log均能正常记录访问。

2、为第2个虚拟主机提供https服务,使得用户可以通过https安全的访问此web站点

首先创建CA服务器,用于签发证书:

[root@localhost ~]# cd /etc/pki/CA/private/
[root@localhost private]# (umask 077;openssl genrsa -out CA.key 1024)  #生成CA的私钥
Generating RSA private key, 1024 bit long modulus
..............................................++++++
....................++++++
e is 65537 (0x10001)
[root@localhost private]# ll
total 4
-rw-------. 1 root root 887 May  1 19:57 CA.key
[root@localhost private]# cd ../certs/
[root@localhost certs]# openssl req -new -x509 -key /etc/pki/CA/private/CA.key  -out CA.crt -days 365  #生成CA的自签证书
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:Beijing
Locality Name (eg, city) [Default City]:Beijing
Organization Name (eg, company) [Default Company Ltd]:MageEdu
Organizational Unit Name (eg, section) []:Ops
Common Name (eg, your name or your server's hostname) []:ca     
Email Address []:
root@localhost certs]# cd 
[root@localhost ~]# touch /etc/pki/CA/{serial,index.txt}  #生成serial,index.txt文件
[root@localhost ~]# echo 00 > /etc/pki/CA/serial   #输入序列号

随后生成签发服务器证书:

[root@localhost ~]# mkdir /etc/httpd/ssl  #创建httpd的ssl目录
[root@localhost ~]# cd /etc/httpd/ssl
[root@localhost ssl]# (umask 077;openssl genrsa -out httpd-ssl.key 1024)  #生成httpd-ssl的私钥
Generating RSA private key, 1024 bit long modulus
....................++++++
.....++++++
e is 65537 (0x10001)
[root@localhost ssl]# openssl req -new -key /etc/httpd/ssl/httpd-ssl.key -out httpd-ssl.csr -days 365  #生成httpd-ssl证书签发请求
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:Beijing
Locality Name (eg, city) [Default City]:Beijing
Organization Name (eg, company) [Default Company Ltd]:MageEdu
Organizational Unit Name (eg, section) []:Ops
Common Name (eg, your name or your server's hostname) []:www2.stuX.com
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
root@localhost ssl]# openssl ca -in httpd-ssl.csr -out httpd-ssl.crt -days 365 -cert /etc/pki/CA/certs/CA.crt -keyfile /etc/pki/CA/private/CA.key   #签发httpd-ssl证书
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 0 (0x0)
        Validity
            Not Before: May  1 12:33:28 2018 GMT
            Not After : May  1 12:33:28 2019 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = Beijing
            organizationName          = MageEdu
            organizationalUnitName    = Ops
            commonName                = www2.stuX.com
        X509v3 extensions:
            X509v3 Basic Constraints: 
                CA:FALSE
            Netscape Comment: 
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier: 
                E2:DC:0A:C4:72:EE:DC:9E:57:4A:F8:38:49:DA:B1:DF:24:24:73:3D
            X509v3 Authority Key Identifier: 
                keyid:E7:5E:74:26:B2:A4:C6:C7:67:7A:BB:8B:8B:DF:E8:C4:AF:39:03:B0

Certificate is to be certified until May  1 12:33:28 2019 GMT (365 days)
Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

之后编辑/etc/httpd/conf.d/ssl.conf文件:

Listen 443 https  #确保有此项配置
SSLCertificateFile /etc/httpd/ssl/httpd-ssl.crt  #修改为刚生成的httpd-ssl证书
SSLCertificateKeyFile /etc/httpd/ssl/httpd-ssl.key  #修改为刚生成的httpd-ssl私钥

编辑/etc/httpd/conf.d/vhost.conf文件:

<virtualhost *:80>  #配置80端口的虚拟主机
        ServerName www2.stuX.com
        Documentroot /web/vhosts/www2
        CustomLog "/var/log/httpd/www2/access_log" combined
        ErrorLog "/var/log/httpd/www2/error_log"
        <Directory "/web/vhosts/www2">
                Options none
                AllowOverride   none
                Require all granted
        </Directory>
</virtualhost>

<virtualhost *:443>  #新增虚拟主机的监听端口为443
        ServerName www2.stuX.com
        Documentroot /web/vhosts/www2
        CustomLog "/var/log/httpd/www2/access_log" combined
        ErrorLog "/var/log/httpd/www2/error_log"
        <Directory "/web/vhosts/www2">
                Options none
                AllowOverride   none
                Require all granted
        </Directory>
</virtualhost>

重启httpd服务,后测试访问:

能正常访问https页面

此时访问http://www2.stuX.com页面,不会跳转到https页面访问:

http页面也能正常访问

3、配置https强制跳转

首先确认配置文件是否加载了mod_rewrite,httpd-2.4 module配置文件在/etc/httpd/conf.modules.d/00-base.conf中:

[root@localhost ~]# vim /etc/httpd/conf.modules.d/00-base.conf
LoadModule rewrite_module modules/mod_rewrite.so  #如若没有指定的mod加载语句,可自行添加

随后编辑www2的虚拟主机配置:

[root@localhost ~]# vim /etc/httpd/conf.d/vhosts.conf
<virtualhost *:80>
        ServerName www2.stuX.com
        Documentroot /web/vhosts/www2
        CustomLog "/var/log/httpd/www2/access_log" combined
        ErrorLog "/var/log/httpd/www2/error_log"
        RewriteEngine on  #启动Rewrite引擎
        RewriteCond %{SERVER_PORT} 80  #定义URL匹配条件,此处匹配端口80
        RewriteRule ^(/test.*)$ https://%{HTTP_HOST}$1 [R,L]  #定义Rewrite复写规则,此处将带有test的URL路径重写为https://www2.stuX.com/test.html
        <Directory "/web/vhosts/www2">
                Options none
                AllowOverride   none
                Require all granted
        </Directory>
</virtualhost>

保存后重启httpd服务,访问相应的页面测试:


访问www2.stuX.com/test.html时会跳转到https访问

此时访问www2.stuX.com的其他路径不会跳转到https访问页面,如index.html。

访问index.html的不会跳转到https

Rewrite的模块使用比较复杂,此处我也是刚接触有兴趣的同学可以参考下面的链接进行学习:
配置https服务:https://blog.csdn.net/wlzx120/article/details/52597338
配置https强制跳转:https://www.centos.bz/2018/01/apache-%E5%BC%BA%E5%88%B6-http-%E5%85%A8%E9%83%A8%E8%B7%B3%E8%BD%AC%E5%88%B0-https/
Rewrite模块:http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
Rewrite模块中文手册:http://man.chinaunix.net/newsoft/Apache2.2_chinese_manual/mod/mod_rewrite.html#rewriterule
RewriteRule和RewriteCond规则参数的详细介绍:https://blog.csdn.net/lijunwyf/article/details/54948463

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容