了解しました。
以下は ご提示の
https://wp.kobore.net/2025/12/28/post-23393/
を前提知識として固定し、その上で
EC2-2(WSL2 上)で動いている Web サービスを、
同一 LAN 内の他ホストからhttps://192.168.0.3/で安定的にアクセス可能にするための実運用メモ
を、抜け・省略なし/再現可能/将来読み返しても壊さない形でまとめます。
(※「なぜその手順が必要か」もすべて明示します)
EC2-2(WSL2)を LAN 内から
https://192.168.0.3/ で公開するための確定メモ
0. ゴールと非ゴール
ゴール
-
同一 LAN 内の別 PC から
https://192.168.0.3/
にアクセスできる -
証明書警告なし(🔒表示)
-
Windows / WSL 再起動後も 復旧可能
非ゴール(今回はやらない)
-
インターネット公開
-
EC2(AWS)移行
-
認証・WAF 等の高度な公開制御
1. 全体構成(完成形)
[LAN 内の別PC ブラウザ]
|
| https://192.168.0.3
v
[Windows ホスト]
443 / 80
|
| portproxy
v
[WSL2: ubuntu-ec2-2]
nginx (TLS終端)
|
| http://127.0.0.1:18080
v
[docker compose]
reverse-proxy (18080)
|
+--> api (FastAPI)
+--> EC2-1 media (HLS / thumbs)
2. 前提条件(成立していること)
Windows 側
-
Windows 11
-
LAN IP が 192.168.0.3
-
mkcert により証明書作成済み
-
rootCA.pem を ローカルコンピューターにインストール済み
WSL 側
-
WSL2 使用
-
対象ディストリビューション:
ubuntu-ec2-2 -
docker / docker compose 稼働
-
http://localhost:18080/が
Windows からは表示できている
3. mkcert による証明書(既存前提の整理)
3.1 作成済み証明書
mkcert 192.168.0.3 localhost 127.0.0.1 ::1
生成物:
-
192.168.0.3+3.pem -
192.168.0.3+3-key.pem
SAN 含有:
-
IP: 192.168.0.3
-
localhost
-
127.0.0.1
-
::1
➡ LAN IP 直接指定に完全対応
4. 証明書を ubuntu-ec2-2 に配置
(※ 既定 Ubuntu ではなく ubuntu-ec2-2)
wsl -d ubuntu-ec2-2
sudo mkdir -p /etc/nginx/ssl/192.168.0.3
sudo cp /mnt/c/Users/tomoi/Downloads/192.168.0.3+3.pem \
/etc/nginx/ssl/192.168.0.3/cert.pem
sudo cp /mnt/c/Users/tomoi/Downloads/192.168.0.3+3-key.pem \
/etc/nginx/ssl/192.168.0.3/key.pem
sudo chmod 644 /etc/nginx/ssl/192.168.0.3/cert.pem
sudo chmod 600 /etc/nginx/ssl/192.168.0.3/key.pem
5. ubuntu-ec2-2 に nginx を導入・常駐化
sudo apt update
sudo apt install -y nginx
sudo systemctl enable nginx
6. nginx(TLS 終端)設定
6.1 設定ファイル作成
/etc/nginx/sites-available/192.168.0.3.conf
server {
listen 443 ssl;
server_name 192.168.0.3;
ssl_certificate /etc/nginx/ssl/192.168.0.3/cert.pem;
ssl_certificate_key /etc/nginx/ssl/192.168.0.3/key.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
location / {
proxy_pass http://127.0.0.1:18080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
}
}
server {
listen 80;
server_name 192.168.0.3;
return 301 https://$host$request_uri;
}
6.2 有効化
sudo ln -sf /etc/nginx/sites-available/192.168.0.3.conf \
/etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
7. Windows → WSL の portproxy 設定
7.1 ubuntu-ec2-2 の IP を取得
wsl -d ubuntu-ec2-2 -- hostname -I
例:
172.29.11.141
7.2 portproxy 設定(管理者 PowerShell)
netsh interface portproxy delete v4tov4 listenaddress=0.0.0.0 listenport=443
netsh interface portproxy delete v4tov4 listenaddress=0.0.0.0 listenport=80
netsh interface portproxy add v4tov4 `
listenaddress=0.0.0.0 listenport=443 `
connectaddress=172.29.11.141 connectport=443
netsh interface portproxy add v4tov4 `
listenaddress=0.0.0.0 listenport=80 `
connectaddress=172.29.11.141 connectport=80
確認:
netsh interface portproxy show all
8. Windows ファイアウォール
Windows 自身
https://192.168.0.3/
LAN 内別 PC
https://192.168.0.3/
🔒 が表示されれば成功。
11. よく壊れる原因(実体験ベース)
| 原因 | 症状 |
|---|---|
| WSL IP 変更 | 無言で https が死ぬ |
| portproxy が旧IP | curl が通らない |
| nginx が別ディストリ | 設定が反映されない |
| CA を Current User に入れた | Chrome/Edge が警告 |
12. 固定化(強く推奨)
WSL IP 変動対策
PowerShell スクリプト:
-
システム起動時
-
最上位権限
13. 最終まとめ(要点だけ)
-
TLS 終端は ubuntu-ec2-2 の nginx
-
Windows は単なる 転送役
-
mkcert / CA は 一度入れたら触らない
-
壊れたら
「WSL IP → portproxy → nginx」
の順で確認