Nginx のソースからの構築とカスタム設定ガイド

本ガイドでは、Nginx をソースコードからビルド・インストールする手順を詳細に解説します。必要な依存ライブラリの準備からコンパイル、設定ファイルの最適化まで、実稼働環境向けの構成を紹介します。

前提となる開発ツールのインストール

Nginx をソースからビルドするには、以下のツール群が必須です。

yum -y install gcc-c++ make autoconf automake zlib-devel openssl-devel

Debian/Ubuntu 系ディストリビューションでは代わりに以下を使用:

sudo apt update && sudo apt install build-essential libtool zlib1g-dev libssl-dev

PCRE ライブラリの構築

URL リライト機能を利用するため、PCRE(Perl Compatible Regular Expressions)を事前にインストールします。

  1. ソース取得:
    wget https://sourceforge.net/projects/pcre/files/pcre/8.45/pcre-8.45.tar.gz
    tar -zxvf pcre-8.45.tar.gz
    cd pcre-8.45
    
  2. コンフィグレーションとインストール:
    ./configure --prefix=/opt/lib/pcre
    make && sudo make install
    

OpenSSL のカスタムビルド

セキュアな通信を実現するため、OpenSSL を独自にビルドします。

wget https://www.openssl.org/source/openssl-1.1.1k.tar.gz
tar -zxvf openssl-1.1.1k.tar.gz
cd openssl-1.1.1k
./config --prefix=/opt/lib/openssl --openssldir=/opt/lib/openssl shared
make && sudo make install

Nginx のビルドとインストール

  1. ダウンロードと展開:
    wget http://nginx.org/download/nginx-1.20.1.tar.gz
    tar -zxvf nginx-1.20.1.tar.gz
    cd nginx-1.20.1
    
  2. コンパイルオプションの指定:
    ./configure \
      --prefix=/home/nginx \
      --with-http_ssl_module \
      --with-http_stub_status_module \
      --with-pcre=/home/daokr/downfile/pcre-8.33 \
      --with-openssl=/home/daokr/downfile/openssl-1.0.2h \
      --user=www \
      --group=www \
      --sbin-path=/home/nginx/sbin/nginx \
      --conf-path=/home/nginx/conf/nginx.conf
    
    注意: --with-pcre および --with-openssl は、圧縮ファイルを展開した「ソースディレクトリ」を指定してください。インストール先パスではありません。
  3. ビルド実行:
    make
    sudo make install
    

実行ユーザーの設定

セキュリティ向上のため、専用ユーザで Nginx を動作させます。

sudo groupadd www
sudo useradd -g www -s /sbin/nologin -M www

高性能な nginx.conf 設定例

以下は、複数のアプリケーションサーバーをホストする本番環境向けの設定です。

worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
pid /home/nginx/logs/nginx.pid;

worker_rlimit_nofile 65535;

events {
    use epoll;
    worker_connections 10240;
}

http {
    include mime.types;
    default_type application/octet-stream;
    server_tokens off;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 60;

    client_header_buffer_size 4k;
    large_client_header_buffers 4 32k;
    client_max_body_size 64m;

    open_file_cache max=10240 inactive=20s;
    open_file_cache_valid 60s;
    open_file_cache_min_uses 1;

    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 64k;
    gzip_comp_level 2;
    gzip_types text/plain application/x-javascript text/css application/xml;
    gzip_vary on;

    log_format access '$remote_addr $http_x_forwarded_for $remote_user [$time_local] "$request" [$request_length/$bytes_sent] $status "$http_referer" "$http_user_agent"';
    access_log logs/access.log access;
    error_log /home/nginx/logs/error.log error;

    # 各サービスの仮想サーバー設定
    include server/*.conf;
}

個別サービスの設定(例)

/home/nginx/conf/server/xapp.conf の内容:

server {
    listen 20037 ssl;
    server_name localhost;
    charset utf-8;

    ssl_certificate ssl/boshang.crt;
    ssl_certificate_key ssl/boshang.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1.2;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    set $root_path "/home/oracle/xapp-1.0.0/public";
    root $root_path;
    index index.php index.html;

    location / {
        try_files $uri $uri/ @rewrite;
    }

    location @rewrite {
        rewrite ^/(.*)$ /index.php?_url=$1 last;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/tmp/php-cgi.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location ~* \.(js|css|png|jpg|gif|ico|woff|ttf)$ {
        expires 30d;
        access_log off;
    }

    location ~ /\.ht {
        deny all;
    }
}

基本操作コマンド

  • 設定テスト:
    /home/nginx/sbin/nginx -t
  • 起動:
    /home/nginx/sbin/nginx
  • 再読み込み:
    /home/nginx/sbin/nginx -s reload
  • 停止:
    /home/nginx/sbin/nginx -s stop

TCPプロキシ機能の活用

Stream モジュールを使用して、データベースなどの TCP サービスをプロキシ可能です。

stream {
    upstream oracle_backend {
        server localhost:21521;
    }

    server {
        listen 11521;
        proxy_pass oracle_backend;
        proxy_timeout 1d;
        proxy_responses 1;
    }
}

タグ: nginx ソースビルド PCRE OpenSSL 設定最適化

8月27日 04:52 投稿