기본 인증 및 접근 제어
Nginx 리소스를 보호하도록 기본 HTTP 인증과 IP 기반 접근 제어를 설정해 보세요.
기본 인증 및 접근 제어은(는) CoddyKit의 무료 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 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 passwordNginx 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
.htpasswdfile, configured withauth_basicandauth_basic_user_file. Best used with HTTPS. - IP-based Access Control: Filters requests based on IP addresses using the
allowanddenydirectives. Order of these rules is crucial.
These methods provide foundational security for your Nginx server and the applications it serves.
자주 묻는 질문
“기본 인증 및 접근 제어” 강의는 무료인가요?
네 — “기본 인증 및 접근 제어” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 강의 전체를 잠금 해제할 수 있습니다. API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 강의에는 총 4개의 강의가 포함되어 있습니다.
“기본 인증 및 접근 제어”에서 뭘 배우나요?
Nginx 리소스를 보호하도록 기본 HTTP 인증과 IP 기반 접근 제어를 설정해 보세요. 브라우저에서 직접 실행하는 실습 코드로 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.
“기본 인증 및 접근 제어” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.