使用 HTTPS 在家庭网络上运行服务器

Ale*_*gue 5 networking https nginx raspberry-pi

正如标题所暗示的那样,我正在尝试配置我的服务器以使用 HTTPS 提供流量。我已经能够公开服务器并从中接收信息,只是没有 HTTPS。

我将介绍一些有趣的细节,但这是主要目标。

  1. 我在家庭网络上的 Raspberry Pi 上运行了一个 Go 服务器,我将其用作个人 API。
  2. 我将家庭网络的端口 80 和 443 端口转发,并将其指向我的 Raspberry Pi 的静态 IP 地址
  3. 我拥有一个指向我的家庭网络 IP 的域。
  4. 我的 go 服务器在端口 8088 上运行,所以我使用 Nginx 将请求从基本 IP 转发到我服务器的端口。

到目前为止,这一切都运行良好,只是我想允许我的服务器使用 HTTPS。

我的问题是我在这个设置中有很多活动部分,那么什么负责确保 HTTPS:

  • 域?
  • 我的 Nginx 配置?
  • 服务器本身?
  • 以上所有的组合。

我不确定。

只是在寻找为此服务器启用 HTTPS 的方向,谢谢!

Roi*_*oid 6

您的 Nginx 可以处理 http 和 https 请求。

  1. 将您的 nginx 设置为侦听端口 80 并将 80 请求重定向到 443。

    server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name example.com www.example.com;
        return 301 https://$server_name$request_uri;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在您的 nginx 中添加您的证书并将来自 443 的请求代理到 GO 服务器。

    server {
       listen 443;
       ssl on;
       ssl_certificate /etc/ssl/ssl-bundle.crt;
       ssl_certificate_key /etc/ssl/ssl-tutorials.key;
       server_name ssl-tutorials.com;
       access_log /var/log/nginx/nginx.vhost.access.log;
       error_log /var/log/nginx/nginx.vhost.error.log;
    
       location / {
           proxy_redirect          off;
           proxy_set_header        Host            $host;
           proxy_set_header        X-Real-IP       $remote_addr;
           proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
           client_body_buffer_size 512k;
           proxy_connect_timeout   180;
           proxy_send_timeout      180;
           proxy_read_timeout      360;
           proxy_buffering         off
    
           #Proxy request to your GO app
           proxy_pass http://<ip_server>:<port>;
        }
     }
    
    Run Code Online (Sandbox Code Playgroud)

  • 也许这个答案还可以详细说明如何从问题中生成所述证书(LetsEncrypt/自签名),OP似乎对此一无所知。 (6认同)