nginxを使ってみます。
/
+ nginx
| + logs
| - nginx.conf
+ scripts
| - entrypoint.sh
- .env
- docker-compose.yaml
|
docker-compose.yaml
|
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:
|
# docker-local/docker-compose.yaml
#
# ローカルPC上で nginx の振り分け動作を確認する
#
# 構成:
# ローカルアプリ --[XML-RPC]--> localhost:8888 (nginx)
# │
# ▼
# 既存試験環境API (Java)
#
# 使い方:
# 1. .env ファイルに既存試験環境のホスト:ポートを設定
# 2. docker compose up -d
# 3. ローカルアプリの接続先を localhost:8888 に変更
# 4. 動作確認
services:
nginx:
image: nginx:1.25-alpine
container_name: migration-nginx
ports:
- "${NGINX_PORT:-8888}:80"
volumes:
# nginx.conf はテンプレートとしてマウント(entrypoint で envsubst)
- ./nginx/nginx.conf:/etc/nginx/nginx.conf.template:ro
- ./nginx/conf.d:/etc/nginx/conf.d:ro
- ./nginx/logs:/var/log/nginx
- ./scripts/entrypoint.sh:/entrypoint.sh:ro
entrypoint: ["/bin/sh", "/entrypoint.sh"]
environment:
- TZ=Asia/Tokyo
- JAVA_API_HOST=${JAVA_API_HOST:-192.168.1.100}
- JAVA_API_PORT=${JAVA_API_PORT:-8080}
#extra_hosts:
# Docker Desktop (Mac/Windows) では自動で使える
# Linux の場合は下記のコメントを外す
# - "host.docker.internal:host-gateway"
restart: unless-stopped
|
|
|
nginx.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:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
|
# docker-local/nginx/nginx.conf
#
# ローカル試験用 nginx 設定
# AWS 本番と同じ振り分け構造を再現する
#
# 現段階の目的:
# 全リクエストを既存試験環境 Java API にそのまま流し、
# nginx を経由しても動作が変わらないことを確認する
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 256;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
# -------------------------------------------------
# ログフォーマット
# AWS 本番と同じ JSON 形式
# -------------------------------------------------
log_format main_json escape=json
'{'
'"time":"$time_iso8601",'
'"remote_addr":"$remote_addr",'
'"request_method":"$request_method",'
'"request_uri":"$request_uri",'
'"status":$status,'
'"body_bytes_sent":$body_bytes_sent,'
'"request_time":$request_time,'
'"upstream_addr":"$upstream_addr",'
'"upstream_status":"$upstream_status",'
'"upstream_response_time":"$upstream_response_time",'
'"api_backend":"$api_backend",'
'"content_type":"$content_type",'
'"http_user_agent":"$http_user_agent"'
'}';
access_log /var/log/nginx/access.log main_json;
sendfile on;
keepalive_timeout 65;
# XML-RPC は POST ボディが大きくなることがある
client_max_body_size 10m;
# -------------------------------------------------
# Upstream 定義
# -------------------------------------------------
# 既存試験環境 Java API
# ※ .env の値は docker-compose では直接 nginx.conf に展開されない
# → scripts/entrypoint.sh で envsubst を使って解決
upstream upstream_java {
server ${JAVA_API_HOST}:${JAVA_API_PORT};
keepalive 8;
}
server {
listen 80;
# ヘルスチェック
location /health {
return 200 '{"status":"ok","backend":"nginx-local"}';
add_header Content-Type application/json;
}
# デバッグ用: 現在の振り分け先を確認
location /debug/routing {
return 200 '{"request_uri":"$request_uri","api_backend":"$api_backend"}';
add_header Content-Type application/json;
}
# API エンドポイント
location / {
# 現段階: 全て Java へ
proxy_pass http://upstream_java;
# ヘッダ設定
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 $scheme;
proxy_set_header X-Original-URI $request_uri;
# XML-RPC の Content-Type をそのまま通す
proxy_set_header Content-Type $content_type;
# タイムアウト設定
proxy_connect_timeout 10s;
proxy_send_timeout 30s;
proxy_read_timeout 30s;
# バッファリング設定
proxy_buffering on;
proxy_buffer_size 8k;
proxy_buffers 8 8k;
}
}
}
|
|
|
entrypoint.sh
|
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:
|
#!/bin/sh
# docker-local/scripts/entrypoint.sh
#
# nginx.conf 内の環境変数を実際の値に置換してから nginx を起動
# ${JAVA_API_HOST}, ${JAVA_API_PORT} を .env の値で置き換える
set -e
# デフォルト値
JAVA_API_HOST=${JAVA_API_HOST:-localhost}
JAVA_API_PORT=${JAVA_API_PORT:-8080}
echo "================================================"
echo " Migration nginx (local)"
echo " Java API: ${JAVA_API_HOST}:${JAVA_API_PORT}"
echo " nginx port: 80 (container) -> see docker-compose"
echo "================================================"
# nginx.conf のテンプレートから実設定を生成
# envsubst で ${JAVA_API_HOST} と ${JAVA_API_PORT} のみを置換
# ※ nginx 変数($host, $request_uri 等)は置換しない
envsubst '${JAVA_API_HOST} ${JAVA_API_PORT}' \
< /etc/nginx/nginx.conf.template \
> /etc/nginx/nginx.conf
echo "nginx.conf generated. upstream_java -> ${JAVA_API_HOST}:${JAVA_API_PORT}"
# 設定テスト
nginx -t
# nginx をフォアグラウンドで起動
exec nginx -g 'daemon off;'
|
|
|
.env
|
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
|
# docker-local/.env
#
# ここを自分の環境に合わせて書き換える
# nginx がリッスンするポート(ローカルアプリからの接続先)
NGINX_PORT=8888
# 既存試験環境の Java API エンドポイント
# 例: 社内サーバ 192.168.1.100:8080
# 例: ローカルの Java host.docker.internal:8080
JAVA_API_HOST=172.20.0.105
JAVA_API_PORT=8080
|
|
.envの内容が反映されているか確認します。
> docker compose config
name: local-bservice-forward
services:
nginx:
container_name: migration-nginx
entrypoint:
- /bin/sh
- /entrypoint.sh
environment:
JAVA_API_HOST: 172.20.0.105
JAVA_API_PORT: "8080"
TZ: Asia/Tokyo
image: nginx:1.25-alpine
networks:
default: null
ports:
- mode: ingress
target: 80
published: "8888"
protocol: tcp
restart: unless-stopped
volumes:
- type: bind
source: C:\docker-work\nginx\nginx.conf
target: /etc/nginx/nginx.conf.template
read_only: true
bind: {}
- type: bind
source: C:\docker-work\nginx\logs
target: /var/log/nginx
bind: {}
- type: bind
source: C:\docker-work\scripts\entrypoint.sh
target: /entrypoint.sh
read_only: true
bind: {}
networks:
default:
name: local-bservice-forward_default
通信を実行するとlogs配下にaccess.logというファイルが生成され、以下の情報が出力されました。
{"time":"2026-03-30T14:36:16+09:00","remote_addr":"172.20.0.1","reque

st_method":"POST","request_uri":"/businessservice/services","status

":200,"body_bytes_sent":1383,"request_time":0.084,"upstream_addr":"

172.20.0.105:8080","upstream_status":"200","upstream_response_time"

:"0.084","api_backend":"java","content_type":"text/xml","http_user_

agent":"XML-RPC.NET"}
# docker
docker compose up -d
docker compose down
docker compose config
# powershell
curl http://localhost:8888/health
curl http://localhost:8888/debug/routing
[System.Text.Encoding]::UTF8.GetString((Invoke-WebRequest http://loca

lhost:8888/debug/routing).Content)
# cmd
curl.exe http://localhost:8888/debug/routing
▲ PageTop ■ Home
Copyright (C) 2026 ymlib.com