支持https访问

文章目录

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

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

打开某为云官网 https://console.huaweicloud.com/



2. 安装 nginx

shell 复制代码
sudo apt update
sudo apt-get install nginx
nginx -v

3. 安装 snapd

shell 复制代码
sudo apt install snapd
sudo apt-get remove certbot

4. 安装 certbot

shell 复制代码
sudo snap install --classic certbot

5. 生成证书

shell 复制代码
sudo certbot certonly --nginx

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

shell 复制代码
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/

7. 修改 main.go 程序如下

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. 编译程序

shell 复制代码
sh build.sh
shell 复制代码
#!/bin/bash
go build -o screen_share src/*

9. 启动程序

shell 复制代码
./screen_share

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

shell 复制代码
https://www.liangzixuexi.com:8081/static/share.html

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

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

go 复制代码
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. 编译

shell 复制代码
sh build.sh

13. 启动程序

shell 复制代码
./screen_share

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

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

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

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

相关推荐
我命由我123451 小时前
Java 开发 - 粘包处理器 - 基于消息头 + 消息体(魔数验证、长度验证)
java·网络·后端·网络协议·java-ee·intellij-idea·intellij idea
小糖学代码1 小时前
网络:5.应用层协议HTTP
网络·网络协议·http
2501_915106322 小时前
iOS性能调优的系统化实践,从架构分层到多工具协同的全流程优化指南(开发者深度版)
android·ios·小程序·架构·uni-app·iphone·webview
liu****4 小时前
18.HTTP协议(一)
linux·网络·网络协议·http·udp·1024程序员节
赖small强5 小时前
【ZeroRange WebRTC】KVS WebRTC 示例中的 HTTP 通信安全说明
https·webrtc·tls·aws sigv4·信道安全·时间与重放控制
拾忆,想起6 小时前
Dubbo异步调用实战指南:提升微服务并发性能
java·服务器·网络协议·微服务·云原生·架构·dubbo
Digitally9 小时前
无需 iTunes 备份与恢复 iPhone 的 2 种方法
iphone
阿巴~阿巴~12 小时前
基于UDP协议的英汉翻译服务系统:从网络通信到字典查询的完整机制
linux·服务器·网络·网络协议·udp协议·套接字绑定·英汉翻译服务系统
another heaven13 小时前
【计算机网络 HTTP 请求参数规范详解】
网络协议·计算机网络·http
慧慧吖@15 小时前
sse,短轮询,长轮询,webSocket
网络·websocket·网络协议