部分内容转载自:https://www.cnblogs.com/jingxiaoniu/p/6745254.html
一、HTTPS简介
1.https简介HTTPS其实是有两部分组成:HTTP + SSL / TLS,也就是在HTTP上又加了一层处理加密信息的模块。服务端和客户端的信息传输都会通过TLS进行加密,所以传输的数据都是加密后的数据2.https协议原理首先,客户端与服务器建立连接,各自生成私钥和公钥,是不同的。服务器返给客户端一个公钥,然后客户端拿着这个公钥把要搜索的东西加密,称之为密文,并连并自己的公钥一起返回给服务器,服务器拿着自己的私钥解密密文,然后把响应到的数据用客户端的公钥加密,返回给客户端,客户端拿着自己的私钥解密密文,把数据呈现出来
二、证书和私钥的生成
注意:一般生成的目录,应该放在nginx/conf/ssl目录1.创建服务器证书密钥文件 server.key:openssl genrsa -des3 -out server.key 1024输入密码,确认密码,自己随便定义,但是要记住,后面会用到。2.创建服务器证书的申请文件 server.csropenssl req -new -key server.key -out server.csr输出内容为:
Enter pass phrase for root.key: ← 输入前面创建的密码
Country Name (2 letter code) [AU]:CN ← 国家代号,中国输入CN
State or Province Name (full name) [Some-State]:BeiJing ← 省的全名,拼音
Locality Name (eg, city) []:BeiJing ← 市的全名,拼音
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名
Organizational Unit Name (eg, section) []: ← 可以不输入
Common Name (eg, YOUR name) []: ← 此时不输入
Email Address []:admin@mycompany.com ← 电子邮箱,可随意填
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []: ← 可以不输入
An optional company name []: ← 可以不输入4.备份一份服务器密钥文件cp server.key server.key.org5.去除文件口令openssl rsa -in server.key.org -out server.key6.生成证书文件server.crtopenssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
三、配置文件
1.下面为配置文件 /usr/local/nginx/conf/vhost/daj.conf
server{
#比起默认的80 使用了443 默认 是ssl方式 多出default之后的ssl
listen 443 default ssl;
#default 可省略
#开启 如果把ssl on;这行去掉,ssl写在443端口后面。这样http和https的链接都可以用
ssl on;
#证书(公钥.发送到客户端的)
ssl_certificate ssl/server.crt;
#私钥,
ssl_certificate_key ssl/server.key;
#下面是绑定域名
server_name www.daj.com;
location / {
#禁止跳转
proxy_redirect off;
#代理淘宝
proxy_pass https://www.tao.com/;
}
}
四、开启nginx的ssl模块
1.the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:37原因是nginx缺少http_ssl_module模块,编译安装时带上--with-http_ssl_module配置就可以了2.如果已经安装过nginx,想要添加模块看下面1)切换到nginx源码包cd /usr/local/src/nginx-1.11.32)查看ngixn原有的模块/usr/local/nginx/sbin/nginx -V3)重新配置./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module4)重新编译,不需要make install安装。否则会覆盖make5)备份原有已经安装好的nginxcp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak6)将刚刚编译好的nginx覆盖掉原来的nginx(ngixn必须停止)cp ./objs/nginx /usr/local/nginx/sbin/这时,会提示是否覆盖,请输入yes,直接回车默认不覆盖7)启动nginx,查看nginx模块,发现已经添加/usr/local/nginx/sbin/nginx -V
nginx 配置:
nginx.conf 配置:
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;client_max_body_size 10m;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;server {listen 80;server_name 172.16.21.123;location / {rewrite ^(.*)$ https://172.16.21.123:433$1 permanent; } # rewrite ^(.*)$ https://$host$1 permanent; }server {# listen 80;listen 433;ssl on;ssl_certificate ssl/server.crt;ssl_certificate_key ssl/server.key;ssl_session_timeout 5m;ssl_protocols TLSv1 TLSv1.1 TLSv1.2;ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;ssl_prefer_server_ciphers on;server_name 172.16.21.123; # rewrite ^(.*)$ https://172.16.21.123$1 permanent; #charset koi8-r;#access_log logs/host.access.log main;location / {root /usr/local/projects/pages/dist;index index.html index.htm;}location ^~/api/ {proxy_pass http://172.16.21.161:8899/; proxy_redirect off;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}#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://127.0.0.1; #}# 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;# }#}}