4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Nginxについて理解を深めてみた

Posted at

はじめに

よくNginxを聞くことはありましたが、あまり理解せずに以下のようなdockerのコマンド練習で使っていました。

docker run --rm -p 8000:80 nginx:latest

そこで理解を深めるためにNginxについて学習してみました。

想定する読者

  • Docker初心者
  • Nginx初心者

NginxについてChatGPTに聞いてみた

NginxについてChatGPTに聞いてみたところ以下の回答が返ってきました。

  • nginx は、軽量かつ高速な Webサーバー/リバースプロキシサーバー です。
    ロシアの開発者 Igor Sysoev によって 2004 年に公開され、現在は F5,inc によって運営され、世界中のウェブサイト(特に高トラフィックなサービス)で利用されています。

主な用途

  1. 静的ファイル配信(HTML、CSS、JavaScript、画像など)

  2. リバースプロキシ(アプリケーションサーバーへの中継)

  3. ロードバランシング

  4. SSL/TLS終端(HTTPS化)

  5. APIゲートウェイ的な役割

  6. キャッシュサーバー

筆者が理解したNginxの役割

ざっくりNginxは、クライアントとwebサーバの仲介役ということ。
以下のようなイメージ。

[ブラウザ] → [Nginxコンテナ] → [静的ファイル or アプリサーバ]

とりあえずdockerで動かしてみる

触った方が早いと思ったためdockerで立ち上げてみることに。

docker run --rm --name test-server -p 8000:80 nginx

http://localhost:8000 へアクセス

いつものようにNginxのページにアクセスできることは、確認できました。

作成したコンテナの中を覗いてみる

まずコンテナの中に入ってみる

docker exec -it test-server bash

するとNginxのコンテナの中に入ることができ、lsをして中に何があるのか見てみると以下のようになりました。

$ docker exec -it test-server bash
root@93e52fd75709:/# ls
bin   dev                  docker-entrypoint.sh  home  media  opt   root  sbin  sys  usr
boot  docker-entrypoint.d  etc                   lib   mnt    proc  run   srv   tmp  var

Nginxが返すデフォルトのページを変えてみた

Nginxの設定ファイルを確認するために /etc/nginx/conf.d/default.conf を見てみることに。

cd /etc/nginx/conf.d
cat default.conf

すると以下の内容が。

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

この部分が初期ページを返している設定みたいです。

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

Nginxが返すページが /usr/share/nginx/html/index.html らしいのでコードを編集して変えてみた。

cdで対象のディレクトリへ移動する。

cd /usr/share/nginx/html/ 
echo "<h1>Hello World</h1>" > index.html

そうして、ブラウザで更新してみると変わりました。
スクリーンショット 2025-08-15 16.03.43.png

今回学んだこと

  • Nginxの大まかな仕組み
  • Dockerコンテナ内のファイルを直接編集することで動作を変えられること
  • デフォルトの静的ファイルは /usr/share/nginx/html に保存されていること

次のステップ

リバースプロキシを設定して、Node.jsと連携させてみたいと思っています。

4
4
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
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?