LoginSignup
35
40

More than 5 years have passed since last update.

nginxへようこそ

Posted at

nginxとは

以前webサーバーといえば、Apacheですが、近年nginx急速に増えました。今回入門として、簡単なインストール、設定、構築など、説明します。

C10K問題


nginxは、C10K問題を解決するため、lgorSysoev氏(ロシア人)が開発されました。
C10K問題とは、クライアント 1 万台問題です。ハードウェアの性能上は問題がなくても、クライアント数があまりにも多くなるとサーバがパンクする問題のこと。

特徴


特徴として、軽量、高速、webサーバーとしても、
・リバースプロキシー
・負荷分散、
・URL Rewrite
・WebDAV(webファイル共有)
などの機能もあります。

インストール

cenots
# yum -y install nginx
ubunut
# apt-get install nginx

起動、リロード、終了コマンド


起動:sudo nginx
終了:sudo nginx -s stop
設定リロード:nginx -s reload
起動チェック:nginx -t

設定ファイル


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       mime.types;
    access_log  /var/log/nginx/access.log;
    keepalive_timeout  65;

    location /images/{
       images/に適用したい設定
    }
    gzip  on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

worker_processes、ディレクティブは動作させるnginxのworkerプロセスの数です。通常はCPUのコア数以下に設定します。
error_log、ディレクティブはエラーログの出力先のファイル名とロギングのレベルを指定します。
pid、ディレクティブはmasterプロセスのプロセスIDを保存するファイルを設定します。
worker_connections、一つのworkerプロセスが同時に処理できる最大コネクション数を設定します。デフォルトの設定値は512です。
mime.types、/etc/nginx/mime.typesをincludeします。

/etc/nginx/mime.types
types {
    text/html                             html htm shtml;
    text/css                              css;
    text/xml                              xml;
    image/gif                             gif;
    image/jpeg                            jpeg jpg;
    application/x-javascript              js;
    application/atom+xml                  atom;
    application/rss+xml                   rss;
  ・・・・
}

access_log、アクセスログを指定します。
keepalive_timeout、サーバ側でのkeepaliveのタイムアウトの秒数を設定します。デフォルトの設定値は75sです。
gzip、レスポンスのコンテンツを圧縮するを設定します。
gzip_disable、gzip無視範囲を設定します。

用途


1) リバースプロキシ
n1.jpg

nginx.conf
     upstream  xxx.domain.com  {
            #domino server1 information  in cluster
              server   9.123.159.49:80;      
            #domino server2 information  in cluster                    
              server   9.123.159.153:80;
      }

2) 静的なコンテンツの配信(例:CDN)

nginx.conf
# コンテンツをgzip圧縮する、効率アップ
gzip on;

圧縮より、ネットワークの負荷を下げると同時に、転送量が減りますから、伝達時間(情報がクライアントまでに届く時間)が短くなります。

3) nginx拡張機能
nginxの拡張性があり、nginxと他のlibの連携が結構使われています。
今回いくつのluaとの連携の例します。
※ Lua(ルア)は移植性が高く、高速な実行速度などの特徴を持っている、スクリプト言語です。

rewirteの例
location / {
    rewrite_by_lua '
    local res = ngx.location.capture("/check-spam")
    if res.body == "spam" then
        -- 利用規約ページへ
        ngx.redirect("/terms-of-use.html")
    end
    ';
    fastcgi_pass ...;
}
IP制御の例
location @client{
    proxy_pass  http://www.domain.com;
}

location ~  /test {
    default_type  text/html;
    content_by_lua 'ngx.say("this is domain.com!")';
    access_by_lua '
        -- check the client IP address is in our black list
        if ngx.var.remote_addr == "10.0.0.100" then
            ngx.exit(ngx.HTTP_FORBIDDEN)
        end
        if ngx.var.remote_addr == "10.0.0.112" then
            ngx.exec("@client")
        end
        ';
}

nginxの不向き


CPUのリソース消費が激しいと他の処理ができなくなるため処理能力が一気に落ちてしまいます。

一般的な構築


フロントエンドは nginx
バックエンドは Apache(nginx)

35
40
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
35
40