nginx

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > nginx > Nginx配置SSL https请求

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

作者:TianXinCoord

这篇文章主要为大家介绍了Nginx实战-配置SSL证书(CentOS环境),实现https请求过程步骤,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

一、证书申请

二、配置SSL

2.1 证书上传

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

2.3 配置转发

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

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

# 配置成功信息
[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
[root@TianXin sbin]# ./nginx -s reload

三、配置问题

3.1 ngx_http_ssl_module

[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
# 清除编译文件
make clean
# 配置
./configure --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module
# 编译
make

3.2 ERR_SSL_PROTOCOL_ERROR

server {
    # 注意这里就是443 ssl, 不要把ssl删除了,之前的版本
    listen       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请求的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文