LoginSignup
2
0

C++で実装したNginx moduleをビルドして、macのローカルのNginxで動作させる

Last updated at Posted at 2023-07-15

概要

C++で実装したNginx moduleをビルドして、macローカルのNginxで登録したEPにリクエストして「Hello World」と表示させる。

前提

macにhomebrewでNginxをインストールしておく

(参考)

実行環境

mac(ローカル)

手順

処理を記述したファイルを用意

実行環境に実装したconfigファイルやcppファイルを用意する

実装についてのメモ

実装内容の詳細はいいのでとりあえず試したい人は以下からgit cloneする

# cloneする
$ git clone https://github.com/takarin0711/nginx-hello.git

Nginxパッケージを用意してビルドする

モジュールをビルドするためにNginxパッケージを用意する

実行するNginxと同じバージョンのNginxソースコードを取得、展開

# macに導入したNginxのバージョンを確認する
$ nginx -v
nginx version: nginx/1.25.1

# 同じバージョンのNginxパッケージを用意する
$ curl http://nginx.org/download/nginx-1.25.1.tar.gz -O

# 解凍
$ tar zxvf nginx-1.25.1.tar.gz

cofigure

$ cd nginx-1.25.1

$ ./configure --add-dynamic-module=<configやcppファイルが置いてあるディレクトリへのパス> --with-http_realip_module --with-http_ssl_module --with-compat

# 例
$ ./configure --add-dynamic-module=/Users/hogeuser/nginx-hello --with-http_realip_module --with-http_ssl_module --with-compat

(補足)

  • 「--with-http_realip_module --with-http_ssl_module --with-compat」オプションをつけないとシグネチャが合わなくエラーになってしまう
  • シグネチャの値については、https://heartbeats.jp/hbblog/2017/02/nginx-dynamic-modules-201702.html を参照
  • Nginxのバージョンとかによって、シグネチャの値が異なる場合があるので、適宜オプションを追加する

(その他)
ここでopensslのエラーが出た場合は以下の対応を参考にしてみると良いかも

モジュールビルド

$ cd nginx-1.25.1

# モジュールをビルドする
$ make modules

# ビルドが成功したらsoファイルが作成されている
$ ls -la objs/ngx_http_hello_module.so

# シグネチャの値が一致していれば問題なし
# macにインストールしたNginx
$ strings /usr/local/bin/nginx | grep '^.,.,.,'
8,4,8,1011000111010111001101111111111111

# 今回ビルドしたNginx
$ strings objs/ngx_http_hello_module.so | grep '^.,.,.,'
8,4,8,1011000111010111001101111111111111

作ったモジュールをNginxに読み込ませて設定

nginx.confで該当のsoファイルを読み込ませるように記述

$ sudo vim /usr/local/etc/nginx/nginx.conf

worker_processes  4;

error_log /usr/local/var/log/nginx/error.log error;

worker_rlimit_nofile 32;

load_module <soファイルが置いてある場所へのパス>; # ←追加
# 例: load_module /Users/hogeuser/nginx-1.25.1/objs/ngx_http_hello_module.so;

events {
    worker_connections  10;
}

http {
    include mime.types;
    default_type application/octet-stream;
------------(省略)-------------

EPを作成

$ sudo vim /usr/local/etc/nginx/nginx.conf

------------(省略)-------------
    server {
        listen 8080;

        server_name localhost;

        charset utf-8;

        access_log /usr/local/var/log/nginx/localhost.access.log  main;

        error_page 404 /404.html;

        location = / {
            root /usr/local/var/www;
            index index.html;
        }
        location /articles/ {
            root /usr/local/var/www;
        }
        location ~* \.(gif|jpg|jpeg|png)$ {
            root /usr/local/var/www/images;
        }
        location = /test {                        # ←追加
            proxy_pass http://qiita.com;          # ←追加
            hello 'Hello World';                  # ←追加
        }                                         # ←追加
    }
}

Nginx再起動

# 構文チェックしてエラーが出ないことを確認
$ sudo nginx -t
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful

$ Nginx再起動
$ sudo nginx -s reload

動作確認

作ったEPに対してcurlしてレスポンスが「Hello World」になっていること

$ curl localhost:8080/test
Hello World%

参考

2
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
2
0