一、http与https的区别
http:是互联网上应用最为广泛的一种网络协议,是一个客户端和端请求和应答的标准(tcp),用于从www服务器传输超文本到本地浏览器的传输协议,它可以使浏览器更加高效,使网络传输减少。
https:是以安全为目标的http通道,简单讲是http的安全版,即http下加入ssl层,https的安全基础是ssl,因此加密的详细内容就需要ssl。https协议的主要作用可以分为两种:一种是建立一个信息安全通道,来保证数据传输的安全;另一种就是确认网站的真实性。
https和http的区别主要如下:
1、https协议需要到ca申请证书,一般免费证书较少,因而需要一定费用。
2、http是超文本传输协议,信息是明文传输,https则是具有安全性的ssl加密传输协议。
3、http和https使用的是完全不同的连接方式,用的端口也不一样,前者是80,后者是443。
4、http的连接很简单,是无状态的;https协议是由ssl http协议构建的可进行加密传输、身份认证的网络协议,比http协议安全。
二、使用openssl生成证书
openssl是目前最流行的ssl密码库工具,其提供了一个通用、健壮、功能完备的工具套件,用以支持ssl/tls协议的实现。
比如生成到:/usr/local/ssl
openssl req -x509 -nodes -days 36500 -newkey rsa:2048 -keyout /usr/local/ssl/nginx.key -out /usr/local/ssl/nginx.crt
生成过程:
# openssl req -x509 -nodes -days 36500 -newkey rsa:2048 -keyout /u sr/local/ssl/nginx.key -out /usr/local/ssl/nginx.crt generating a 2048 bit rsa private key ............................................................................... ............... writing new private key to '/usr/local/ssl/nginx.key' ----- 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]:xxxx organizational unit name (eg, section) []:xxxx common name (eg, your name or your server's hostname) []:xxxx(一般是域名) email address []:xxxx@xxxx.com # ll total 8 -rw-r--r--. 1 root root 1391 apr 21 13:29 nginx.crt -rw-r--r--. 1 root root 1704 apr 21 13:29 nginx.key
三、nginx安装http_ssl_module模块
nginx如果未开启ssl模块,配置https时提示错误。
nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:xxx
nginx缺少http_ssl_module模块,编译安装的时候带上--with-http_ssl_module配置就行了。
本场景是服务器已经安装过nginx,但是未安装http_ssl_module。
1.进入到源码包,如:
cd /app/download/nginx-1.12.2
2.configure:
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module #可能需要的依赖包 yum -y install pcre-devel openssl openssl-devel
3.make:
make
4.不需要执行make install,否则就覆盖安装了。
5.备份原有的nginx,如:
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_bak
6.然后将刚刚编译好的nginx覆盖掉原有的nginx(nginx需要停止)
cp ./objs/nginx /usr/local/nginx/sbin/
7.查看安装情况:
/usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.12.2 built by gcc 4.8.5 20150623 (red hat 4.8.5-16) (gcc) built with openssl 1.0.2k-fips 26 jan 2017 tls sni support enabled configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
四、nginx配置https
贴部分配置信息:
server { listen 80; server_name www.yourdomain.com; rewrite ^(.*) https://$server_name$1 permanent; #http 跳转 https }
server { listen 443 ssl; server_name www.yourdomain.com; ssl_certificate /usr/local/ssl/nginx.crt; ssl_certificate_key /usr/local/ssl/nginx.key; ssl_session_cache shared:ssl:1m; ssl_session_timeout 5m; #禁止在header中出现服务器版本,防止黑客利用版本漏洞攻击 server_tokens off; #如果是全站 https 并且不考虑 http 的话,可以加入 hsts 告诉你的浏览器本网站全站加密,并且强制用 https 访问 fastcgi_param https on; fastcgi_param http_scheme https; access_log /usr/local/nginx/logs/httpsaccess.log; }
先检验配置的对不对:
/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
重启nginx:
/usr/local/nginx/sbin/nginx -s reload
访问: