Nginx SSL 证书部署 并配置http重定向到https

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zyddj123/article/details/84973125

取自腾讯云文档结合自己站点,总结如下:

1.获取证书

证书申请下来之后 ,下载证书,解压之后图片:
在这里插入图片描述
这里我们使用 nginx 服务器 ,打开文件夹如下:
在这里插入图片描述

2.部署证书

将域名 www.bug404.club 的
证书文件 1_www.bug404.club_bundle.crt 、
私钥文件 2_www.bug404.club.key
保存到同一个目录,例如 /usr/local/nginx/conf 目录下(随便一个目录,后面配置nginx需要引入)

3.nginx配置

server {
        listen 443;
        server_name www.bug404.club; #填写绑定证书的域名
        ssl on;
        ssl_certificate 1_www.bug404.club_bundle.crt;
        ssl_certificate_key 2_www.bug404.club.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照这个套件配置
        ssl_prefer_server_ciphers on;
        location / {
            root   html; #站点目录
            index  index.html index.htm;
        }
    }

配置完成后,开放443端口,详见CentOS7 防火墙 firewalld使用方法,重启 nginx。就可以使用 https://www.bug404.club 来访问。

配置文件参数 说明
listen 443 SSL 访问端口号为 443
ssl on 启用 SSL 功能
ssl_certificate 证书文件
ssl_certificate_key 私钥文件
ssl_protocols 使用的协议
ssl_ciphers 配置加密套件,写法遵循 openssl 标准

4.使用全站加密,http 自动跳转 https(可选)

对于用户不知道网站可以进行 https 访问的情况下,让服务器自动把 http 的请求重定向到 https。
在服务器这边的话配置的话,可以在页面里加 js 脚本,也可以在后端程序里写重定向,当然也可以在 web 服务器来实现跳转。Nginx 是支持 rewrite 的(只要在编译的时候没有去掉 pcre)
httpserver (另外写一个server)里增加 rewrite ^(.*) https://$host$1 permanent;
这样就可以实现 80 进来的请求,重定向为 https 了。

5.我的配置

#
# The default server
#
server {
    listen 443;
    server_name www.bug404.club; #填写绑定证书的域名
    ssl on;
    ssl_certificate 1_www.bug404.club_bundle.crt;
    ssl_certificate_key 2_www.bug404.club.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照这个套件配置
    ssl_prefer_server_ciphers on;

    charset utf8;

    access_log  logs/host.access.log  main;

    location / {
        root   /var/www/html/index/;
        index  index.html index.php index.htm;
    # example
        #ModSecurityEnabled on;
        #ModSecurityConfig /etc/nginx/modsecurity.conf;
        autoindex on;                            #开启目录浏览功能;   
        autoindex_exact_size off;            #关闭详细文件大小统计,让文件大小显示MB,GB单位,默认为b;   
        autoindex_localtime on;              #开启以服务器本地时区显示文件修改日期!
    }

    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /var/www/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
server{
	listen 80;
    server_name www.bug404.club;
    rewrite ^(.*) https://$host$1 permanent;
}

猜你喜欢

转载自blog.csdn.net/zyddj123/article/details/84973125