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

2023-12-01 0 312
目录
  • 一、证书申请
  • 二、配置SSL
    • 2.1 证书上传
    • 2.2 Server配置
    • 2.3 配置转发
  • 三、配置问题
    • 3.1 ngx_http_ssl_module
    • 3.2 ERR_SSL_PROTOCOL_ERROR
  • 四、配置示例
    • 4.1 SSL完整配置

一、证书申请

  • 申请SSL证书,申请之后会有两个文件提供下载(注意下载nginx版本),阿里云有免费的SSL证书申请
    • xxx.key
    • xxx.pem
  • nginx安装版本使用的是1.16.1

二、配置SSL

2.1 证书上传

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

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

2.2 Server配置

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

# 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;
}
}

  • 需要配置一下说明的内容

# 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
  • 删除多余配置,只需要留下以下配置

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

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

  • ./nginx -t

# 配置成功信息
[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,使配置生效

[root@TianXin sbin]# ./nginx -s reload

  • 完整配置参考第四点配置示例
  • 配置完成后访问域名,即可显示https信息

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

三、配置问题

3.1 ngx_http_ssl_module

  • 注意如果是nginx 1.16.1之前版本, 配置内容会有有所变化,请参考别的版本配置
  • 如果运行./nginx -t时出现以下错误,标识nginx没有安装SSL模块

[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(建议, 可以避免很多问题)也可以不用重新安装, 不用重新安装只需要执行下面的两个命令即可

# 清除编译文件
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删除了导致这个错误

server {
# 注意这里就是443 ssl, 不要把ssl删除了,之前的版本
listen 443 ssl;

}

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

四、配置示例

4.1 SSL完整配置

#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请求的资料请关注悠久资源其它相关文章!

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

悠久资源 Nginx服务器 CentOS环境下Nginx配置SSL证书实现https请求详解 https://www.u-9.cn/server/nginx/7979.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务