支持https访问

文章目录

      • 1. 打开自己的云服务器的 80 和 443 端口
      • 2. 安装 nginx
      • 3. 安装 snapd
      • 4. 安装 certbot
      • 5. 生成证书
      • 6. 拷贝生成的证书到项目工作目录
      • 7. 修改 main.go 程序如下
      • 8. 编译程序
      • 9. 启动程序
      • 10. 使用 https 和端口 8081 访问页面成功
      • 11. 下面修改程序,支持 https 和 http 能同时访问
      • 12. 编译
      • 13. 启动程序
      • 14. 使用 http 和 8080 端口访问成功
      • 15. 使用 https 和 8081 端口访问成功

1. 打开自己的云服务器的 80 和 443 端口

打开某为云官网 https://console.huaweicloud.com/
支持https访问_第1张图片

支持https访问_第2张图片

支持https访问_第3张图片

支持https访问_第4张图片
支持https访问_第5张图片

支持https访问_第6张图片

支持https访问_第7张图片
支持https访问_第8张图片

2. 安装 nginx

sudo apt update
sudo apt-get install nginx
nginx -v

3. 安装 snapd

sudo apt install snapd
sudo apt-get remove certbot

4. 安装 certbot

sudo snap install --classic certbot

5. 生成证书

sudo certbot certonly --nginx

支持https访问_第9张图片

6. 拷贝生成的证书到项目工作目录

cd ~/dev/go/screen_share
mkdir conf
cp /etc/letsencrypt/live/www.liangzixuexi.com/fullchain.pem   ~/dev/go/screen_share/conf/
cp /etc/letsencrypt/live/www.liangzixuexi.com/privkey.pem   ~/dev/go/screen_share/conf/

支持https访问_第10张图片

7. 修改 main.go 程序如下

package main

import (
  "fmt"
  "net/http"
)

func main() {
  // 1.定义一个 URL 前缀
  staticURL := "/static/"
  // 2.定义一个 FileServer
  fs := http.FileServer(http.Dir("./static"))
  // 3.绑定 url 和 FileServer
  http.Handle(staticURL, http.StripPrefix(staticURL, fs))
  // 4.启动 HttpServer
  //err := http.ListenAndServe(":8080",nil)
  err := http.ListenAndServeTLS(":8081", "./conf/fullchain.pem", "./conf/privkey.pem", nil)
  if err != nil {
    fmt.Println(err)
  }
}

8. 编译程序

sh build.sh

在这里插入图片描述

#!/bin/bash
go build -o screen_share src/*

9. 启动程序

./screen_share

10. 使用 https 和端口 8081 访问页面成功

https://www.liangzixuexi.com:8081/static/share.html

支持https访问_第11张图片

但是现在只能通过 https 访问,原来的 http 不能访问了

11. 下面修改程序,支持 https 和 http 能同时访问

package main

import (
  "fmt"
  "net/http"
)

func startHttp(port string){
  fmt.Printf("Start Http port: %s\n", port)
  err := http.ListenAndServe(port, nil)
  if err != nil {
    fmt.Println(err)
  }
}

func startHttps(port, cert, key string){
  fmt.Printf("Start Https port: %s\n", port)
  err := http.ListenAndServeTLS(port, cert, key, nil)
  if err != nil {
    fmt.Println(err)
  }
}

func main() {
  // 1.定义一个 URL 前缀
  staticURL := "/static/"
  // 2.定义一个 FileServer
  fs := http.FileServer(http.Dir("./static"))
  // 3.绑定 url 和 FileServer
  http.Handle(staticURL, http.StripPrefix(staticURL, fs))
  // 4.启动 HttpServer
  //err := http.ListenAndServe(":8080",nil)
  go startHttp(":8080")
  //err := http.ListenAndServeTLS(":8081", "./conf/fullchain.pem", "./conf/privkey.pem", nil)
  // 5.启动 HttpsServer
  startHttps(":8081", "./conf/fullchain.pem", "./conf/privkey.pem")
}

12. 编译

sh build.sh

在这里插入图片描述

13. 启动程序

./screen_share

在这里插入图片描述

14. 使用 http 和 8080 端口访问成功

输入 http://www.liangzixuexi.com:8080/static/share.html

支持https访问_第12张图片

15. 使用 https 和 8081 端口访问成功

输入 https://www.liangzixuexi.com:8081/static/share.html

支持https访问_第13张图片

你可能感兴趣的:(webrtc源码分析,https,iphone,网络协议)