IP アドレス計算をする「ipcalc」コンテナを Nginx Proxy 環境下で動作させる
以前に IP アドレスの計算をしてくれる Web アプリケーションを作成したのですが、distroless を使ったマルチステージビルドの練習をすべく、この Web アプリケーションを docker コンテナイメージにしてみました。 作成した Docker コンテナイメージは以下で公開しています。
この Docker コンテナイメージを Nginx を使った HTTP Proxy 配下で動作させる方法をメモしておきます。
Docker のインストール¶
Docker をインストールします。
dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
dnf install -y docker-ce docker-ce-cli containerd.io
systemctl start docker
systemctl enable docker
ipcalc コンテナの開始¶
Docker コンテナを起動しておきます。
Nginx のインストール¶
Nginx をインストールします。
dnf config-manager --add-repo https://s3.sig9.org/repos/nginx.repo
dnf install -y nginx
systemctl start nginx
systemctl enable nginx
設定ファイル¶
Nginx の設定ファイルである /etc/nginx/conf.d/default.conf を以下のように修正します。 server_name は適宜、環境に合わせて修正します。
upstream upstream {
server localhost:5000;
}
server {
listen 80;
server_name ipcalc.example.com;
location / {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Headers' X-Requested-With;
add_header 'Access-Control-Allow-Methods' GET,POST,OPTIONS;
proxy_pass http://upstream/;
}
}
Nginx の起動¶
設定ファイルの修正が完了したら Nginx を起動します。
Web ブラウザでのアクセステスト¶
Web ブラウザでアクセスした際、以下のように表示されれば成功です。

IP アドレスを入力するとホスト数などを計算した結果を表示します。

参考¶
デフォルトの /etc/nginx/conf.d/default.conf¶
server {
listen 80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}