1. 首页
  2. 技术知识

CentOS环境下Nginx配置SSL证书实现https请求详解

目录

    一、证书申请二、配置SSL

      2.1 证书上传2.2 Server配置2.3 配置转发

    三、配置问题

      3.1 ngx_http_ssl_module3.2 ERR_SSL_PROTOCOL_ERROR

    四、配置示例

      4.1 SSL完整配置

一、证书申请

    申请SSL证书,申请之后会有两个文件提供下载(注意下载nginx版本),阿里云有免费的SSL证书申请

      xxx.keyxxx.pem

    nginx安装版本使用的是1.16.1


二、配置SSL


2.1 证书上传

    在nginx的安装目录下创建cert(别的名字也可以)将下载的SSL证书文件上传到cert下


2.2 Server配置

    进入到nginx下的conf文件夹下打开nginx.conf文件取消https server的注释

  1. # HTTPS server
  2. server {
  3.     listen       443 ssl;
  4.     server_name  localhost;
  5.     ssl_certificate      cert.pem;
  6.     ssl_certificate_key  cert.key;
  7.     ssl_session_cache    shared:SSL:1m;
  8.     ssl_session_timeout  5m;
  9.     ssl_ciphers  HIGH:!aNULL:!MD5;
  10.     ssl_prefer_server_ciphers  on;
  11.     location / {
  12.         root   html;
  13.         index  index.html index.htm;
  14.     }
  15. }

复制代码

    需要配置一下说明的内容

  1. # HTTPS server
  2. server {
  3.     # 注意这里就是443 ssl, 不要把ssl删除了
  4.     listen       443 ssl;
  5.     # 把localhost替换为SSL绑定的域名, 如www.codecoord.com
  6.     # server_name  localhost;
  7.     server_name  www.codecoord.com;
  8.     # 添加默认主目录和首页, 根据自己的路径修改
  9.     root /opt/nginx/html;
  10.     index index.html;
  11.     # cert.pem和cert.key替换为上传文件的路径(最好使用完整路径)
  12.     # ssl_certificate      cert.pem;
  13.     # ssl_certificate_key  cert.key;
  14.     ssl_certificate      /opt/nginx/cert/cert.pem;
  15.     ssl_certificate_key  /opt/nginx/cert/cert.key;
  16.     # 下面的不用动
  17.     ssl_session_cache    shared:SSL:1m;
  18.     ssl_session_timeout  5m;
  19.     ssl_ciphers  HIGH:!aNULL:!MD5;
  20.     ssl_prefer_server_ciphers  on;
  21.     location / {
  22.         root   html;
  23.         index  index.html index.htm;
  24.     }
  25. }

复制代码

    注意443端口需要在开启外网访问(比如阿里云服务器需要在控制台配置安全组, 不过默认是打开的)


2.3 配置转发

    这一步是配置对外访问端口和将http请求强制转为https删除多余配置,只需要留下以下配置

  1. server {
  2.     # 监听端口
  3.     listen       80;
  4.     # 改为自己的域名
  5.     server_name  www.codecoord.com;
  6.     # 将http请求强制转为https
  7.     # rewrite:重写指令,$host$:请求地址,$1:请求参数,permanent:永久访问
  8.     rewrite ^(.*)$ https://$host$1 permanent;
  9. }

复制代码 上述两步配置完成后测试一下是否配置正确,在sbin目录下运行测试命令

    ./nginx -t

  1. # 配置成功信息
  2. [root@TianXin sbin]# ./nginx -t
  3. nginx: the configuration file /opt/Nginx/conf/nginx.conf syntax is ok
  4. 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. [root@tianxin conf]# nginx -t
  2. nginx: [emerg] the “ssl” parameter requires ngx_http_ssl_module in /opt/nginx/conf/nginx.conf:112
  3. nginx: configuration file /opt/nginx/conf/nginx.conf test failed

复制代码

    解决方法是重新配置nginx,重新编译带上–with-http_stub_status_module –with-http_ssl_module可以重新安装nginx(建议, 可以避免很多问题)也可以不用重新安装, 不用重新安装只需要执行下面的两个命令即可

  1. # 清除编译文件
  2. make clean
  3. # 配置
  4. ./configure –prefix=/opt/nginx –with-http_stub_status_module –with-http_ssl_module
  5. # 编译
  6. make

复制代码

    不要执行make install 否则会覆盖原来的文件关闭nginx

      nginx -s stop

    拷贝目录下的objs/nginx替换之前的nginx启动文件

      cp objs/nginx /opt/nginx/sbin/

    最后启动nginx即可


3.2 ERR_SSL_PROTOCOL_ERROR

    此问题在该版本X现是因为listen配置的时候把443 后面的ssl删除了导致这个错误

  1. server {
  2.     # 注意这里就是443 ssl, 不要把ssl删除了,之前的版本
  3.     listen       443 ssl;
  4.     …
  5. }

复制代码

    解决方法就是不要把443后面的ssl漏了,注意中间有空格


四、配置示例


4.1 SSL完整配置

  1. #user  nobody;
  2. worker_processes  1;
  3. #error_log  logs/error.log;
  4. #error_log  logs/error.log  notice;
  5. #error_log  logs/error.log  info;
  6. #pid        logs/nginx.pid;
  7. events {
  8.     worker_connections  1024;
  9. }
  10. http {
  11.     include       mime.types;
  12.     default_type  APPlication/octet-stream;
  13.     sendfile        on;
  14.     keepalive_timeout  65;
  15.     server {
  16.         listen       80;
  17.         server_name  www.codecoord.com codecoord.com;
  18.         rewrite ^(.*)$ https://$host$1 permanent;
  19.     }
  20.     # https
  21.     server {
  22.         # 注意这里就是443 ssl, 不要把ssl删除
  23.         listen       443 ssl;
  24.         # 替换为SSL绑定的域名, 如www.codecoord.com
  25.         server_name  www.codecoord.com;
  26.         # 添加默认主目录和首页, 根据自己的路径修改
  27.         root /opt/nginx/html;
  28.         index index.html;
  29.         # cert.pem和cert.key替换为上传文件的路径
  30.         ssl_certificate      /opt/nginx/cert/www.codecoord.com.pem;
  31.         ssl_certificate_key  /opt/nginx/cert/www.codecoord.com.key;
  32.         # 下面的不用动
  33.         ssl_session_cache    shared:SSL:1m;
  34.         ssl_session_timeout  5m;
  35.         ssl_ciphers  HIGH:!aNULL:!MD5;
  36.         ssl_prefer_server_ciphers  on;
  37.         location / {
  38.             root   html;
  39.             index  index.html index.htm;
  40.             try_files $uri $uri/ /index.html;   # 解决vue页面刷新404问题
  41.         }
  42.     }
  43. }

复制代码 以上就是Nginx实战-配置SSL证书(CentOS环境),实现https请求的详细内容,更多关于Nginx配置SSL实现https请求的资料请关注软件技术网其它相关文章!

原创文章,作者:starterknow,如若转载,请注明出处:https://www.starterknow.com/105988.html

联系我们