0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

nginx tcpフォワード(Almalinux)

Last updated at Posted at 2023-01-13

背景

Almalinuxのdnfでインストールされるnginxにはtcpフォワード(stream)の機能がない。
使用したい場合はソースコードからビルドする必要がある。

前提・環境

OS:AlmaLinux 9.1
本作業はrootユーザで行う
クローズドの環境ではないこと
ファイアーウォールおよびSELinuxは考慮しないものとする
受信ポートを8000/tcp、転送先を192.168.1.1の10000/tcpとする

パッケージのインストール

必要なパッケージをインストールする

dnf -y install wget tar gcc pcre-devel zlib-devel

インストールするパッケージについて

パッケージ 用途
wget nginxの公式ページからソースコードをDLするため
tar ソースコード(tar.gz)を展開するため
gcc ソースコードをビルドする場合に必要
pcre-devel ソースコードをビルドする場合に必要
zlib-devel ソースコードをビルドする場合に必要

nginxのダウンロード

 ダウンロードサイト

wgetでダウンロード

ダウンロードするバージョンはStable versionを対象とする

wget http://nginx.org/download/nginx-1.22.1.tar.gz

ビルド

展開する

tar zxvf nginx-1.22.1.tar.gz

展開先に移動する

cd nginx-1.22.1

コンパイルするために"Makefile"を作成する

./configure --with-stream --without-stream_geo_module --without-stream_map_module

コンパイルおよびインストール

make && make install

インストール先に移動

cd /usr/local/nginx/
ls -l

["ls-l"の結果]
drwxr-xr-x 3 root root 4096  1月 13 01:13 conf
drwxr-xr-x 2 root root 4096  1月 13 00:07 html
drwxr-xr-x 2 root root 4096  1月 14 00:45 logs
drwxr-xr-x 2 root root 4096  1月 13 00:07 sbin

tcpフォワード(stream)の設定

コンフィグファイル編集

cd conf
cp -p nginx.conf nginx.conf.org
vi nginx.conf

編集画面が表示されるので以下の内容を最終行に追加する

stream {
   upstream testserver {
      server 192.168.100.1:10000;
   }

   server {
      listen       0.0.0.0:8000;
      proxy_pass   testserver;
   }
}

nginx起動

cd /usr/local/nginx/sbin
nginx -c /usr/local/nginx/conf/nginx.conf

ポートの確認

ss -nate | grep ":8000" | grep LISTEN

「0.0.0.0:8000」が表示されていることを確認する。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?