以前,我们的web网站服务访问一直是以http协议为基础进行访问的!但是这种访问协议在后来的生产实践中,被认证为不安全的访问协议!之后,互联网领域针对这种不安全的访问方式,提出了https安全访问协议的方式进行访问!
在新开发的网站中,我这边都将访问协议配置为https的安全访问协议的方式访问网站!一直没有时间整理关于这方面的实践步骤!最近,时间相对比较充裕,抽出了一部分时间,整理一下这方面的东西!
一、https和http的概念
https是安全为通道的http访问协议,是在http下加入ssl加密层!http的默认端口是80 , https的默认端口是443!
SSL 证书是一种数字证书,它使用 Secure Socket Layer 协议在浏览器和 Web 服务器之间建立一条安全通道,从而实现:
1、数据信息在客户端和服务器之间的加密传输,保证双方传递信息的安全性,不可被第三方窃听;
2、用户可以通过服务器证书验证他所访问的网站是否真实可靠。
二、获取ssl证书
在正式的生产部署环境下,推荐从第三方CA机构处购买ssl证书!目前国内的CA机构有:
1、阿里云
2、腾讯云
3、百度
内部使用,可以按照接下来的过程进行操作:
1、自行生成ssl证书
1)、生成rsa密钥
openssl genrsa -des3 -out ssltest.key 1024
2)、拷贝一个不需要密码的密钥文件
openssl rsa -in ssltest.key -out ssltest_nopass.key
3)、生成证书请求(注意这边会提示输入信息,至少输入一个,其余的回车就好,最后让输入密码时,直接回车)
openssl req -new -key ssltest.key -out ssltest.csr
4)、自己签发证书
openssl x509 -req -days 365 -in ssltest.csr -signkey ssltest.key -out ssltest.crt
三、服务器启用https
在nginx服务器配置文件nginx.conf中添加如下配置:
listen 443 ssl;
# ssl on;
ssl_certificate /etc/nginx/ssltest.crt;
ssl_certificate_key /etc/nginx/ssltest_nopass.key;
完整配置事例:
user nobody;
worker_processes 1;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
gzip_disable "MSIE [1-6].";
client_header_buffer_size 128k;
large_client_header_buffers 4 128k;
upstream blogs{
server 127.0.0.1:8080;
}
server {
listen 443 ssl;
listen 80;
server_name localhost;
#charset koi8-r;
ssl_certificate /Users/lvhong/server.crt;
ssl_certificate_key /Users/lvhong/server.key;
access_log logs/host.access.log main;
location / {
root html;
proxy_pass http://blogs;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://server_lb;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
include servers/*;
}
注意:ssl on;注释掉,是为了让网站可以在使用https访问的同时也可以使用http访问!
使用中遇到的问题:
网站引入外部http网站时访问不了,直接blank,这是由于浏览器默认不允许在https中引入http,解决方案:
将http://改成相对协议//
如:< img src="//domain.com/img/logo.png" >
或者
通过iframe内部框架
或者
通过nginx的反向代理将https://转换为http://