Nginx(8):nginx实现https

1、nginx源码安装时,需要将--with-http_ssl_module --with-http_stab_status_module模块开启

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stab_status_module

make && make install

注:如果nginx已安装,可通过新的解压源码包重新编译,再将/root/nginx-1.14.1/objs/nginx拷贝到已安装目录/usr/local/nginx/sbin/ (可备份原有nginx文件),拷贝前需stop已安装nginx服务

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stab_status_module

make     #不需要继续make install

mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

cp /root/nginx-1.14.1/objs/nginx /usr/local/nginx/sbin/

2、对加密的server标签添加SSL

server {
  .......;
  ssl on;
  ssl_certificate /usr/local/nginx/conf/ssl/atguigu.crt;
  sl_certificate_key /usr/local/nginx/conf/ssl/atguigu.key;
  ssl_session_timeout 5m;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  ssl_ciphers "EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES2 56:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5
}

3、生成密钥和证书文件

# openssl genrsa -out atguigu.key 1024
建立服务器私钥,生成 RSA 密钥 

# openssl req -new -key atguigu.key -out atguigu.csr  
需要依次输入国家,地区,组织,email。最重要的是有一个common name,可以写你的名字或者域 名。如果为了 https 申请,
这个必须和域名吻合,否则会引发浏览器警报。生成的 csr 文件交给 CA 签 名后形成服务端自己的证书 

# openssl x509 -req -days 365 -sha256 -in atguigu.csr -signkey atguigu.key -out atguigu.crt  
生成签字证书 

# cp atguigu.crt /usr/local/nginx/conf/ssl/atguigu.crt 
# cp atguigu.key /usr/local/nginx/conf/ssl/atguigu.key 
将私钥和证书复制到指定位置

4、设置http自动跳转https

修改原有server标签监听端口

server {
  ..........;
  listen 443;
}

新增server标签

server{
  listen 80;
  server_name www.test1.com;
  rewrite ^(.*)$ https://www.test1.com permanent;
  root html;
  index index.html;
}

重启nginx,测试

猜你喜欢

转载自blog.csdn.net/qq_40836555/article/details/105879248