Skip to content

nginx部署https

下载证书

腾讯云免费ssl证书地址

免费证书保质期:90天

放置证书

文件地址:

bash
/usr/local/nginx/cert/blog.coolo.top_nginx

配置nginx

  • nginx配置文件地址
bash
nginx -t

输出:

bash
/usr/local/lighthouse/softwares/nginx/conf/nginx.conf
  • nginx配置
nginx
server {
    listen 80;
    server_name blog.coolo.top;

    root /var/www/blog;
    index index.html;

    location / {
        try_files $uri /index.html;
    }
}

server {
    listen       443 ssl;
    server_name  blog.coolo.top;
    ssl_certificate       /usr/local/nginx/cert/blog.coolo.top_nginx/blog.coolo.top_bundle.pem;
    ssl_certificate_key   /usr/local/nginx/cert/blog.coolo.top_nginx/blog.coolo.top.key;
	  ssl_session_timeout 5m;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    root /var/www/blog;
    index index.html;

    location / {
    	try_files $uri /index.html;
    }
}
  • 验证配置文件
bash
nginx -t

输出成功:

bash
nginx: the configuration file /usr/local/lighthouse/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lighthouse/softwares/nginx/conf/nginx.conf test is successful
  • 重启nginx
bash
nginx -s reload

重定向http请求到https(可选)

nginx

# http请求
server {
    listen 80;
    server_name blog.coolo.top;

    # 将所有HTTP请求重定向到HTTPS
    return 301 https://$host$request_uri;
}

# https请求
server {
    listen       443 ssl;
    server_name  blog.coolo.top;
    ssl_certificate       /usr/local/nginx/cert/blog.coolo.top_nginx/blog.coolo.top_bundle.pem;
    ssl_certificate_key   /usr/local/nginx/cert/blog.coolo.top_nginx/blog.coolo.top.key;
	  ssl_session_timeout 5m;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    root /var/www/blog;
    index index.html;

    location / {
    	try_files $uri /index.html;
    }
}

京ICP备2024093538号-1