nginx+Javaアプリのリバースプロキシ構成構築

nginxで、とあるJavaアプリをリバプロ構成で動作させたときのメモ

Contents

対象環境

  • OS:CentOS 7.2
  • WEBサーバ:nginx 1.10.1
  • アプリ:Java(Tomcat) ※某OSSツール

やりたかったこと

諸事情により、[デフォルト81番ポート]で動作するJavaアプリを80番ポートで動作させたかった。

http://[アプリの稼働URL]:81
↓↓↓
http://[アプリの稼働URL]

nginxのリバプロ活用事例は以前から知っていたので、何はともあれ実践してみることにした。

nginxのインストール

普通にyum install。自動起動まで有効

# yum install nginx
# systemctl enabled nginx
# systemctl start nginx

nginx設定

ここからはnginx自体の初期設定とアプリ用のconfファイルを設定。

# cat /etc/nginx/nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    real_ip_header                 X-Forwarded-For;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
    server_tokens    off;

}
# cat /etc/nginx/conf.d/java_apps.conf

server {

server_name [サーバドメイン名];
client_max_body_size 128m;
location / { # IPv6 isn't supported in proxy_pass yet.
proxy_pass [アプリの稼働URL]:81;
proxy_set_header Host             $host;
proxy_set_header X-Real-IP        $remote_addr;
proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
access_log on;

            }
}

設定の適用・動作確認

設定後、’nginx -t’ で設定テスト(念の為)の後でリロード。
リロード後に80番ポート(特にポート指定せずアクセス)で確認できればOK

# nginx -t
# systemctl reload nginx

Todo

転送先をhttps(443)でも動作できるよう設定を検証してみたい。
あと、nginxのロゴは中々いい感じだと思う。

シェアする

  • このエントリーをはてなブックマークに追加

フォローする