一、证书申请
- 申请SSL证书,申请之后会有两个文件提供下载(注意下载nginx版本),阿里云有免费的SSL证书申请
- xxx.key
- xxx.pem
- nginx安装版本使用的是1.16.1
二、配置SSL
2.1 证书上传
- 在nginx的安装目录下创建cert(别的名字也可以)
- 将下载的SSL证书文件上传到cert下
2.2 Server配置
- 进入到nginx下的conf文件夹下打开nginx.conf文件
- 取消https server的注释
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | # 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; } } |
- 需要配置一下说明的内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | # HTTPS server server { # 注意这里就是443 ssl, 不要把ssl删除了 listen 443 ssl; # 把localhost替换为SSL绑定的域名, 如www.codecoord.com # server_name localhost; server_name www.codecoord.com; # 添加默认主目录和首页, 根据自己的路径修改 root /opt/nginx/html ; index index.html; # cert.pem和cert.key替换为上传文件的路径(最好使用完整路径) # ssl_certificate cert.pem; # ssl_certificate_key cert.key; ssl_certificate /opt/nginx/cert/cert .pem; ssl_certificate_key /opt/nginx/cert/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; } } |
- 注意443端口需要在开启外网访问(比如阿里云服务器需要在控制台配置安全组, 不过默认是打开的)
2.3 配置转发
- 这一步是配置对外访问端口和将http请求强制转为https
- 删除多余配置,只需要留下以下配置
1 2 3 4 5 6 7 8 9 | server { # 监听端口 listen 80; # 改为自己的域名 server_name www.codecoord.com; # 将http请求强制转为https # rewrite:重写指令,$host$:请求地址,$1:请求参数,permanent:永久访问 rewrite ^(.*)$ https: // $host$1 permanent; } |
上述两步配置完成后测试一下是否配置正确,在sbin目录下运行测试命令
- ./nginx -t
1 2 3 4 | # 配置成功信息 [root@TianXin sbin] # ./nginx -t nginx: the configuration file /opt/Nginx/conf/nginx .conf syntax is ok nginx: configuration file /opt/Nginx/conf/nginx .conf test is successful |
- 如果测试成功则重启nginx,使配置生效
1 | [root@TianXin sbin] # ./nginx -s reload |
- 完整配置参考第四点配置示例
- 配置完成后访问域名,即可显示https信息
三、配置问题
3.1 ngx_http_ssl_module
- 注意如果是nginx 1.16.1之前版本, 配置内容会有有所变化,请参考别的版本配置
- 如果运行./nginx -t时出现以下错误,标识nginx没有安装SSL模块
1 2 3 | [root@tianxin conf] # nginx -t nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /opt/nginx/conf/nginx .conf:112 nginx: configuration file /opt/nginx/conf/nginx .conf test failed |
- 解决方法是重新配置nginx,重新编译带上–with-http_stub_status_module –with-http_ssl_module
- 可以重新安装nginx(建议, 可以避免很多问题)也可以不用重新安装, 不用重新安装只需要执行下面的两个命令即可
1 2 3 4 5 6 | # 清除编译文件 make clean # 配置 . /configure --prefix= /opt/nginx --with-http_stub_status_module --with-http_ssl_module # 编译 make |
- 不要执行make install 否则会覆盖原来的文件
- 关闭nginx
- nginx -s stop
- 拷贝目录下的objs/nginx替换之前的nginx启动文件
- cp objs/nginx /opt/nginx/sbin/
- 最后启动nginx即可
3.2 ERR_SSL_PROTOCOL_ERROR
- 此问题在该版本中出现是因为listen配置的时候把443 后面的ssl删除了导致这个错误
1 2 3 4 5 | server { # 注意这里就是443 ssl, 不要把ssl删除了,之前的版本 listen 443 ssl; ... } |
- 解决方法就是不要把443后面的ssl漏了,注意中间有空格
四、配置示例
4.1 SSL完整配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #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 ; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.codecoord.com codecoord.com; rewrite ^(.*)$ https: // $host$1 permanent; } # https server { # 注意这里就是443 ssl, 不要把ssl删除 listen 443 ssl; # 替换为SSL绑定的域名, 如www.codecoord.com server_name www.codecoord.com; # 添加默认主目录和首页, 根据自己的路径修改 root /opt/nginx/html ; index index.html; # cert.pem和cert.key替换为上传文件的路径 ssl_certificate /opt/nginx/cert/www .codecoord.com.pem; ssl_certificate_key /opt/nginx/cert/www .codecoord.com.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; try_files $uri $uri/ /index .html; # 解决vue页面刷新404问题 } } } |
以上就是Nginx实战-配置SSL证书(CentOS环境),实现https请求的详细内容,更多关于Nginx配置SSL实现https请求的资料请关注IT俱乐部其它相关文章!