Apache 实现http协议自动转成https协议

教程发布:风哥 教程分类:ITPUX技术网 更新日期:2022-02-12 浏览学习:333

[p=28, null, left][color=rgb(80, 80, 80)][font=宋体,]一:实践环境介绍[p=28, null, left][color=rgb(80, 80, 80)][font=宋体,]二:配置Apache,实现访问http页面自动转成https页面[p=28, null, left][color=rgb(80, 80, 80)][font=宋体,] 需求1:整个站点都实现http自动转https[p=28, null, left][color=rgb(80, 80, 80)][font=宋体,] 需求2:整个站点使用http协议,只有某个单独页面从http自动转到https
[list=1]
[*]实验环境介绍

[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]使用yum 安装apache[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]Apache版本[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]# httpd -v[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]Server version: Apache/2.2.15 (Unix)[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]Server built: Aug 13 2013 17:29:28[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]使用yum 安装openssl[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]# yum install openssl[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]openssl version[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]OpenSSL 1.0.1e-fips 11 Feb 2013[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]
[p=28, null, left][font=微软雅黑, Verdana, sans-serif, 宋体]2. 配置Apache,实现访问http页面自动转成https页面[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]Apache官方rewrite使用文档:[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px][color=#505050]http://man.chinaunix.net/newsoft/ApacheMenual_CN_2.2new/rewrite/rewrite_guide.html]http://man.chinaunix.net/newsoft/ApacheMenual_CN_2.2new/rewrite/rewrite_guide.html
[color=#505050]需求[color=#505050]1:整个站点都实现http自动转https[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]1. 配置apache支持ssl协议[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]# vi /etc/httpd/conf.d/ssl.conf[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]LoadModule ssl_module modules/mod_ssl.so[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]Listen 443[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]……[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px][font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]DocumentRoot "/var/www/html/"[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]ServerName [color=#ff8373]http://www.test.com:443/]www.test.com:443 #不是必须配置的[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]SSLEngine on #为虚拟机开启ssl协议[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]SSLCertificateFile /etc/pki/tls/certs/localhost.crt #加载ssl证书文件[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]SSLCertificateKeyFile /etc/pki/tls/private/localhost.key #加载ssl私钥文件[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px][font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]2. 修改# vi /etc/httpd/conf/httpd.conf文件,将80端口自动转到443端口[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]Listen 80[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]Include conf.d/*.conf #加载conf.d目录下的配置文件[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]ServerName [color=#ff8373]http://www.test.com/]www.test.com:80 #这里是否配置不影响http转https,只是避免重启时提示[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]……[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]# add by sxr 这里是实现转发的关键,整个站点都转就把这一段写在httpd.conf中[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]RewriteEngine on[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]RewriteCond %{SERVER_PORT} 80[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]#RewriteCond %{SERVER_PORT} !^443$ #这样写也有效只是匹配方式不同而已[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]#RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [L,R] #这样的正则也有效与下面相同[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R][font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]RewriteLog /var/log/httpd/rewrite.log #增加记录转发的日志,可以不设置[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]RewriteLogLevel 10[font=宋体,]保存,重启apache生效[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]访问浏览器测试,输入192.168.123.75回车以后自动转成[url]https://192.168.123.75[color=#ff8373]http://static.oschina.net/uploads/space/2014/0624/143750_ORHB_86462.png][img=650,0]http://static.oschina.net/uploads/space/2014/0624/143750_ORHB_86462.png[/img][font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]查看rewrite.log日志,红色的表示rewrite的动作[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]# tail -f /var/log/httpd/rewrite.log[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]172.16.10.48 - - [19/Jun/2014:12:28:32 +0800] [192.168.123.75/sid#7f9ee141f860][rid#7f9ee1d18338/initial] (2) init rewrite engine with requested uri /[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]172.16.10.48 - - [19/Jun/2014:12:28:32 +0800] [192.168.123.75/sid#7f9ee141f860][rid#7f9ee1d18338/initial] (3) applying pattern '^.*$' to uri '/'[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]172.16.10.48 - - [19/Jun/2014:12:28:32 +0800] [192.168.123.75/sid#7f9ee141f860][rid#7f9ee1d18338/initial] (4) RewriteCond: input='80' pattern='80' => matched[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]172.16.10.48 - - [19/Jun/2014:12:28:32 +0800] [192.168.123.75/sid#7f9ee141f860][rid#7f9ee1d18338/initial] (2) rewrite '/' -> 'https://192.168.123.75/'[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]172.16.10.48 - - [19/Jun/2014:12:28:32 +0800] [192.168.123.75/sid#7f9ee141f860][rid#7f9ee1d18338/initial] (2) explicitly forcing redirect with [url]https://192.168.123.75/[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]172.16.10.48 - - [19/Jun/2014:12:28:32 +0800] [192.168.123.75/sid#7f9ee141f860][rid#7f9ee1d18338/initial] (1) escaping [url]https://192.168.123.75/ for redirect[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]172.16.10.48 - - [19/Jun/2014:12:28:32 +0800] [192.168.123.75/sid#7f9ee141f860][rid#7f9ee1d18338/initial] (1) redirect to [url]https://192.168.123.75/ [REDIRECT/302][font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]

[color=#505050]需求[color=#505050]2:整个站点使用http协议,只有某个单独页面从http自动转到https[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]修改# vi /etc/httpd/conf/httpd.conf文件,在文件末尾添加[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]# add by sxr[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]RewriteEngine on[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]RewriteCond %{SERVER_PORT} 80[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]RewriteRule /login.html https://%{SERVER_NAME}%{REQUEST_URI} [L,R][font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]RewriteLog /var/log/httpd/rewrite.log[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]RewriteLogLevel 10[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]保存[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]注意:如果这里配置了虚拟主机的话,要把Rewrte部分写进虚拟主机里才能有效果。[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]测试看看[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]首先,我们访问ip地址192.168.123.75,打开默认页面,不会转到https,如下图[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px][color=#ff8373]http://static.oschina.net/uploads/space/2014/0624/143809_mp5i_86462.png][img=650,0]http://static.oschina.net/uploads/space/2014/0624/143809_mp5i_86462.png[/img][font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]访问192.168.123.75/login.html,立马就转到https协议了[font=微软雅黑, Verdana, sans-serif, 宋体][size=13px]访问域名的效果与访问ip一样。

本文标签:
网站声明:本文由风哥整理发布,转载请保留此段声明,本站所有内容将不对其使用后果做任何承诺,请读者谨慎使用!
【上一篇】
【下一篇】