0Pricing
API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) · レッスン

Nginxの基本設定

Nginxの主要な設定ファイルの構成と、シンプルなWeb配信に必要な基本ディレクティブを理解します。

「Nginxの基本設定」はCoddyKit上の無料API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)レッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはAPI Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)コースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Nginx Config: The Blueprint

Nginx uses a simple, yet powerful, configuration file to tell it what to do. Think of this file as the blueprint for your web server.

It defines how Nginx handles requests, serves content, and interacts with other services. Mastering this file is key to using Nginx effectively.

Nginx Config: Main Blocks

Nginx configuration is organized into a hierarchical structure using logical blocks. Each block groups related directives.

  • main: Global settings for the entire Nginx process.
  • events: Settings for how Nginx handles connections.
  • http: The core for all HTTP web server functionalities.

Directives inside these blocks control Nginx's specific behaviors.

user  nginx;
worker_processes  auto;

events {
    # Event context directives
}

http {
    # HTTP context directives
    # ... server blocks go here
}

Global Settings: The `main` Context

The main context contains global directives that affect the entire Nginx process. These are foundational settings for Nginx's operation.

  • user: Defines the user and group that Nginx worker processes run as (e.g., nginx or www-data).
  • worker_processes: Sets the number of worker processes. auto is often a good default, matching your CPU cores.
user  nginx;
worker_processes  auto;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

Connection Handling: The `events` Context

The events context configures how Nginx handles network connections. It's about optimizing performance for concurrent clients.

  • worker_connections: The maximum number of simultaneous connections a single worker process can open.

A higher number here allows Nginx to handle more concurrent users efficiently. The use directive (e.g., epoll for Linux) specifies the connection processing method.

events {
    worker_connections  1024;
    use epoll; # Linux-specific, for high performance
}

Web Serving: The `http` Context

The http context is where most of your web server configuration resides. It defines how Nginx processes HTTP requests and responses.

Inside this block, you'll configure things like MIME types, logging, and, most importantly, define individual web servers using server blocks.

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    # ... server blocks will be here ...
}

Defining Websites: `server` Blocks

Inside the http block, you define one or more server blocks. Each server block acts like a "virtual host," allowing Nginx to host multiple websites or applications on the same physical server.

It specifies what port Nginx should listen on and for which domain names it should respond.

http {
    server {
        # This is a server block for one website
    }

    server {
        # Another server block for a different site
    }
}

Server Basics: `listen` & `server_name`

Two crucial directives within a server block are listen and server_name. They tell Nginx how to identify and handle requests for a specific website.

  • listen: Specifies the IP address and port number Nginx should listen on (e.g., 80 for HTTP).
  • server_name: Defines the domain names (hostnames) this server block should respond to (e.g., example.com www.example.com).
server {
    listen       80;
    server_name  example.com www.example.com;

    # ... location blocks will be here ...
}

URL Matching: `location` Blocks

Within a server block, location blocks are used to define how Nginx should handle requests for specific URLs or URL patterns.

For instance, you might want to serve static files from one directory for requests to /images/ and handle all other requests differently.

server {
    listen 80;
    server_name mywebsite.com;

    location / {
        # Handle requests to the root path
    }

    location /api/ {
        # Handle requests starting with /api/
    }
}

Serving Files: `root` & `index`

For serving static content like HTML, CSS, and images, root and index are essential directives within a location block.

  • root: Specifies the base directory where Nginx should look for files for requests handled by this location.
  • index: Defines the default file Nginx should serve when a directory is requested (e.g., index.html).

This is how Nginx finds and delivers your website's files.

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

Full Example: Static Web Server

Let's put it all together to create a simple Nginx configuration that serves static HTML files from a specific directory.

This configuration listens on port 80 for requests to mywebsite.com and serves files from the /var/www/html directory.

http {
    include       mime.types;
    default_type  application/octet-stream;

    server {
        listen       80;
        server_name  mywebsite.com;

        location / {
            root   /var/www/html;
            index  index.html index.htm;
        }
    }
}

Quick Check: Nginx Directives

Which of the following Nginx directives are typically found within a server block, or within a location block nested inside a server block?

Recap: Nginx Config Basics

Great job! You've learned the fundamental structure of Nginx configuration, including the main, events, http, server, and location blocks.

You can now configure Nginx to listen on specific ports, respond to domain names, and serve static files effectively.

Next, we'll dive into more advanced Nginx features, including how to configure it as a reverse proxy!

よくある質問

「Nginxの基本設定」レッスンは無料ですか?

はい。「Nginxの基本設定」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)コースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)コースには全4レッスンが含まれています。

「Nginxの基本設定」で何を学びますか?

Nginxの主要な設定ファイルの構成と、シンプルなWeb配信に必要な基本ディレクティブを理解します。 ブラウザで直接実行するハンズオンコードでAPI Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)を演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)を始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのAPI Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)は初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「Nginxの基本設定」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このAPI Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)レッスンでコードを書いて実行できますか?

はい。すべてのAPI Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)レッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Nginxのインストールとセットアップ
  2. Nginxの基本設定
  3. Nginxで静的コンテンツを配信する
  4. Nginxのlocationブロックとリクエストマッチング
← API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)に戻る