腾讯云服务器nginx配置SSL证书实现https访问

1、由于我是在买的是腾讯云主机,在腾讯云上可以申请免费ssl证书,申请通过后就可以下载证书文件,另外先打开安全组里面的443端口。

2、腾讯云的SSL证书下载包中,有一个单独的Nginx文件夹,里面有两个文件都是我们需要的,我们需要把这两个文件放到我们的服务器中,如果是linux系统,我目前是放到/usr/local/nginx/conf,也就是nginx的配置目录下。

3、使用vim打开nginx配置文件

vim /usr/local/nginx/conf/nginx.conf

配置文件内容:

server {
    listen 443;
    server_name _;
    ssl_certificate /usr/local/nginx/conf/1_www.dsiab.com_bundle.crt;
    ssl_certificate_key /usr/local/nginx/conf/2_www.dsiab.com.key;
    ssl on;
    root /data/wwwroot/default/;
    index index.html index.htm;
    ...
}

5、如果用户使用的是http协议进行访问,那么默认监听的端口是80端口,所以我们需要做一个重定向,我们在上一个代码块的基础上增加一个server节点提供重定向服务:

server {
    listen 80;
    server_name _;
    rewrite ^/(.*)$ https://www.dsiab.com:443/ permanent;
}

所以我的nginx完整的配置内容如下:

server {
    listen 443;
    server_name _;
    ssl_certificate /usr/local/nginx/conf/1_www.dsiab.com_bundle.crt;
    ssl_certificate_key /usr/local/nginx/conf/2_www.dsiab.com.key;
    ssl on;**
    root /data/wwwroot/default/;
    index index.html index.htm;
    location /nginx_status {
      stub_status on;
      access_log off;
      allow 127.0.0.1;
      deny all;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
      expires 30d;
      access_log off;
    }
    location ~ .*\.(js|css)?$ {
      expires 7d;
      access_log off;
    }
    location /bootService {
      proxy_pass http://127.0.0.1:8080;
    }
    location / {
      try_files $uri $uri/ /index.html;
      include proxy.conf;
    }
    location ~ ^/(\.user.ini|\.ht|\.git|\.svn|\.project|LICENSE|README.md) {
      deny all;
    }
  }
  ## 80重定向443
  server {
    listen 80;
    server_name _;
    rewrite ^/(.*)$ https://www.dsiab.com:443/ permanent;
  }
}

7、启动nginx服务:service nginx restart,重启完就可以通过https访问了。

本文章由javascript技术分享原创和收集

发表评论 (审核通过后显示评论):