> 文档中心 > Nginx http转换成https

Nginx http转换成https

http 大家都知道是不安全的 然后想换成https

申请ssl证书

1.你得先有ssl证书(阿里云为例)

然后就是配置一下

审核完成之后就是下载证书了

下载完会有一个压缩包

你把压缩包解压放入你nginx.conf目录下就可以了

/www/server/nginx/conf (宝塔对应的目录)

重点来了

配置

nginx.conf

  server {
        listen       443 ssl;
        #存放域名或者你服务的路径
        server_name localhost;
        #把证书放进conf目录下 然后就是对应着填写
        ssl_certificate xxxxxx.pem;
        ssl_certificate_key xxxxx.key;

        #access_log  logs/host.access.log  main;
        #后台管理静态资源存放
      location / { 
          #文件目录
          root  html;
          #首页的样式
          index  index.html;
        }
      location /prod-api { 
        #反向代理
          proxy_pass xxxx.xxxx.xxxx;
        }

  }
  server {
        listen       80;

       #域名
        server_name  xxxx.xxx.com xxxx.com;

    #让所有访问80端口 http 的都跳转到443 也就是https
        return 301 https://$server_name$request_uri;
        root html;
      }

感谢评论给出一个另一种的配置方法(这里也加上去)

方法二(80=>443):

配置:

 server {
        listen 80;
        listen       443 ssl;
        #存放域名或者你服务的路径
        server_name xxx.xxx.com xxx.com;
        #把证书放进conf目录下 然后就是对应着填写
        ssl_certificate xxxxx.xxx.pem;
        ssl_certificate_key xxxx.xxxx.com.key;
        if ($server_port !~ 443){
         rewrite ^(/.*)$ https://$host$1 permanent;
       }
        #access_log  logs/host.access.log  main;
        #后台管理静态资源存放
      location / { 
          #文件目录
          root  html;
          #首页的样式
          index  index.html;
        }
      location /prod-api { 
        #反向代理 
          proxy_pass http://xxx.xxx.xxxx.xxx;
        }

  }

成果展示:

冰雪之城