一、Nginx 服务器
Nginx 是一款高性能的HTTP和反向代理服务器。在高连接并发的情况下,能够支持高达5万个并发连接
数的响应,而内存、CPU等系统资源消耗却非常低,运行非常稳定。
1.1节点规划
| 节点名称 | 节点ip | 作用 |
|---|---|---|
| nginx-server | 10.1.8.10/24 | 搭建nginx |
| nginx-client | 10.1.8.11/24 | 客户端访问 |
1.2 Nginx 部署
bash
# 安装文件包
[root@nginx-server ~ 11:45:23]# yum install -y wget
[root@nginx-server ~ 11:47:25]# yum install -y nginx
[root@nginx-server ~ 11:51:03]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
--2026-04-20 11:51:29-- http://mirrors.aliyun.com/repo/epel-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 222.186.17.201, 221.229.77.35, 117.85.69.32, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|222.186.17.201|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 664 [application/octet-stream]
Saving to: '/etc/yum.repos.d/epel.repo'
100%[==================================================================>] 664 --.-K/s in 0s
2026-04-20 11:51:29 (90.7 MB/s) - '/etc/yum.repos.d/epel.repo' saved [664/664
# 启动 nginx
[root@nginx-server ~ 12:26:44]# systemctl enable nginx --now
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
# 准备主页
[root@nginx-server ~ 12:27:00]# mv /usr/share/nginx/html/index.html{,.ori}
[root@nginx-server ~ 12:27:47]# echo bukeyiwanshui From Nginx > /usr/share/nginx/html/index.html.ori
# 防火墙
[root@nginx-server ~ 12:28:04]# firewall-cmd --add-service=http --permanent
FirewallD is not running
[root@nginx-server ~ 12:28:21]# firewall-cmd --reload
FirewallD is not running
测试
bash
[root@client ~ 12:30:11]# curl http://www.jiang.cloud
# windows客户端修改 C:\Windows\System32\drivers\etc\hosts
# Linux或Unix修改 /etc/hosts
# 添加如下记录
10.1.8.10 www.jiang.cloud
二、Nginx 配置
nginx的配置文件/etc/nginx/ngnix.conf
2.1 配置结构
Nginx 配置采用层级化、模块化的组织方式,整体是 "全局块 → 核心模块块 → 业务模块块" 的嵌套结
构。
2.1.1 全局配置块
作用于 Nginx 整个进程的基础配置,不嵌套在任何块内,是配置文件的 "根级别"。
bash
# 全局配置示例
user nginx;
worker_processes auto;
# 运行Nginx的用户/用户组
# 工作进程数(核心参数,建议设为CPU核心数)
error_log /var/log/nginx/error.log; # 错误日志路径
pid /run/nginx.pid;
# 主进程PID文件路径
include /usr/share/nginx/modules/*.conf; # 加载外部模块配置(全局级引入)
2.1.2 核心模块 块
Nginx 的核心功能模块 events 块,用于处理网络连接相关配置。
bash
events {
worker_connections 1024; # 每个工作进程的最大并发连接数
use epoll; # 事件驱动模型(epoll是Linux下高性能选择)
multi_accept on; # 允许一个进程一次性接受多个新连接
}
2.1.3 业务模块 块
处理具体业务的核心配置块,最核心的是 http 块(HTTP/HTTPS 服务),可以包含多个server (虚拟主机)。
bash
# http块:所有HTTP/HTTPS服务的公共配置,可嵌套多个server块
http {
# HTTP全局公共配置
include
server 块
/etc/nginx/mime.types; # 加载MIME类型映射
default_type application/octet-stream; # 默认响应类型
log_format main '$remote_addr - $remote_user [$time_local] "$request"'; #
日志格式
access_log /var/log/nginx/access.log main; # 访问日志
sendfile
on; # 高效文件传输开关
keepalive_timeout 65; # 长连接超时时间
# server块:虚拟主机配置(一个http块可包含多个server)
server {
listen
80; # 监听端口(80=HTTP,443=HTTPS)
server_name localhost; # 域名/IP(可配置多个,用空格分隔)
root
/usr/share/nginx/html; # 网站根目录
# location块:URL路径匹配规则(一个server块可包含多个location)
location / {
index index.html index.htm; # 默认首页
try_files $uri $uri/ /index.html; # 路径匹配规则
}
# 错误页面配置
error_page 404
/404.html;
error_page 500 502 503 504 /50x.html;
}
# 第二个虚拟主机(示例)
server {
listen
8080;
server_name test.example.com;
# ... 其他配置
}
}
三、虚拟主机
同一个 web 服务器提供多个站点。
虚拟主机支持多种方式:
- 主机名
- 端口号
- IP地址(基本不用)
3.1 根据名称
bash
[root@nginx-server nginx 14:09:54]# cp /etc/nginx/nginx.conf /etc/nginx/conf.d/vhost-name.conf
[root@nginx-server nginx 14:10:45]# vim /etc/nginx/conf.d/vhost-name.conf
bash
server {
server_name web1.jiang.cloud;
root /usr/share/nginx/web1;
}
server {
server_name web2.jiang.cloud;
root
}
bash
[root@nginx-server nginx 14:15:40]# mkdir /usr/share/nginx/web{1,2}
[root@nginx-server nginx 14:16:27]# echo web1.jiang.cloud > /usr/share/nginx/web1/index.html
[root@nginx-server nginx 14:17:00]# echo web1.jiang.cloud > /usr/share/nginx/web2/index.html
[root@nginx-server nginx 14:17:07]# systemctl restart nginx
客户端测试
bash
# 配置名称解析,假设web服务器ip地址为10.1.8.10
[root@nginx-client ~ 11:33:55]# vim /etc/hosts
10.1.8.10 web1.jiang.cloud
10.1.8.10 web2.jiang.cloud
[root@nginx-client ~ 14:18:10]# curl http://web1.jiang.cloud
web1.jiang.cloud
[root@nginx-client ~ 14:18:23]# curl http://web2.jiang.cloud
web1.jiang.cloud
提示:清理环境,避免影响后续实验。
bash
[root@nginx-client ~ 14:20:58]# mkdir /etc/nginx/conf.d/vhosts
mkdir: cannot create directory '/etc/nginx/conf.d/vhosts': No such file or directory
[root@nginx-client ~ 14:21:14]# mv /etc/nginx/conf.d/vhost-name.conf /etc/nginx/conf.d/vhosts
mv: cannot stat '/etc/nginx/conf.d/vhost-name.conf': No such file or directory
四、根据 port
bash
[root@nginx-server nginx 14:17:16]# vim /etc/nginx/conf.d/vhost-name.conf
server {
listen 8081;
server_name www.laogao.cloud;
root /usr/share/nginx/8081;
}
server {
listen 8082;
server_name www.laogao.cloud;
root /usr/share/nginx/8082;
}
bash
[root@nginx-server nginx 14:51:32]# mkdir /usr/share/nginx/808{1,2}
[root@nginx-server nginx 14:52:42]# echo 80821 > /usr/share/nginx/8081/index.html
[root@nginx-server nginx 14:53:06]# echo 8081 > /usr/share/nginx/8081/index.html
[root@nginx-server nginx 14:53:25]# echo 8082 > /usr/share/nginx/8082/index.html
[root@nginx-server nginx 14:53:37]# systemctl restart nginx
客户端测试
bash
[root@nginx-client ~ 14:55:12]# curl http://www.jiang.cloud:8081
8081
[root@nginx-client ~ 15:07:25]# curl http://www.jiang.cloud:8082
8082
提示:清理环境,避免影响后续实验。
bash
[root@nginx-server nginx 15:12:33]# mv /etc/nginx/conf.d/vhost-port.conf /etc/nginx/conf.d/vhosts
五、配置 SSL/TLS
5.1 生成证书
bash
#--1--生成私钥
[root@nginx-server nginx 15:12:41]# mkdir certs && cd certs
[root@nginx-server certs 15:26:04]# openssl genrsa -out www.key 2048
Generating RSA private key, 2048 bit long modulus
.........................................+++
..........................+++
e is 65537 (0x10001)
#--2--生成请求文件csr
[root@nginx-server certs 15:26:54]# openssl req -new -key www.key -out www.csr -subj "/C=CN/ST=JS/L=NJ/O=LG/OU=DEVOPS/CN=www.jiang.cloud/emailAddress=webadmin@jiang.cloud"
# CN的值必须是网站域名
#--3--使用自己的私钥对请求文件签名,以生成证书
[root@nginx-server certs 15:28:06]# openssl x509 -req -days 3650 -in www.csr -signkey www.key -out www.crt [root@www certs]# mkdir /etc/ssl/cer
- www.key 私钥(必须保密)
- www.csr 证书请求(中间文件)
- www.crt 公钥证书(配置 HTTPS 用)
5.1 配置站点
bash
[root@nginx-server certs 15:30:34]# mkdir /etc/ssl/certs/www.jiang.cloud
[root@nginx-server certs 15:31:03]# mv www* /etc/ssl/certs/www.jiang.cloud
# 参照默认配置修改
[root@nginx-server certs 15:31:20]# cp /etc/nginx/nginx.conf /etc/nginx/conf.d/vhost www.jiang.cloud-ssl.confcp: target 'www.jiang.cloud-ssl.conf' is not a directory
[root@nginx-server certs 15:31:40]# vim /etc/nginx/conf.d/vhost-www.jiang.cloud-ssl.conf
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.jiang.cloud;
root /usr/share/nginx/html;
# 证书
ssl_certificate "/etc/ssl/certs/www.jiang.cloud/www.crt";
# 私钥
ssl_certificate_key "/etc/ssl/certs/www.jiang.cloud/www.key";
}
[root@nginx-server certs 15:32:36]# systemctl restart nginx
配置HTTP重定向到https
bash
[root@nginx-server ~ 15:33:05]# vim /etc/nginx/conf.d/vhost-www.jiang.cloud-ssl.conf
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.jiang.cloud;
root /usr/share/nginx/html;
# 证书
ssl_certificate "/etc/ssl/certs/www.jiang.cloud/www.crt";
# 私钥
ssl_certificate_key "/etc/ssl/certs/www.jiang.cloud/www.key";
}
# 配置HTTP重定向到https
server {
listen 80;
listen [::]:80;
server_name www.jiang.cloud;
root /usr/share/nginx/html;
return 301 https://$host$request_uri;
}
[root@nginx-server ~ 15:36:18]# firewall-cmd --add-service=https --permanent
FirewallD is not running
[root@nginx-server ~ 15:36:29]# firewall-cmd --reload
FirewallD is not running
# 测试
[root@nginx-client ~ 15:43:21]# curl http://www.jiang.cloud/
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
# 使用-k指明目标站点不是一个安全站点
[root@nginx-client ~ 15:43:25]# curl -k https://www.jiang.cloud/
bukeyiwanshui From Nginx
# 使用-L指明跟随重定向
[root@nginx-client ~ 16:07:26]# curl -Lk https://www.jiang.cloud/
bukeyiwanshui From Nginx
六、配置基本认证
用户名和密码使用plain text发送,所以最好配置SSL/TLS。
bash
#安装工具
[root@nginx-server ~ 16:06:55]# yum -y install httpd-tools
[root@nginx-server ~ 16:15:40]# vim /etc/nginx/conf.d/vhost-www.jiang.cloud-ssl.conf
# add into the [server] section
server {
.....
location /auth-basic/ {
auth_basic "Basic Auth";
auth_basic_user_file "/etc/nginx/.htpasswd";
}
}
# 加完效果
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.jiang.cloud;
root /usr/share/nginx/html;
# 证书
ssl_certificate "/etc/ssl/certs/www.jiang.cloud/www.crt";
# 私钥
ssl_certificate_key "/etc/ssl/certs/www.jiang.cloud/www.key";
location /auth-basic/ {
auth_basic "Basic Auth";
auth_basic_user_file "/etc/nginx/.htpsswd"
}
}
server {
listen 80;
listen [::]:80;
server_name www.jiang.cloud;
root /usr/share/nginx/html;
return 301 https://$host$request_uri;
}
[root@nginx-server ~ 16:20:10]# systemctl restart nginx
# add user for Basic authentication
[root@nginx-server ~ 16:22:11]# htpasswd -b -c /etc/nginx/.htpasswd jiang 123456
Adding password for user jiang
[root@nginx-server ~ 16:23:28]# mkdir /usr/share/nginx/html/auth-basic
[root@nginx-server ~ 16:23:51]# vim /usr/share/nginx/html/auth-basic/index.html
# 测试,通过-u选项指定用户名和密码
[root@nginx-client ~ 16:32:18]# curl -ku jiang:123456 https://www.jiang.cloud/auth-basic/
test