SSL 证书是一种数字证书,它使用 Secure Socket Layer 协议在浏览器和 Web 服务器之间创建一条安全通道,从而实现:
一、数据信息在客户端和服务器之间的加密传输,保证双方传递信息的安全性,不可被第三方窃听;
二、用户能够经过服务器证书验证他所访问的网站是否真实可靠。php
要设置安全服务器,使用公共钥建立一对公私钥对。大多数状况下,发送证书请求(包括本身的公钥),你的公司证实材料以及费用到一个证书颁发机构(CA).CA验证证书请求及您的身份,而后将证书返回给您的安全服务器。html
可是内网实现一个服务器端和客户端传输内容的加密,能够本身给本身颁发证书,只须要忽略掉浏览器不信任的警报便可!nginx
由CA签署的证书为您的服务器提供两个重要的功能:web
生成SSL证书apache
# 生成一个RSA密钥
$ openssl genrsa -des3 -out 33iq.key 1024
# 拷贝一个不须要输入密码的密钥文件
$ openssl rsa -in 33iq.key -out 33iq_nopass.key
# 生成一个证书请求
$ openssl req -new -key 33iq.key -out 33iq.csr
# 本身签发证书
$ openssl x509 -req -days 365 -in 33iq.csr -signkey 33iq.key -out 33iq.crt
第3个命令是生成证书请求,会提示输入省份、城市、域名信息等,重要的是,email必定要是你的域名后缀的。这样就有一个 csr 文件了,提交给 ssl 提供商的时候就是这个 csr 文件。固然我这里并无向证书提供商申请,而是在第4步本身签发了证书。浏览器
HTTPS 是以安全为目标的 HTTP 通道,即 HTTP 下加入 SSL 加密层。HTTPS 不一样于 HTTP 的端口,HTTP默认端口为80,HTTPS默认端口为443。安全
nginx配置,/etc/nginx/nginx.conf 加入:bash
server {
listen 80; server_name kwdst.welcom.cn; rewrite ^ https://$http_host$request_uri? permanent; } server { listen 443 ssl; server_name kwdst.welcom.cn ssl on; ssl_certificate /etc/nginx/cert/2ceo.pem; //路径 ssl_certificate_key /etc/nginx/cert/2ceo.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; location / { root /var/www/static; add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; } }
Apache 可直接在apache2.conf中加入也能够在/etc/apache/sites-enabled/建立 .conf后缀的配置文件.服务器
<VirtualHost *:80>
ServerName kwdbiz.welcom.cn
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/carku/public ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined RewriteEngine on RewriteCond %{HTTPS} !=on RewriteRule ^(.*)?$ https://%{SERVER_NAME}$1 [L,R]</VirtualHost> <VirtualHost *:443> ServerName kwdbiz.welcom.cn ServerAdmin webmaster@localhost DocumentRoot /var/www/html/carku/public ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined SSLEngine on SSLCertificateFile /etc/apache2/cert/2ceo/2ceo.pem SSLCertificateKeyFile /etc/apache2/cert/2ceo/2ceo.key <FilesMatch "\.(cgi|shtml|phtml|php)$"> SSLOptions +StdEnvVars </FilesMatch> <Directory /usr/lib/cgi-bin> SSLOptions +StdEnvVars </Directory> </VirtualHost>
重启apache/nginx便可访问HTTPS 网站.session
使用HTTPS协议安全但对服务器来讲是很大的负载开销,因此通常对一些安全有要求的操做使用HTTPS安全加密,如:交易,用户密码相关等.
在https server下加入以下配置:
if ($uri !~* "/login.php$") { rewrite ^/(.*)$ http://$host/$1 redirect; }
在http server下加入以下配置:
if ($uri ~* "/login.php$") { rewrite ^/(.*)$ https://$host/$1 redirect; }
这样一来,用户会且只会在访问login.php的状况下,才会经过https访问。