Introduction

固定 IP を取得しようと思ったら ISP が固定 IP サービスを数年前に廃止していたので困っていたところ、 cloudflared を使えば自宅サーバを公開できるらしい、とのことで試してみた。
ドメインの取得は必要だが、格安ドメインで対処。
GitLab、Snipe-IT があるので nginx をリバースプロキシにして公開。

Description

環境は

  • OS: Debian: 12
  • nginx: 1,22
  • ドメイン: xxxxxx.com

1. Nginx のインストール

1
$ sudo apt install -y nginx

2. /etc/hosts の編集

DNS が諸事情で動いていないので名前解決は手動

1
2
192.168.11.107 snipeit.xxxxxx.local
192.168.11.108 gitlab.xxxxxx.local

3. nginx の設定

リバースプロキシとして使用するための設定を実施。
/etc/nginx/sites-available/lan-proxy.conf を作成していく。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
map $http_upgrade $connection_upgrade { default upgrade; '' close; }

server {
listen 127.0.0.1:8081;
server_name snipeit.xxxxxx.com;

location / {
proxy_pass https://192.168.11.107:443;
proxy_redirect ~^https://snipeit\.xxxxxx\.local(/.*)?$ https://snipeit.xxxxxx.com$1;

proxy_http_version 1.1;
proxy_set_header Host snipeit.xxxxxx.local;
proxy_ssl_name snipeit.xxxxxx.local;
proxy_ssl_server_name on;
proxy_ssl_verify off;
proxy_ssl_protocols TLSv1.3;

proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_read_timeout 300s;

keepalive_timeout 65s;

proxy_set_header Accept-Encoding "";

sub_filter_once off;
sub_filter_types text/html
text/css
application/javascript
application/json
text/plain
application/xml;
sub_filter https://snipeit.xxxxxx.local https://snipeit.xxxxxx.com;
sub_filter //snipeit.xxxxxx.local //snipeit.xxxxxx.com;
}
}

server {
listen 127.0.0.1:8082;
server_name gitlab.xxxxxx.com;

location / {
proxy_pass https://192.168.11.108:443;
proxy_redirect ~^https://gitlab\.xxxxxx\.local(/.*)?$ https://gitlab.xxxxxx.com$1;

proxy_http_version 1.1;
proxy_set_header Host gitlab.xxxxxx.local;
proxy_ssl_name gitlab.xxxxxx.local;
proxy_ssl_server_name on;
proxy_ssl_verify off;
proxy_ssl_protocols TLSv1.3;

proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_read_timeout 300s;

keepalive_timeout 65s;

proxy_set_header Accept-Encoding "";

sub_filter_once off;
sub_filter_types text/html
text/css
application/javascript
application/json
text/plain
application/xml;
sub_filter https://gitlab.xxxxxx.local https://gitlab.xxxxxx.com;
sub_filter //gitlab.xxxxxx.local //gitlab.xxxxxx.com;
}
}
設定 概要
proxy_pass 転送する先の url
proxy_redirect プロキシ先のサーバが nginx に返すレスポンスのLocation, Refreshヘッダの書き換えルール
subsub_filter_types_filter リクエストを書き換える対象。デフォルトは text/html
sub_filter リクエストに含まれる文字を書き換える正規表現ルール
proxy_ssl_verify 転送先のサーバの証明書を検証するか。自己署名証明書が配置されているが面倒なので off

リンク先の url が古い url のままだったりしたので、 sub_filter を使って書き換えることで対処。

設定後は下記で nginx に反映して起動。

1
2
3
4
5
6
7
8
# デフォルト設定のシンボリックリンクを削除
$ rm /etc/nginx/sites-enabled/default
$ ln -s /etc/nginx/sites-available/lan-proxy.conf /etc/nginx/sites-enabled/
# 構文チェック
$ /usr/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
$ systemctl reload nginx

4. cloudflared をインストール

