Nginxの設定

NginxからHULFT10 Smart Proxy(Web API)またはHULFT10 Smart Proxy(WSS)に処理を振り分けるための設定について説明します。

設定は、Nginxの設定ファイル(/(導入ディレクトリー)/nginx/conf/nginx.conf)にて行います。

 

振り分けの設定例は以下のとおりです。

注意

ログの出力先となる/var/log/nginxディレクトリーは、自動では作成されません。

Nginxを起動する前に、/var/log/nginxディレクトリーを作成してください。

Web APIまたはWSSへの振り分け

https 標準待ち受けポート(443)にて受信した要求を、HULFT10 Smart Proxy(Web API)またはHULFT10 Smart Proxy(WSS)へ振り分けます。

 

設定ファイル(/(導入ディレクトリー)/nginx/conf/nginx.conf)

# HTTPS server
#
server {
    listen   443 ssl;
    server_name  localhost;

    ssl_certificate      cert.pem;
    ssl_certificate_key  cert.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;
    access_log /var/log/nginx/access.log;
    error_log  /var/log/nginx/error.log;

#    ssl_ciphers  HIGH:!aNULL:!MD5;
#    ssl_prefer_server_ciphers  on;
#
    # bind to api-server port
    location /api/ {
       proxy_pass   https://sp-api-server-001:41000;
    }
    # bind to wss-server port
    location /wss/ {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_pass   https://sp-wss-server-001:42000/;
    }
    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }
}




https 標準待ち受けポート (443) にてlistenし、ssl を有効化


証明書を指定
秘密鍵を指定










REST APIの待ち受け設定は、/api/にて受け付け



WebSocketの待ち受け設定は、/wss/ にて受け付け



HULFT10 Smart Proxy(WSS)の待ち受けポートへ接続

任意のポートで受け、WSSへの振り分け

HULFTからの要求を任意のポートで受け、HULFT10 Smart Proxy(WSS)へ振り分けます。

 

設定ファイル(/(導入ディレクトリー)/nginx/conf/nginx.conf)

# inner port
#
#  intranet | server | internet
#  client -----> 30000
#             38000 -> tunnel
stream {
        upstream tcp_backend {
             server       127.0.0.1:38000;
        }

        server {
             # bind loopback address or interface card address
             listen       127.0.0.1:30000;
             listen       172.16.41.103:30000;

             # 10.0.0.0?10.255.255.255(24ビットブロック)
             # 172.16.0.0?172.31.255.255(20ビットブロック)
             # 192.168.0.0?192.168.255.255(16ビットブロック)

             # allow 10.0.0.0/8;             # class A
             # allow 172.16.0.0/12;          # class B
             # allow 192.168.0.0/16;         # class C
             # allow 172.16.41.103/32;       # sample
             # allow 127.0.0.1/32;           # sample
             allow   172.16.42.0/24;
             deny    all;

             proxy_connect_timeout 60s;
             proxy_timeout 10m;
             proxy_pass tcp_backend;
             # access_log  /var/log/nginx/access.log;
             error_log  /var/log/nginx/error.log;
        }
}








HULFT10 Smart Proxy(WSS)のtcp raw待ち受けポートへ接続



loopback, interface cardを指定
Web サーバー(Nginx)のstream TCP 受付ポート番号


ipv4 プライベートアドレス








ipv4 プライベートアドレス(172.16.42.0/24)の範囲を許可
これ以外は拒否







複数のWeb APIまたはWSSへの振り分け

Nginxのロードバランサー機能を使い、複数台のHULFT10 Smart Proxy(Web API)またはHULFT10 Smart Proxy(WSS)へ振り分けます。

 

設定ファイル(/(導入ディレクトリー)/nginx/conf/nginx.conf)

#
# smartproxy - destination raw tcp port to wss-server
#
stream {
    # bind to wss-server tcp raw port
    upstream tcp_backend {
        server       sp-wss-server-001:30001;
        server       sp-wss-server-003:30001;
    }

    server {
        # -------------------
        # bind loopback address or interface card address
        # -------------------
        listen       0.0.0.0:40000;

        # 10.0.0.0?10.255.255.255(24ビットブロック)
        # 172.16.0.0?172.31.255.255(20ビットブロック)
        # 192.168.0.0?192.168.255.255(16ビットブロック)

        # allow 10.0.0.0/8;         # class A
        # allow 172.16.0.0/12;      # class B
        # allow 192.168.0.0/16;     # class C
        # allow 172.16.41.103/32;   # sample
        # allow 127.0.0.1/32;       # sample
        allow   192.168.0.0/16;
        allow   172.0.0.0/8;
        deny    all;

        proxy_connect_timeout 60s;
        proxy_timeout 10m;
        proxy_pass tcp_backend;
        # access_log  /var/log/nginx/access.log;
        error_log  /var/log/nginx/error.log;
    }
}






upstreamに別マシンのHULFT10 Smart Proxy(WSS)を複数台列挙
接続先は、tcp rawポート







tcp rawを受けポート (40000) にてlisten
Web サーバー(Nginx)のstream TCP 受付ポート番号















受け付けた要求は、proxy_pass 構文にて、tcp_backend へ渡す




#
# smartproxy
#  destination https/websocket to wss-server or api-server
#  inner/output
#
# export port     - 8443
# api-server port - 8080 and uri pass /api
#  ex. https://example.com/api
# wss-server port - 8081 and uri pass /wss
#  ex. https://example.com/wss
#
http {
    include       mime.types;
    default_type  application/octet-stream;

    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }

    #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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    # bind to api-server
    upstream sp-api-server {
        server       sp-api-server-001:41000;
        server       sp-api-server-003:41000;
    }
    # bind to wss-server
    upstream sp-wss-server {
        server       sp-wss-server-001:42000;
        server       sp-wss-server-003:42000;
    }

    # HTTPS server
    server {
        listen 443 ssl;
        server_name  localhost;

        ssl_certificate      cert.pem;
        ssl_certificate_key  cert.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        access_log /var/log/nginx/access.log;
        error_log  /var/log/nginx/error.log;

            location / {
                    root   html;
                    index  index.html index.htm;
            }

        # bind to api-server port
        location /api/ {
            proxy_pass   https://sp-api-server;
        }
        # bind to wss-server port
        location /wss/ {
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_pass   https://sp-wss-server/;
        }
    }
}




































upstream に別マシンのHULFT10 Smart Proxy(Web API)を複数台列挙
接続先は、HULFT10 Smart Proxy(Web API)の公開ポート



upstream に別マシンのHULFT10 Smart Proxy(WSS)を複数台列挙
接続先は、HULFT10 Smart Proxy(WSS)の https(wss) 公開ポート





https 標準待ち受けポート (443) にて listen し、ssl を有効化


証明書を指定
秘密鍵を指定












REST APIの待ち受け設定は、/api/ にて受け付け
受け付けた内容は、proxy_pass 構文を使い、転送


WebSocketの待ち受け設定は、/wss/にて受け付け
WebSocketの場合は、http version 指定と upgrade を指定
受け付けた内容は、proxy_pass 構文を使い、転送