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?

[1分でわかる]nginxってなんだ?

Posted at

一言で言うと

高速・軽量なWebサーバー。リバースプロキシ、ロードバランサーとしても使う。


nginxとは

nginx(エンジンエックス)

2004年にロシアのIgor Sysoev氏が開発。
「C10K問題」(1万同時接続)を解決するために生まれた。

現在、世界で最も使われているWebサーバーの一つ。
Apache、Microsoft IISと並ぶ三大Webサーバー。

nginxの3つの役割

┌─────────────────────────────────────────────────────────┐
│                                                         │
│  ① Webサーバー                                         │
│     → HTML、CSS、JS、画像などの静的ファイルを配信     │
│     → 非常に高速                                       │
│                                                         │
│  ② リバースプロキシ                                    │
│     → クライアントの代わりに背後のサーバーにアクセス  │
│     → Gunicorn/uWSGI の前に置く                       │
│                                                         │
│  ③ ロードバランサー                                    │
│     → 複数のサーバーに負荷を分散                      │
│     → 大規模サイトで必須                              │
│                                                         │
└─────────────────────────────────────────────────────────┘

図解:nginxの立ち位置

【静的ファイル配信】
ブラウザ ──→ nginx ──→ HTML/CSS/JS/画像

【動的コンテンツ(Python)】
ブラウザ ──→ nginx ──→ Gunicorn ──→ Flask/Django

【ロードバランサー】
              ┌──→ サーバー1
ブラウザ ──→ nginx ──┼──→ サーバー2
              └──→ サーバー3

なぜGunicornの前にnginxを置く?

┌─────────────────────────────────────────────────────────┐
│                                                         │
│  ① 静的ファイルを高速配信                              │
│     → Gunicornより圧倒的に速い                        │
│     → Pythonを経由しないので効率的                    │
│                                                         │
│  ② セキュリティ                                        │
│     → Gunicornを直接インターネットに晒さない          │
│     → DDoS対策、レート制限などが可能                  │
│                                                         │
│  ③ SSL/TLS終端                                         │
│     → HTTPSの処理をnginxで行う                        │
│     → Let's Encrypt + Certbot との相性◎              │
│                                                         │
│  ④ バッファリング                                      │
│     → 遅いクライアントからの接続をnginxが受け持つ     │
│     → Gunicornのワーカーを効率的に使える             │
│                                                         │
└─────────────────────────────────────────────────────────┘

インストール

# Ubuntu / Debian
sudo apt update
sudo apt install nginx

# CentOS / RHEL
sudo dnf install nginx

# Mac (Homebrew)
brew install nginx

# 起動
sudo systemctl start nginx
sudo systemctl enable nginx  # 自動起動

基本的な設定(Flask + Gunicorn)

# /etc/nginx/sites-available/myapp
server {
    listen 80;
    server_name example.com;

    # 静的ファイル
    location /static/ {
        alias /var/www/myapp/static/;
    }

    # 動的コンテンツ → Gunicornへ転送
    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
# 設定を有効化
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t  # 設定テスト
sudo systemctl reload nginx

語呂合わせ

「nginx = エンジン X(クロス)で高速配信」

エンジン全開で、クロス(交差)する大量のリクエストを高速処理。


Apacheとの違い

┌──────────────────┬─────────────────┬─────────────────┐
│                  │ nginx           │ Apache          │
├──────────────────┼─────────────────┼─────────────────┤
│ アーキテクチャ   │ イベント駆動    │ プロセス/スレッド│
├──────────────────┼─────────────────┼─────────────────┤
│ 同時接続         │ 得意            │ やや苦手        │
├──────────────────┼─────────────────┼─────────────────┤
│ 静的ファイル     │ 超高速          │ 高速            │
├──────────────────┼─────────────────┼─────────────────┤
│ 設定の柔軟性     │ シンプル        │ .htaccessで柔軟 │
├──────────────────┼─────────────────┼─────────────────┤
│ メモリ使用量     │ 少ない          │ 多い            │
├──────────────────┼─────────────────┼─────────────────┤
│ 学習コスト       │ やや高い        │ 資料が豊富      │
└──────────────────┴─────────────────┴─────────────────┘

メリット・デメリット

┌─────────────────────────────────────────────────────────┐
│ 【メリット】                                            │
├─────────────────────────────────────────────────────────┤
│ ・大量の同時接続を少ないメモリで処理                   │
│ ・静的ファイル配信が超高速                             │
│ ・リバースプロキシ、ロードバランサーとして優秀         │
│ ・設定がシンプルで読みやすい                           │
└─────────────────────────────────────────────────────────┘

┌─────────────────────────────────────────────────────────┐
│ 【デメリット】                                          │
├─────────────────────────────────────────────────────────┤
│ ・.htaccess のような動的設定変更ができない             │
│ ・Apacheに比べてモジュールが少ない                     │
│ ・初心者には設定がやや難しい                           │
└─────────────────────────────────────────────────────────┘

まとめ

項目 内容
読み方 エンジンエックス
役割 Webサーバー、リバースプロキシ、ロードバランサー
強み 高速、軽量、大量同時接続
定番構成 nginx + Gunicorn + Flask/Django
設定ファイル /etc/nginx/nginx.conf

次回は「Apache」─ 歴史あるWebサーバーの王様

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?