1
2
3
4
$ curl -LO https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb
$ apt install ./cloudflared-linux-amd64.deb && rm ./cloudflared-linux-amd64.deb
$ cloudflared --version
cloudflared version 2025.8.0 (built 2025-08-08-1545 UTC)

Cloudflare のアカウントにログインし、認証情報を配置。
url をコピーして任意の端末上のブラウザから認証すれば OK。

1
2
3
4
5
6
7
8
9
10
$ cloudflared tunnel login
Please open the following URL and log in with your Cloudflare account:

https://dash.cloudflare.com/argotunnel?aud=&callback=https%3A%2F%2Flogin.cloudflareaccess.org%2FyvsCJ9NAdeij4RRREdnz3pNNx5QPdxX6zyeYfauDc8Nf%3D

Leave cloudflared running to download the cert automatically.
2025-08-09T08:42:18Z INF Waiting for login...
2025-08-09T08:43:02Z INF You have successfully logged in.
If you wish to copy your credentials to a server, they have been saved to:
/root/.cloudflared/cert.pem

続いてトンネルを作成。

1
2
3
4
$ cloudflared tunnel create mytunnel
Tunnel credentials written to /root/.cloudflared/713ba16e-21ed-4b21-a2d0-c7afad85f728.json. cloudflared chose this file based on where your origin certificate was found. Keep this file secret. To revoke these credentials, delete the tunnel.

Created tunnel mytunnel with id 713ba16e-21ed-4b21-a2d0-c7afad85f728

次にトンネルの設定ファイルを作成。

/etc/cloudflared/config.yml を編集。
最後の - service: http_status:404 は必須なので注意。

1
2
3
4
5
6
7
8
9
10
11
12
tunnel: mytunnel
credentials-file: /root/.cloudflared/713ba16e-21ed-4b21-a2d0-c7afad85f728.json
# 高負荷時は並列数を追加
# connections: 4

# それぞれの FQDN を nginx のローカル待受にバインド
ingress:
- hostname: snipeit.xxxxxx.com
service: http://127.0.0.1:8081
- hostname: gitlab.xxxxxx.com
service: http://127.0.0.1:8082
- service: http_status:404

作成した設定ファイルを検証。
- service: http_status:404 がないとエラーになります (戒め)

1
2
3
$ cloudflared tunnel ingress validate /etc/cloudflared/config.yml
Validating rules from /etc/cloudflared/config.yml
OK

サブドメインとトンネルのルーティングを実施。

1
2
3
4
$ cloudflared tunnel route dns mytunnel snipeit.xxxxxx.com
2025-08-09T09:07:17Z INF Added CNAME snipeit.xxxxxx.com which will route to this tunnel tunnelID=713ba16e-21ed-4b21-a2d0-c7afad85f728
$ cloudflared tunnel route dns mytunnel gitlab.xxxxxx.com
2025-08-09T09:07:29Z INF Added CNAME gitlab.xxxxxx.com which will route to this tunnel tunnelID=713ba16e-21ed-4b21-a2d0-c7afad85f728

次に cloudflared を自動起動するように設定。
何故か systemctl enable --now cloudflared がエラーになったので、手動で unit ファイルを作成。
/etc/systemd/system/cloudflared.service を編集。

1
2
3
4
5
6
7
8
9
10
11
12
13
[Unit]
Description=cloudflared tunnel
After=network-online.target
Wants=network-online.target

[Service]
User=nobody
ExecStart=/usr/bin/cloudflared --no-autoupdate --config /etc/cloudflared/config.yml tunnel run
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=multi-user.target

後は念のため cloudflared を再起動。

1
$ systemctl restart cloudflared

5. ファイアウォール設定

別にサーバを直接公開しているわけでもないが、 cloudflared が動いているサーバのファイアーウォールを強化しておく。
管理は必要なので SSH 以外は拒否。
cloudflared はアウトバウンドを使ってトンネルをしているので、SSH 以外のインバウンドは拒否しても問題なし。

1
2
3
4
5
$ apt install ufw -y
$ ufw default deny incoming
$ ufw default allow outgoing
$ ufw allow 22/tcp
$ ufw enable