0
点赞
收藏
分享

微信扫一扫

nginx 配置 https 证书完整过程

Separes 2021-09-21 阅读 65

nginx 的 ssl 模块安装

  • 查看 nginx 是否安装 http_ssl_module 模块。
$ /usr/local/nginx/sbin/nginx -V
  • 如果出现 configure arguments: --with-http_ssl_module, 则已安装(下面的步骤可以跳过,进入 nginx.conf 配置)。

  • 下载 nginx 安装包, nginx官网1.14.1稳定版本tar.gz包

# 下载安装包到 src 目录
$ cd /usr/local/src
$ wget http://nginx.org/download/nginx-1.14.1.tar.gz
  • 解压安装包。
$ tar -zxvf nginx-1.14.1.tar.gz

$ 配置 ssl 模块。

$ cd nginx-1.14.1
$ ./configure --prefix=/usr/local/nginx --with-http_ssl_module

编译安装在执行./configure步骤报错,是因为缺少环境变量
checking for C compiler … not found
./configure: error: C compiler cc is not found
解决办法

yum -y install gcc
yum -y install gcc-c++

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre= option.
解决办法

yum -y install openssl openssl-devel
  • 使用 make 命令编译(使用make install会重新安装nginx),此时当前目录会出现 objs 文件夹。
$ make
  • 用新的 nginx 文件覆盖当前的 nginx 文件。
$ cp ./objs/nginx /usr/local/nginx/sbin/
  • 再次查看安装的模块(configure arguments: --with-http_ssl_module说明ssl模块已安装)。
$ /usr/local/nginx/sbin/nginx -V

nginx version: nginx/1.14.1
...
configure arguments: --with-http_ssl_module

ssl 证书部署

  • 在 nginx 目录新建 cert 文件夹存放证书文件。
$ cd /usr/local/nginx
$ mkdir cert
  • 将这两个文件上传至服务器的 cert 目录里。
    这里使用 mac 终端上传至服务器的 scp 命令(这里需要新开一个终端,不要使用连接服务器的窗口):
$ scp /Users/yourname/Downloads/ssl.pem root@xxx.xx.xxx.xx:/usr/local/nginx/cert/
$ scp /Users/yourname/Downloads/ssl.key root@xxx.xx.xxx.xx:/usr/local/nginx/cert/

nginx.conf 配置

编辑 /usr/local/nginx/conf/nginx.conf 配置文件:

  • 配置 https server。
    注释掉之前的 http server 配置,新增 https server:
server {
    # 服务器端口使用443,开启ssl, 这里ssl就是上面安装的ssl模块
    listen       443 ssl;
    # 域名,多个以空格分开
    server_name  baidu.com www.baidu.com;
    
    # ssl证书地址
    ssl_certificate     /usr/local/nginx/cert/ssl.pem;  # pem文件的路径
    ssl_certificate_key  /usr/local/nginx/cert/ssl.key; # key文件的路径
    
    # ssl验证相关配置
    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   html;
        index  index.html index.htm;
    }
}
  • 将 http 重定向 https
server {
    listen       80;
    server_name  baidu.com www.baidu.com;
    return 301 https://$server_name$request_uri;
}
  • 重启 nginx
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  • 如果 80 端口被占用,用kill [id]来结束进程:
# 查看端口使用
$ netstat -lntp

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      21307/nginx: master 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      3072/sshd           
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      21307/nginx: master 

# 结束 80 端口进程
$ kill 21307
  • 再次重启 nginx
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

二级域名也要申请ssl证书吗

  • 单域名SSL证书
  • 多域名SSL证书
  • 通配符SSL证书
  • 多域名通配符证书
举报

相关推荐

0 条评论