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

基本身份验证与访问控制

设置基本的 HTTP 身份验证和基于 IP 的访问控制,以保护 Nginx 资源。

第 3 / 4 课11 个步骤

基本身份验证与访问控制 是 CoddyKit 上的免费 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Protecting Nginx Resources

When Nginx serves content or acts as a proxy, it's crucial to control who can access your resources. This helps prevent unauthorized access and keeps your applications secure.

In this lesson, we'll explore two fundamental ways to secure Nginx: Basic HTTP Authentication and IP-based Access Control.

Basic HTTP Authentication

Basic HTTP Authentication is a simple way to protect web resources using a username and password. When a user tries to access a protected resource, their browser will prompt them to enter credentials.

  • The browser sends credentials with each request.
  • Nginx verifies these against a stored file.
  • It's straightforward but sends credentials as base64 encoded text (not encrypted), so always use it with HTTPS!

Creating a Password File

To use basic authentication, Nginx needs a file containing usernames and encrypted passwords. We use the htpasswd utility, which is typically part of the Apache utilities package.

Here's how to create or add users to a password file:

sudo apt install apache2-utils  # On Debian/Ubuntu
sudo yum install httpd-tools   # On CentOS/RHEL

sudo htpasswd -c /etc/nginx/.htpasswd coddyuser
# -c creates the file if it doesn't exist
# You'll be prompted to enter and confirm password

Nginx Basic Auth Configuration

Once you have your password file, you can configure Nginx to use it. You'll use two main directives:

  • auth_basic: Sets the realm (a message shown in the login prompt).
  • auth_basic_user_file: Specifies the path to your password file.

These directives can be placed in http, server, or location blocks.

Example: Basic Auth with Nginx

This Nginx configuration snippet protects the /admin path, requiring users to authenticate with credentials from the .htpasswd file.

http {
    # ... other http settings

    server {
        listen 80;
        server_name example.com;

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

        location /admin {
            auth_basic "Restricted Area";
            auth_basic_user_file /etc/nginx/.htpasswd;
            root /var/www/admin;
            index index.html;
        }
    }
}

IP-based Access Control

Another way to secure resources is by controlling access based on the client's IP address. Nginx uses the allow and deny directives for this.

  • allow IP_ADDRESS | CIDR | all: Permits access from specified IPs.
  • deny IP_ADDRESS | CIDR | all: Denies access from specified IPs.

These directives are processed in order within a block. The first matching rule applies.

Configuring IP Access Rules

You can specify single IP addresses, IP ranges using CIDR notation (e.g., 192.168.1.0/24), or all to refer to all IPs. Remember, the order matters!

For example, to allow a specific IP and deny all others, you'd list allow first, then deny all.

Example: IP Access with Nginx

This configuration allows access to /private only from 192.168.1.100 and any IP within the 10.0.0.0/8 network, denying everyone else.

http {
    # ... other http settings

    server {
        listen 80;
        server_name example.com;

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

        location /private {
            allow 192.168.1.100;
            allow 10.0.0.0/8;
            deny all;
            root /var/www/private;
            index index.html;
        }
    }
}

Combining Access Controls

You can combine both basic authentication and IP-based access control for enhanced security. Nginx processes these rules sequentially.

  • First, IP-based rules are evaluated.
  • If access is allowed by IP, then basic authentication is checked.

This means a request must satisfy both sets of rules to gain access.

Quick Check: Nginx Security

Consider the following Nginx configuration snippet. A user with IP 192.168.1.50 tries to access example.com/sensitive. The .htpasswd file contains a valid entry for 'admin'.

What will happen?

location /sensitive {
    deny 192.168.1.0/24;
    allow 192.168.1.50;
    deny all;
    auth_basic "Secure Area";
    auth_basic_user_file /etc/nginx/.htpasswd;
}

Recap: Nginx Access Control

In this lesson, you learned how to protect your Nginx resources using two key methods:

  • Basic HTTP Authentication: Uses username/password stored in an .htpasswd file, configured with auth_basic and auth_basic_user_file. Best used with HTTPS.
  • IP-based Access Control: Filters requests based on IP addresses using the allow and deny directives. Order of these rules is crucial.

These methods provide foundational security for your Nginx server and the applications it serves.

免费开始

用 AI 导师学习 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「基本身份验证与访问控制」课时是免费的吗?

是的 — 「基本身份验证与访问控制」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程的其余内容,请升级到 CoddyKit PRO。 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程共包含 4 节课。

「基本身份验证与访问控制」这节课中我会学到什么?

设置基本的 HTTP 身份验证和基于 IP 的访问控制,以保护 Nginx 资源。 你通过在浏览器中直接运行的动手代码来练习 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) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「基本身份验证与访问控制」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课中编写并运行代码吗?

能。每节 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 使用 SSL/TLS 保护 Nginx
  2. HTTP/2 与 Nginx 优化
  3. 基本身份验证与访问控制
  4. 使用安全标头强化 Nginx
← 返回 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)