Apache配置Https

Httpd配置https

事前说明,我的Httpd版本是:Server version: Apache/2.4.6 (CentOS)

首先先准备https证书文件,把他们传递到apache服务器的/etc/httpd/ssl文件夹里,然后安装yum install -y mod_ssl openssl,安装完毕之后,发现/etc/httpd/conf.d文件夹下多了一个ssl.conf,出于安全先备份一份,然后修改ssl.conf的如下几个地方:

1
2
3
4
5
未涉及的字段保留原样
DocumentRoot "/var/www/html" #网站的目录
ServerName 自己的域名
SSLCertificateFile /etc/httpd/ssl/imoulife.crt #秘钥crt文件及路径
SSLCertificateKeyFile /etc/httpd/ssl/imoulife.key #秘钥key文件及路径

保存退出,重启httpd即可生效。注意!因为一个ip只能绑一个SSL,因此这里就算在写了两份<VirtualHost *:443>...</VirtualHost>,也还是会读取第一个SSL。

Httpd配置http跳转https

如果想要达到http跳转https的话,还是在ssl文件里的最下面追加这段内容:

1
2
3
4
5
<VirtualHost  *:80>
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/?(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
</VirtualHost>

如果只是单url跳转,比如http://test.imoulife.com/login跳转到https://test.imoulife.com/login,其他的域名依旧是http。那么就把最后一句改成:RewriteRule ^/logon.do$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R],重启httpd就生效。

httpd配置ip白名单

设置了https不能说很安全,我们还需要设置IP白名单才能让WEB界面更加放心。由于我这个httpd主要是给zabbix使用的,所以就拿访问zabbix的IP白名单为例。

首先打开/etc/httpd/conf.d/zabbix.conf,修改如下地方:

1
2
3
4
5
6
7
8
9
10
11
Alias /zabbix /usr/share/zabbix

<Directory "/usr/share/zabbix">
Options FollowSymLinks
AllowOverride None
#Require all granted #这句话是任何人都可以访问的意思
<RequireAll>
Require ip 192.168.1 #准许192.168.1开头的IP地址的访问
Require ip 192.168.1.104 192.168.1.205 #准许固定IP地址访问
Require ip 10.1.0.0/16 #网络/子网掩码的访问
</RequireAll>

保存退出,重启httpd即可。

Gitlab配置Https

我的gitlab是容器做的,其实无论容器还是非容器其实配置都是一样的。

首先先开放443端口给相应的IP,然后进入容器,在/etc/gitlab/下先创建一个ssl文件夹,里面放入https证书,如图:
akb48

放好证书文件之后,返回上一级目录,修改一下gitlab.rb文件:

1
2
3
4
5
external_url 'https的域名'
nginx['redirect_http_to_https'] = true
nginx['redirect_http_to_https_port'] = 80
nginx['ssl_certificate'] = "上面https证书的路径/crt文件名称"
nginx['ssl_certificate_key'] = "上面https证书的路径/key文件名称"

然后执行gitlab-ctl reconfigure更新配置,完事之后找到nginx的gitlab配置文件gitlab-http.conf,发现由于更新了配置,所以里面已经生成好了一份新的配置文件,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server {
listen *:443 ssl http2;
server_name https的域名;
server_tokens off;
client_max_body_size 0;
ssl on;
ssl_certificate 上面https证书的路径/crt文件名称;
ssl_certificate_key 上面https证书的路径/key文件名称;
............................. #剩余的信息省略了
}

server{
listen*:80;
server_name https的域名;
rewrite^(.*)$https://$host$1permanent;
}

确认各个信息无误之后,退出执行gitlab-ctl restart即可。

参考资料

http://tonylit.me/2016/02/29/apache_http%E8%B7%B3%E8%BD%AC/
http://zhizhi.tangliangdong.me/2017/10/12/2017-10-12-http-to-https/
https://blog.mallux.me/2017/02/27/gitlab/
https://blog.csdn.net/leshami/article/details/78521031

感谢您请我喝咖啡~O(∩_∩)O,如果要联系请直接发我邮箱chenx1242@163.com,我会回复你的
-------------本文结束感谢您的阅读-------------