0Pricing
API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) · 课时

Nginx 基础配置

了解 Nginx 主配置文件的结构,以及用于简单 Web 内容提供的基本指令。

Nginx 基础配置 是 CoddyKit 上的免费 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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 基础配置」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 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),全天候 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 位置块与请求匹配
← 返回 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)