Nginx配置SSL证书(CentOS环境),实现https请求

目录

一、Nginx配置SSL

1.证书申请

二、配置SSL

2.1 证书上传

2.2 HTTPS server配置

2.2.3 配置转发

三、配置问题

四、配置示例

1.nginx.conf配置SSL示例


一、Nginx配置SSL

1.证书申请

1. 向自己所在云服务提供商申请SSL,例如阿里云提供免费的SSL申请,配置SSL必须要有自己的域名

2. 本篇文章主要讲Nginx配置SSL证书,nginx怎么安装和证书怎么申请百度一下,这里不做过多讲解

3.申请SSL证书,通过之后下载证书(注意下载nginx版本),下载后会有以下两个证书文件:xxx.key、xxx.pem

4. 注意:nginx安装版本使用的是1.16.1,别的版本不能保证一定成功

下载地址:http://nginx.org/download/nginx-1.16.1.tar.gz

二、配置SSL

2.1 证书上传

1. 在nginx的安装目录下创建cert(别的名字也可以)

2. 将下载的SSL证书文件上传到cert下

3. 上传之后如下图所示

2.2 HTTPS server配置

1. 进入到nginx下的conf文件夹下打开nginx.conf文件,更改之前最好先备份

2. 取消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;
    }
}

3. 需要配置一下注释说明的内容

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

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

2.2.3 配置转发

1. 这一步是配置对外访问端口和将http请求强制转为http

2. 删除server多余配置,只需要留下以下配置

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

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

[root@TianXin 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

 4. 置完成后测试一下是否配置正确,在sbin目录下运行测试命令

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

5. 完整配置参考配置示例

6. 配置完成后访问域名

三、配置问题

1. 注意如果是nginx 1.16.1之前版本, 配置内容会有有所变化,请参考别的版本配置

2. 如果运行./nginx -t时出现以下错误,标识nginx没有安装SSL模块

# 错误提示
[root@TianXin sbin]# ./nginx -t
nginx: [emerg] unknown directive "ssl_certificate" in /opt/Nginx/conf/nginx.conf:34
nginx: configuration file /opt/Nginx//conf/nginx.conf test failed

# 解决方法是重新配置nginx,重新编译带上--with-http_stub_status_module --with-http_ssl_module
# 可以重新安装nginx(建议, 可以避免很多问题)也可以不用重新安装, 不用重新安装只需要执行下面的两个命令即可
./configure --prefix=/opt/Nginx --with-http_stub_status_module --with-http_ssl_module
make
# 不要执行make install 否则会覆盖原来的文件

3. 配置完成访问网站提示ERR_SSL_PROTOCOL_ERROR

# 此问题在该版本中出现是因为listen配置的时候把443 后面的ssl删除了导致这个错误
server {
    # 注意这里就是443 ssl, 不要把ssl删除了,之前的版本
    listen       443 ssl;
    ...
}

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

四、配置示例

1.nginx.conf配置SSL示例

worker_processes  1;

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;
        rewrite ^(.*)$ https://$host$1 permanent;
    }

    # https
    server {
        listen       443 ssl;
        server_name  www.codecoord.com;
        root /opt/Nginx/html;
        index index.html;
        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;
        }
    }
}

 

发布了118 篇原创文章 · 获赞 1115 · 访问量 213万+

猜你喜欢

转载自blog.csdn.net/sinat_34104446/article/details/103406785