《Nginx全栈部署与运维实战指南|从环境搭建到反向代理、SSL证书全落地》
**摘要:**本文是一套落地型Nginx运维实战教程,从零梳理Nginx编译/包管理器两种安装方式、配置文件整体架构与加载逻辑。内容全覆盖:多方式虚拟主机(域名+端口)配置、Let’s Encrypt免费SSL证书申请与自动续期、站点基础认证、静态/PHP动态站点部署、KodExplorer网盘项目实战;深挖Location四大匹配优先级(精确/前缀/正则/兜底)、proxy_pass路径易错细节;配套本地+远端反向代理完整实验环境与curl验证案例,所有命令均可直接复制实操,适合运维新人、后端开发者系统化练手Nginx。
一、Nginx 服务器
Nginx 是一款高性能的HTTP和反向代理服务器。在高连接并发的情况下,能够支持高达5万个并发连接数的响应,而内存、CPU等系统资源消耗却非常低,运行非常稳定。
(一)Nginx 部署
# 安装 nginx
[lz@controller webapp 11:18:18]$ sudo yum install -y nginx
# 启动 nginx
[root@controller ~ 11:18:45]# systemctl enable nginx –now
# 准备主页
[root@controller ~ 11:18:53]# mv /usr/share/nginx/html/index.html{,.ori}
[root@controller ~ 11:19:01]# echo Hello World From Nginx > /usr/share/nginx/html/index.html
# 防火墙
# firewall-cmd –add-service=http –permanent
# firewall-cmd –reload
#未启动不用管
[root@controller ~ 11:19:24]# curl http://www.lz.cloud
Hello World From Nginx
# windows客户端修改 C:\\Windows\\System32\\drivers\\etc\\hosts
# Linux或Unix修改 /etc/hosts
# 添加如下记录
10.1.8.10 www.lz.cloud
(二)Nginx 配置
1.配置结构
Nginx 配置采用层级化、模块化的组织方式,整体是 “全局块 → 核心模块块 → 业务模块块” 的嵌套结构。
(1)全局配置块
作用于 Nginx 整个进程的基础配置,不嵌套在任何块内,是配置文件的 “根级别”。
# 全局配置示例
user nginx; # 运行Nginx的用户/用户组
worker_processes auto; # 工作进程数(核心参数,建议设为CPU核心数)
error_log /var/log/nginx/error.log; # 错误日志路径
pid /run/nginx.pid; # 主进程PID文件路径
include /usr/share/nginx/modules/*.conf; # 加载外部模块配置(全局级引入)
(2)核心模块 块
Nginx 的核心功能模块 events 块,用于处理网络连接相关配置。
events {
worker_connections 1024; # 每个工作进程的最大并发连接数
use epoll; # 事件驱动模型(epoll是Linux下高性能选择)
multi_accept on; # 允许一个进程一次性接受多个新连接
}
(3)业务模块 块
处理具体业务的核心配置块,最核心的是 http 块(HTTP/HTTPS 服务),可以包含多个 server 块(虚拟主机)。
# http块:所有HTTP/HTTPS服务的公共配置,可嵌套多个server块
http {
# HTTP全局公共配置
include /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;
# … 其他配置
}
}
(4)特殊配置:HTTPS 专属块
如果配置 HTTPS,会在 server 块内增加 SSL 相关配置:
server {
listen 443 ssl; # 监听HTTPS端口并启用SSL
server_name example.com;
# SSL证书配置
ssl_certificate /etc/nginx/cert/server.crt; # 公钥文件
ssl_certificate_key /etc/nginx/cert/server.key; # 私钥文件
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
# … 其他配置(如root、location等)
}
2.配置加载机制
(1)include 指令:
Nginx 支持通过 include 引入外部配置文件,实现模块化管理。 – 把不同虚拟主机配置拆到 /etc/nginx/conf.d/.conf – 把不同代理配置拆到 /etc/nginx/default.d/.conf
(2)配置优先级:
- 同层级:后定义的配置覆盖先定义的; – 不同层级:子级(如 location)覆盖父级(如 server/http); – location 匹配:精准匹配(=)> 正则匹配(/*)> 普通前缀匹配。
3.nginx.conf 配置详解
# 更多配置详情参考官方文档:
# * 英文官方文档: http://nginx.org/en/docs/
# * 俄文官方文档: http://nginx.org/ru/docs/
# 指定Nginx工作进程的运行用户为nginx
user nginx;
# 工作进程数,设置为auto时会自动根据CPU核心数调整
worker_processes auto;
# 错误日志文件路径及存储位置
error_log /var/log/nginx/error.log;
# Nginx主进程PID文件路径,用于标识进程ID
pid /run/nginx.pid;
# 加载动态模块,详细说明可查看/usr/share/doc/nginx/README.dynamic文件
include /usr/share/nginx/modules/*.conf;
# 事件模块配置块,用于设置网络连接相关参数
events {
# 每个工作进程的最大并发连接数,默认1024
worker_connections 1024;
}
# HTTP核心模块配置块,包含HTTP服务的主要配置
http {
# 定义访问日志的格式,命名为main
# 日志字段说明:客户端IP – 远程用户 [访问时间] "请求信息" 状态码 发送字节数 "来源页面" "用户代理" "代理IP"
log_format main '$remote_addr – $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# 启用访问日志,使用main格式,日志文件存储路径
access_log /var/log/nginx/access.log main;
# 启用高效文件传输模式,减少磁盘I/O和CPU消耗
sendfile on;
# 启用TCP_NOPUSH选项,在发送响应时累积数据后一次性发送,提高网络效率(需配合sendfile使用)
tcp_nopush on;
# 启用TCP_NODELAY选项,禁用Nagle算法,减少数据传输延迟(适用于实时性要求高的场景)
tcp_nodelay on;
# HTTP长连接超时时间,超过65秒无活动则关闭连接
keepalive_timeout 65;
# 文件类型哈希表的最大容量,增大可提高文件类型查找效率
types_hash_max_size 4096;
# 引入MIME类型配置文件,定义不同文件后缀对应的响应类型
include /etc/nginx/mime.types;
# 默认MIME类型,当无法识别文件类型时使用(二进制流格式)
default_type application/octet-stream;
# 加载/etc/nginx/conf.d目录下的所有.conf后缀配置文件(模块化配置)
# 更多说明参考http://nginx.org/en/docs/ngx_core_module.html#include
include /etc/nginx/conf.d/*.conf;
# 虚拟主机配置块(默认HTTP服务)
server {
# 监听IPv4的80端口(HTTP默认端口)
listen 80;
# 监听IPv6的80端口
listen [::]:80;
# 虚拟主机域名,_表示匹配所有未明确指定的域名
server_name _;
# 网站根目录,存放静态资源的路径
root /usr/share/nginx/html;
# 加载默认虚拟主机的额外配置文件(来自/etc/nginx/default.d/*.conf)
include /etc/nginx/default.d/*.conf;
# 配置404错误页面,当请求资源不存在时返回/404.html
error_page 404 /404.html;
# 精确匹配/404.html的访问路径(无额外配置,直接返回文件)
location = /404.html {
}
# 配置500/502/503/504服务器错误页面,返回/50x.html
error_page 500 502 503 504 /50x.html;
# 精确匹配/50x.html的访问路径(无额外配置,直接返回文件)
location = /50x.html {
}
}
# TLS/SSL加密服务配置(默认注释,启用需取消注释并配置证书)
#
# server {
# # 监听IPv4的443端口(HTTPS默认端口),启用SSL和HTTP/2协议
# listen 443 ssl http2;
# # 监听IPv6的443端口,启用SSL和HTTP/2协议
# listen [::]:443 ssl http2;
# # 虚拟主机域名(需替换为实际域名)
# server_name _;
# # 网站根目录(与HTTP服务一致)
# root /usr/share/nginx/html;
#
# # SSL证书文件路径(公钥)
# ssl_certificate "/etc/pki/nginx/server.crt";
# # SSL证书密钥文件路径(私钥,需保密)
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# # SSL会话缓存配置:共享缓存,名称SSL,大小1MB
# ssl_session_cache shared:SSL:1m;
# # SSL会话超时时间,10分钟内再次连接无需重新握手
# ssl_session_timeout 10m;
# # SSL加密套件,优先选择高强度加密算法,排除aNULL和MD5
# ssl_ciphers HIGH:!aNULL:!MD5;
# # 优先使用服务器端指定的加密套件
# ssl_prefer_server_ciphers on;
#
# # 加载默认虚拟主机的额外配置文件
# include /etc/nginx/default.d/*.conf;
#
# # 404错误页面配置(原配置笔误,应为/404.html,此处保留原注释结构)
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# # 服务器错误页面配置
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
(三)虚拟主机
同一个 web 服务器提供多个站点。
虚拟主机支持多种方式:
根据名称
# 参考主配置文件/etc/nginx/nginx.conf中server块配置
[root@controller ~ 13:44:07]# cp /etc/nginx/nginx.conf /etc/nginx/conf.d/vhost-name.conf
[root@controller ~ 13:44:16]# vim /etc/nginx/conf.d/vhost-name.conf
[root@controller ~ 14:26:20]# cat /etc/nginx/conf.d/vhost-name.conf
server {
server_name web1.lz.cloud;
root /usr/share/nginx/web1;
}
server {
server_name web2.lz.cloud;
root /usr/share/nginx/web2;
}
客户端测试
10.1.8.10 web1.lz.cloud web1
10.1.8.10 web2.lz.cloud web2

提示:清理环境,避免影响后续实验。
[root@controller ~ 14:48:15]# cd /etc/nginx/conf.d/
[root@controller conf.d 14:48:34]# ls
vhost-name.conf vhost-port.conf
[root@controller conf.d 14:48:35]# mkdir backup
[root@controller conf.d 14:48:45]# mv vhost-* backup/
[root@controller conf.d 14:48:56]# ls
backup
根据 port
[root@controller conf.d 14:10:13]# ls
vhost-name.conf
[root@controller conf.d 14:10:15]# cp vhost-name.conf vhost-port.conf
[root@controller conf.d 14:12:16]# vim vhost-port.conf
[root@controller conf.d 14:13:30]# cd
[root@controller ~ 14:13:45]# mkdir /usr/share/nginx//808{1,2}
[root@controller ~ 14:14:01]# echo 8081 > /usr/share/nginx/8081/index.html
[root@controller ~ 14:14:24]# echo 8082 > /usr/share/nginx/8082/index.html
[root@controller ~ 14:14:40]# systemctl restart nginx
客户端测试
# 配置名称解析,假设web服务器ip地址为10.1.8.10
10.1.8.10 www.lz.cloud

提示:清理环境,避免影响后续实验。
(四)配置 SSL/TLS
1.生成证书
#–1–生成私钥
[root@controller certs 14:57:04]# openssl genrsa -out www.key 2048
Generating RSA private key, 2048 bit long modulus
..+++
.......................................................................................+++
e is 65537 (0x10001)
#–2–生成请求文件csr
[root@controller certs 14:57:08]# openssl req -new -key www.key -out www.csr -subj "/C=CN/ST=JS/L=NJ/O=LM/OU=DEVOPS/CN=www.lz.cloud/emailAddress=webadmin@lz.cloud"
# CN的值必须是网站域名
#–3–使用自己的私钥对请求文件签名,以生成证书
[root@controller certs 14:57:14]# openssl x509 -req -days 3650 -in www.csr -signkey www.key -out www.crt
Signature ok
subject=/C=CN/ST=JS/L=NJ/O=LM/OU=DEVOPS/CN=www.lz.cloud/emailAddress=webadmin@lz.cloud
Getting Private key
[root@controller certs 14:57:19]# ls
www.crt www.csr www.key
2.配置站点
[root@controller certs 14:57:21]# mkdir /etc/ssl/certs/www.lz.cloud
[root@controller certs 14:57:40]# mv www* /etc/ssl/certs/www.lz.cloud
[root@controller certs 14:57:59]# cd
# 参照默认配置修改
[root@controller ~ 14:58:04]# cp /etc/nginx/nginx.conf /etc/nginx/conf.d/vhost-ssh.conf
cp:是否覆盖"/etc/nginx/conf.d/vhost-ssh.conf"? y
[root@controller ~ 14:58:44]# vim /etc/nginx/conf.d/vhost-ssh.conf
[root@controller ~ 14:59:39]# cat /etc/nginx/conf.d/vhost-ssh.conf
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.lz.cloud;
root /usr/share/nginx/html;
# 证书
ssl_certificate "/etc/ssl/certs/www.lz.cloud/www.crt";
# 私钥
ssl_certificate_key "/etc/ssl/certs/www.lz.cloud/www.key";
}
[root@controller ~ 14:59:46]# systemctl restart nginx
配置HTTP重定向到https
[root@controller ~ 14:59:56]# vim /etc/nginx/conf.d/vhost-ssh.conf
[root@controller ~ 15:18:40]# cat /etc/nginx/conf.d/vhost-ssh.conf
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.lz.cloud;
root /usr/share/nginx/html;
# 证书
ssl_certificate "/etc/ssl/certs/www.lz.cloud/www.crt";
# 私钥
ssl_certificate_key "/etc/ssl/certs/www.lz.cloud/www.key";
}
# 配置HTTP重定向到https
server {
listen 80;
listen [::]:80;
server_name www.lz.cloud;
root /usr/share/nginx/html;
# 添加重定向
return 301 https://$host$request_uri;
}
[root@controller ~ 15:18:47]# systemctl restart nginx
[root@controller ~ 15:19:03]# vim /etc/hosts
#添加域名为了下面测试:10.1.8.10 www.lz.cloud www
#测试
[root@controller ~ 15:19:47]# curl http://www.lz.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@controller ~ 15:20:00]# curl -k https://www.lz.cloud
Hello World
# 使用-L指明跟随重定向
[root@controller ~ 15:20:48]# curl -Lk http://www.lz.cloud
Hello World
[root@controller ~ 15:21:00]# ls /etc/nginx/conf.d/
backup vhost-ssh.conf
[root@controller ~ 15:34:42]# mv /etc/nginx/conf.d/vhost-ssh.conf /etc/nginx/conf.d/backup/
[root@controller ~ 15:35:04]# ls /etc/nginx/conf.d/
backup
(五)申请免费的https证书
Let’s Encrypt 官方推荐使用 ACME 客户端获取证书,其中 Certbot 是最常用的工具,适配 Linux、Windows 等主流系统。
1.CentOS 7 系统
(1)安装 Certbot
[root@controller ~ 16:22:31]# yum install certbot -y
提示:certbot 依赖 epel仓库。
(2)发起证书申请
执行以下命令启动手动 DNS 验证模式,将mylz.top替换为你的域名:
[root@controller ~ 16:23:06]# certbot certonly –manual –preferred-challenges dns -d mylz.top -d *.mylz.top
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Enter email address (used for urgent renewal and security notices)
(Enter 'c' to cancel): 2370787636@qq.com
Starting new HTTPS connection (1): acme-v02.api.letsencrypt.org
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.6-August-18-2025.pdf. You must agree
in order to register with the ACME server. Do you agree?
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
(Y)es/(N)o: Y
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
(Y)es/(N)o: Y
Account registered.
Requesting a certificate for mylz.top and *.mylz.top
Performing the following challenges:
dns-01 challenge for mylz.top
dns-01 challenge for mylz.top
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
Please deploy a DNS TXT record under the name
_acme-challenge.mylz.top with the following value:
2mgqSSOYj5V8QVLp85BX_6bSGYiJttDTMV3zCqwfkYQ
Before continuing, verify the record is deployed.
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
Press Enter to Continue
# 这里不要按回车,等 TXT 记录配置完成后再按回车
# 这里不要按回车,等 TXT 记录配置完成后再按回车
# 这里不要按回车,等 TXT 记录配置完成后再按回车
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
登录域名服务商(如阿里云、腾讯云)的 DNS 控制台,添加对应的 TXT 记录。以下截图是阿里云控制台。

添加后需要等待一段时间,然后通过以下命令验证记录是否生效,直到能查到该记录,再按回车继续。
[root@controller ~ 18:35:43]# nslookup -type=TXT _acme-challenge.mylz.top
Server:223.5.5.5
Address:223.5.5.5#53
Non-authoritative answer:
_acme-challenge.mylz.toptext = "2mgqSSOYj5V8QVLp85BX_6bSGYiJttDTMV3zCqwfkYQ"
Authoritative answers can be found from:
根据提示,再次添加一个TXT记录。
Please deploy a DNS TXT record under the name
_acme-challenge.mylz.top with the following value:
LGFbw12poWjg1uo-W011sZItLXRVIgygMYAxuFir3WI
Before continuing, verify the record is deployed.
(This must be set up in addition to the previous challenges; do not remove,
replace, or undo the previous challenge tasks yet. Note that you might be
asked to create multiple distinct TXT records with the same name. This is
permitted by DNS standards.)
– – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – – –
Press Enter to Continue
# 这里不要按回车,等 TXT 记录配置完成后再按回车
# 这里不要按回车,等 TXT 记录配置完成后再按回车
# 这里不要按回车,等 TXT 记录配置完成后再按回车
再次登录域名服务商(如阿里云、腾讯云)的 DNS 控制台,添加新的 TXT 记录。以下截图是阿里云控制台。

添加后需要等待一段时间,然后通过以下命令验证记录是否生效,直到能查到该记录,再按回车继续。
[root@controller ~ 18:37:42]# nslookup -type=TXT _acme-challenge.mylz.top
Server:223.5.5.5
Address:223.5.5.5#53
Non-authoritative answer:
_acme-challenge.mylz.toptext = "LGFbw12poWjg1uo-W011sZItLXRVIgygMYAxuFir3WI"
_acme-challenge.mylz.toptext = "2mgqSSOYj5V8QVLp85BX_6bSGYiJttDTMV3zCqwfkYQ"
Authoritative answers can be found from:
获取证书
Waiting for verification...
Cleaning up challenges
Subscribe to the EFF mailing list (email: 2370787636@qq.com).
Starting new HTTPS connection (1): supporters.eff.org
IMPORTANT NOTES:
– Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/mylz.top/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/mylz.top/privkey.pem
Your certificate will expire on 2026-09-02. To obtain a new or
tweaked version of this certificate in the future, simply run
certbot again. To non-interactively renew *all* of your
certificates, run "certbot renew"
– If you like Certbot, please consider supporting our work by:
Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le
验证通过后,证书会自动生成并存储在/etc/letsencrypt/live/你的域名/目录下,包含证书链文件fullchain.pem和私钥文件privkey.pem。
#打开 nginx 站点配置,ssl 两行严格这样
ssl_certificate /etc/letsencrypt/live/mylz.top/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mylz.top/privkey.pem;
利用ai生成图形化界面脚本,放在/usr/share/nginx/html/index.html中。

2.设置自动续期
Let’s Encrypt 证书有效期为 90 天,可通过定时任务实现自动续期。例如 Linux 系统中,添加 crontab 定时任务:
# 每天凌晨2点检查证书,到期自动续期
echo "0 2 * * * /usr/bin/certbot renew –quiet" | tee -a /etc/crontab
3.证书续期后,网站如何更新证书
推荐解决方案:
(六)配置基本认证
用户名和密码使用plain text发送,所以最好配置SSL/TLS。
[root@controller ~ 15:35:07]# yum -y install httpd-tools
[root@controller ~ 16:01:07]# cat /etc/nginx/conf.d/vhost-ssh.conf
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.lz.cloud;
root /usr/share/nginx/html;
# 证书
ssl_certificate "/etc/ssl/certs/www.lz.cloud/www.crt";
# 私钥
ssl_certificate_key "/etc/ssl/certs/www.lz.cloud/www.key";
location /auth-basic/ {
auth_basic "Basic Auth";
auth_basic_user_file "/etc/nginx/.htpasswd";
}
}
server {
listen 80;
listen [::]:80;
server_name www.lz.cloud;
root /usr/share/nginx/html;
# 添加重定向
return 301 https://$host$request_uri;
}
[root@controller ~ 16:01:18]# systemctl restart nginx
[root@controller ~ 15:54:20]# htpasswd -b -c /etc/nginx/.htpasswd lz 123456
Adding password for user lz
[root@controller ~ 15:54:42]# mkdir /usr/share/nginx/html/auth-basic
[root@controller ~ 15:54:57]# vim /usr/share/nginx/html/auth-basic/index.html
[root@controller ~ 15:55:49]# cat /usr/share/nginx/html/auth-basic/index.html
<html>
<body>
<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: lzer;">
Test Page for Basic Authentication
</div>
</body>
</html>

(七)静态、动态站点
1. 概念
静态:页面写死,服务器只原样返回 HTML,无数据交互。
动态:后端程序生成页面,可读写数据库、用户交互。
2. 运行原理
静态网站
例:纯官网介绍页、企业落地页
动态网站
例:商城、博客、后台管理、知乎、淘宝
3. 内容特点
| 内容 | 所有用户打开页面一模一样,改内容必须改源码文件 | 不同用户 / 不同时间内容不同(登录后个人中心、商品库存实时变) |
| 修改内容 | 改源码→重新上传服务器 | 后台管理页面在线编辑,自动存数据库,不用改代码 |
| 交互能力 | 只能前端 JS 做简单动画,无法提交表单存数据(注册、留言、评论不行) | 支持注册登录、留言、下单、搜索、数据增删改查 |
4. 服务器依赖
- 静态:只需要 Nginx/Apache 静态 web 服务器,不需要运行语言环境、数据库,部署简单。
- 动态:Web 服务器 + 运行环境(PHP/JDK/Python)+ 数据库 (MySQL) 三件套缺一不可。
5. 性能 & 成本
6. 常见技术栈
- 静态:HTML+CSS+JS、Hugo/VitePress/Hexo 静态生成器
- 动态:
- PHP+MySQL+Nginx(LAMP/LNMP)
- Java(SpringBoot)+Mysql
- Python(Django/Flask)
- Node.js(Express/NestJS)
(八)PHP 站点
客户端访问php网页流程:
# 安装PHP和php-fpm,建议把其他的扩展包一起安装
[root@server ~ 09:47:14]# yum install -y php php-fpm
# php-fpm: 负责接收web程序发来的php代码
# php:负责解析和执行php代码,并将结果返回给php-fpm
# 当客户端访问 php 站点时,web站点接收用户请求
# 并转发 php 代码给php-fpm服务
# php-fpm 服务调用php解析php网页,然后将结果返回给web程序
# web 程序将结果返回给客户端
# 启用并启动php-fpm服务
[root@server ~ 09:47:54]# systemctl enable php-fpm.service –now
[root@server ~ 09:49:15]# systemctl status php-fpm.service
[root@server ~ 09:49:35]# ss -lntp | grep 1435
LISTEN 0 128 127.0.0.1:9000 *:* users:(("php–fpm",pid=1440,fd=0),("php–fpm",pid=1439,fd=0),("php–fpm",pid=1438,fd=0),("php–fpm",pid=1437,fd=0),("php–fpm",pid=1436,fd=0),("php–fpm",pid=1435,fd=6))
# 建议把其他的扩展包一起安装
[root@server ~ 09:49:55]# yum install -y php-gd php-common php-pear php-mbstring php-mcrypt
# 查看 php 版本
[root@server ~ 09:57:02]# php -v
# 测试 php 是否正常
[root@server ~ 09:57:10]# echo "<?php echo 'PHP Test Page'.\\"\\n\\"; ?>" > php_test.php [root@server ~ 10:00:39]# php php_test.php
PHP Test Page
# 准备测试页,使用phpinfo查看详细信息
[root@server ~ 10:00:49]# echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/info.php
配置虚拟机主机支持php
# 修改配置文件(方法1)
[root@server ~ 10:05:45]# cp /etc/nginx/nginx.conf /etc/nginx/conf.d/vhost-www.lz.conf
[root@server ~ 10:06:08]# vim /etc/nginx/conf.d/vhost-www.lz.conf
server {
listen 80;
listen [::]:80;
server_name server.lz.cloud;
root /usr/share/nginx/html;
# 匹配所有以.php结尾的URL请求,验证PHP文件是否存在
# 存在则转发给本地9000端口的PHP-FPM处理,不存在则返回404
location ~ \\.php$ {
# try_files:检测请求的PHP文件($uri)是否存在,不存在直接返回404错误
# 作用:防止伪造PHP路径的恶意请求(如/xxx.php/yyy.jpg)被PHP-FPM解析,是重要的安全防护
try_files $uri =404;
# fastcgi_pass:指定FastCGI服务地址,将PHP请求转发到本地9000端口的PHP-FPM进程
fastcgi_pass 127.0.0.1:9000;
# fastcgi_index:定义FastCGI默认索引文件,请求目录时默认使用index.php
fastcgi_index index.php;
# fastcgi_param:设置传递给PHP-FPM的核心环境变量
# SCRIPT_FILENAME:指定要执行的PHP文件绝对路径,
# $document_root是网站根目录,$fastcgi_script_name是请求的脚本名
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@server ~ 10:17:08]# systemctl restart nginx
#修改配置文件(方法2)
[root@server ~ 10:29:19]# cp /etc/nginx/conf.d/vhost-www.lz.conf /etc/nphp.conf
[root@server ~ 10:34:34]# vim /etc/nginx/default.d/php.conf
[root@server ~ 10:37:16]# cat /etc/nginx/default.d/php.conf
location ~ \\.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
[root@server ~ 10:36:50]# vim /etc/nginx/conf.d/vhost-www.lz.conf
[root@server ~ 10:37:04]# cat /etc/nginx/conf.d/vhost-www.lz.conf
server {
listen 80;
listen [::]:80;
server_name server.lz.cloud;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
}
浏览器访问

(九)项目实战:网盘-KodExplorer
1.KodExplorer 介绍
KodExplorer(可道云,原名芒果云)是一款基于Web技术开发的开源私有云与在线文件管理系统,由杭州可道云网络有限公司研发,致力于为个人、小型团队及企业提供安全可控、易用高效的私有云解决方案,目前已拥有超过40万服务器装机量,被数千家企业及机构广泛应用。
作为轻量级开源项目,KodExplorer无需依赖第三方运维面板,部署门槛低、资源占用少,适配低配服务器,尤其适合CentOS 7.9等Linux系统私有化部署,数据完全由自身掌控,避免公共网盘的隐私泄露、速度限制等问题[2][7]。其核心特性贴合日常文件管理与协作需求,具体如下:
2.实验环境
本手册聚焦CentOS 7.9环境部署KodExplorer的,适配个人及小型团队的私有网盘、文件管理需求
# systemctl disable firewalld –now
# setenforce 0
# sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
3.安装 Nginx
# yum install -y nginx
# systemctl enable nginx –now
# curl -s http://localhost/ |grep Welcome
4.安装 PHP
# 安装Remi源
[root@server ~ 10:46:59]# yum install -y yum-utils https://rpms.remirepo.net/enterprise/remi-release-7.rpm
# 启用Remi PHP 7.4仓库(稳定性最优)
[root@server ~ 10:51:07]# yum-config-manager –enable remi-php74
[root@server ~ 10:52:11]# cat /etc/yum.repos.d/remi-php74.repo
# 安装 PHP
[root@server ~ 10:52:59]# yum install -y php php-fpm php-mbstring php-gd
# 备用命令
# yum install -y php php-fpm php-mysqlnd php-zip php-gd php-mbstring php-xml php-bcmath php-intl php-opcache php-ldap php-pecl-apcu php-cli
# 验证PHP版本
[root@server ~ 11:04:25]# php -v
PHP 7.4.33 (cli) (built: Jun 5 2024 05:05:14) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
# 配置php-fpm运行账户
[root@server ~ 11:16:23]# vim /etc/php-fpm.d/www.conf
user = nginx
group = nginx
# 关闭 PHP Session 的 Secure 选项(避免 HTTP 环境下 Cookie 无法传递)
[root@server ~ 11:18:29]# sed -i 's/^session.cookie_secure = On/session.cookie_secure = Off/' /etc/php.ini
# 设置 php 会话目录权限
[root@server ~ 11:18:38]# chgrp -R nginx /var/lib/php/session/
[root@server ~ 11:18:50]# systemctl enable php-fpm.service –now
5.部署 KodExplorer
部署 kodexplorer 源码
到 https://gitee.com/cneapp/KODExplorer 下载 kodexplorer文件。
[root@server ~ 11:18:59]# rz -E
rz waiting to receive.
[root@server ~ 11:19:38]# ls
anaconda-ks.cfg kodexplorer4.51.zip php_test.php
[root@server ~ 11:19:51]# mv kodexplorer4.51.zip /usr/share/nginx/html/
[root@server ~ 11:20:08]# cd /usr/share/nginx/html/
[root@server html 11:20:15]# ls
404.html en-US img index.html.ori kodexplorer4.51.zip poweredby.png
50x.html icons index.html info.php nginx-logo.png
[root@server html 11:20:20]# unzip kodexplorer4.51.zip
# 批量授权目录读写权限
[root@server html 11:21:53]# chown -R nginx:nginx KODExplorer
[root@server html 11:22:14]# chown -R 755 KODExplorer
# 新建KodExplorer专属独立站点配置文件
[root@server ~ 11:34:54]# vim /etc/nginx/conf.d/kod.conf
[root@server ~ 11:40:12]# cat /etc/nginx/conf.d/kod.conf
server {
listen 80;
server_name 10.1.8.10;
root /usr/share/nginx/html/KODExplorer;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
# PHP解析
location ~ \\.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
# 配置核心服务开机自启,后台常驻运行
systemctl restart nginx
6.初始化 KodExplorer
1.浏览器访问地址:直接打开浏览器,输入服务器IP,自动跳转KodExplorer安装初始化页面;

2.环境自检:页面会自动检测PHP版本、目录权限、拓展组件,全程绿灯即为环境正常,无需手动排查;
3.管理员账号设置:自定义高强度专属管理员用户名、复杂登录密码,牢记账号信息,杜绝非法越级访问;

登录。

点击 自动更新,也可以直接关闭更新页面。

7.使用 KodExplorer
桌面

(十)反向代理
1.反向代理介绍

反向代理(reverse proxy),指的是代理外网用户的请求到内部的指定的服务器,并将数据返回给用户。客户端不直接与后端服务器进行通信,而是与反向代理服务器进行通信,隐藏了后端服务器的 IP 地址。
反向代理的主要作用是提供负载均衡和高可用性:
• 负载均衡:Nginx可以将传入的请求分发给多个后端服务器,以平衡服务器的负载,提高系统性能和可靠性。
• 缓存功能:Nginx可以缓存静态文件或动态页面,减轻服务器的负载,提高响应速度。
• 动静分离:将动态生成的内容(如 PHP、Python、Node.js 等)和静态资源(如 HTML、CSS、JavaScript、图片、视频等)分别存放在不同的服务器或路径上。
• 多站点代理:Nginx可以代理多个域名或虚拟主机,将不同的请求转发到不同的后端服务器上,实现多个站点的共享端口。
2.反向代理模块
• ngx_http_proxy_module:将客户端的请求以http协议转发至指定服务器进行处理。
• ngx_http_upstream_module:用于定义为proxy_pass,fastcgi_pass,uwsgi_pass等指令引用的后端服务器分组。
• ngx_stream_proxy_module:将客户端的请求以tcp协议转发至指定服务器处理。
• ngx_http_fastcgi_module:将客户端对php的请求以fastcgi协议转发至指定服务器处理。
• ngx_http_uwsgi_module:将客户端对Python的请求以uwsgi协议转发至指定服务器处理。
3.Location 配置
(1)Location 配置语法
Nginx 使用 location 匹配规则 + proxy_pass 反向代理指令实现反向代理功能,匹配本质是 “URL 路径匹配 → 命中对应规则 → 转发至指定后端地址”。
• location 定义匹配路径
• proxy_pass 指定后端服务地址
http {
# 后端服务可配置 upstream 集群(推荐,支持负载均衡)
upstream backend_nginx {
nginx 192.168.1.100:8080; # 后端服务1
nginx 192.168.1.101:8080; # 后端服务2(多节点自动轮询负载均衡)
}
server {
listen 80; # Nginx 监听端口
server_name localhost; # 访问域名/IP
# 1. 匹配所有请求(兜底规则)
location / {
proxy_pass http://backend_nginx; # 转发至 upstream 集群
# 必加的反向代理核心参数(传递客户端真实信息、适配后端服务)
proxy_set_header Host $host; # 传递客户端访问的域名
proxy_set_header X-Real-IP $remote_addr; # 传递客户端真实IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #传递IP链路
proxy_set_header X-Forwarded-Proto $scheme; # 传递请求协议(http/https)
}
# 2. 匹配特定路径(如 /api 开头的请求,单独转发)
location /api/ {
proxy_pass http://192.168.1.102:9090/; # 后端地址末尾带 /,会剔除匹配的 /api/
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
(2)Location 匹配规则
后端匹配逻辑:URL 路径 → 按 location 优先级命中规则 → 由规则内的 proxy_pass 转发至对应后端;
优先级:精确匹配(=)> 前缀匹配(^~)> 正则匹配(/*)> 普通前缀 > 兜底(/);
URL 重构关键:proxy_pass 末尾是否带 /,决定是否剔除 location 匹配的路径前缀。
精确匹配(=)
-
语法:location = /path { … }
-
逻辑:仅当请求 URL 与 /path 完全一致时命中,优先级最高。
-
示例:
# 仅匹配 http://localhost/login,不匹配 /login?a=1、/login/
location = /login {
proxy_pass http://backend_login:8080;
}
2.前缀配(^~)
语法:location ^~ /path { … }
逻辑:URL 以 /path 开头即命中,优先级仅次于精确匹配,会跳过正则匹配。
用途:优先匹配静态资源(如 /static、/img)或特定业务路径,避免被正则规则拦截。
示例:
# 匹配所有 /static 开头的请求(如 /static/css/main.css、/static/img/1.jpg)
location ^~ /static/ {
proxy_pass http://backend_static:80;
}
3.正则匹配(~ / ~*)
-
语法:
- 区分大小写:location ~ /regex { … }(如 /API 不匹配 /api 规则)
- 不区分大小写:location ~* /regex { … }(如 /API、/api 均匹配)
-
逻辑:URL 符合正则表达式即命中,优先级低于前缀匹配(^~),多个正则规则按定义顺序匹配,先命中先生效。
-
示例:
# 匹配所有 .jpg、.png、.gif 结尾的图片请求(不区分大小写)
location ~* \\.(jpg|png|gif)$ {
proxy_pass http://backend_img:80;
}
4.普通前缀匹配(无符号)
-
语法:location /path { … }
-
逻辑:URL 以 /path 开头即命中,优先级低于正则匹配,多个普通前缀规则按 “路径最长” 优先命中。
-
示例:
# 规则1:匹配 /api/xxx(路径长度3)
location /api/ {
proxy_pass http://backend_api:9090;
}
# 规则2:匹配 /api/user/xxx(路径长度7,比规则1长,优先命中)
location /api/user/ {
proxy_pass http://backend_user:9090;
}
5.通用匹配(/)
语法:location / { … }
逻辑:所有未被上述规则命中的请求,都会匹配此规则(兜底),优先级最低。
用途:通常作为全局反向代理,转发所有默认请求到主后端服务。
(3)proxy_pass 后端地址细节
proxy_pass 末尾是否带 /,会直接改变转发到后端的 URL 路径,这是后端匹配后 “URL 重构” 的核心,分 2 种场景:
场景 1:proxy_pass 末尾带 /
-
逻辑:转发时,会剔除 location 匹配的路径前缀,将剩余路径拼接在后端地址后。
-
示例:
# location 匹配 /api/,proxy_pass 末尾带 /
location /api/ {
proxy_pass http://192.168.1.102:9090/;
}
# 实际转发逻辑:
# 客户端请求 http://localhost/api/user/list → 后端接收 http://192.168.1.102:9090/user/list
场景 2:proxy_pass 末尾不带 /
-
逻辑:转发时,会保留 location 匹配的路径前缀,直接拼接在后端地址后。
-
示例:
# location 匹配 /api/,proxy_pass 末尾不带 /
location /api/ {
proxy_pass http://192.168.1.102:9090;
}
# 实际转发逻辑:
# 客户端请求 http://localhost/api/user/list → 后端接收 http://192.168.1.102:9090/api/user/list
(4)综合示例
配置文件
http {
upstream backend_main { nginx 192.168.1.200:8080; }
upstream backend_api { nginx 192.168.1.201:9090; }
upstream backend_static { nginx 192.168.1.202:80; }
upstream backend_login { nginx 192.168.1.203:8080; }
server {
listen 80;
server_name localhost;
# 1. 精确匹配:仅 /login → 后端 login 服务
location = /login {
proxy_pass http://backend_login;
proxy_set_header Host $host;
}
# 2. 前缀匹配:/static/ 开头 → 后端静态服务(跳过正则)
location ^~ /static/ {
proxy_pass http://backend_static;
proxy_set_header Host $host;
}
# 3. 正则匹配:图片后缀 → 后端静态服务
location ~* \\.(jpg|png|gif)$ {
proxy_pass http://backend_static;
proxy_set_header Host $host;
}
# 4. 普通前缀:/api/ 开头 → 后端 api 服务
location /api/ {
proxy_pass http://backend_api/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
# 5. 兜底匹配:所有未命中的请求 → 主后端服务
location / {
proxy_pass http://backend_main;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
匹配流程

4.反向代理实践环境
环境架构
| 客户端 | client.lz.cloud | 10.1.8.11 | 测试服务器 |
| Nginx 服务器 | proxy.lz.cloud | 10.1.8.20 | 代理服务器 |
| Nginx 服务器 | nginx1.lz.cloud | 10.1.8.21 | Web 服务器 |
| Nginx 服务器 | nginx2.lz.cloud | 10.1.8.22 | Web 服务器 |
| Nginx 服务器 | nginx3.lz.cloud | 10.1.8.23 | Web 服务器 |
/etc/hosts
[root@proxy ~ 15:45:53]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.1.8.10 server.lz.cloud server
10.1.8.11 client.lz.cloud client
10.1.8.20 proxy.lz.cloud proxy
10.1.8.21 nginx1.lz.cloud nginx1
10.1.8.22 nginx2.lz.cloud nginx2
10.1.8.23 nginx3.lz.cloud nginx3
10.1.8.20 www.lz.cloud www
后端 nginx 服务器配置
[root@client ~ 09:45:03]# ssh-keygen
[root@client ~ 14:20:07]# ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa
[root@client ~ 14:21:42]# for host in 10.1.8.{11,20,21,22,23}; do sshpass -p123 ssh-copy-id root@$host;done
[root@client ~ 14:21:54]# for host in 10.1.8.{11,20,21,22,23}; do ssh root@$host hostname; done
client.lz.cloud
proxy.lz.cloud
nginx1.lz.cloud
nginx2.lz.cloud
nginx3.lz.cloud
[root@client ~ 14:42:49]# for host in 10.1.8.{11,20,21,22,23}; do scp /etc/hosts root@$host:/etc/hosts; done
hosts 100% 354 570.4KB/s 00:00
hosts 100% 354 364.5KB/s 00:00
hosts 100% 354 145.1KB/s 00:00
hosts 100% 354 313.6KB/s 00:00
hosts 100% 354 259.4KB/s 00:00
[root@client ~ 14:42:55]# for host in 10.1.8.{11,20,21,22,23}; do ssh root@$host cat /etc/hosts; done
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.1.8.10 server.lz.cloud server
10.1.8.11 client.lz.cloud client
10.1.8.20 proxy.lz.cloud proxy
10.1.8.21 nginx1.lz.cloud nginx1
10.1.8.22 nginx2.lz.cloud nginx2
10.1.8.23 nginx3.lz.cloud nginx3
10.1.8.20 www.lz.cloud www
# 除了客户端,所有节点安装nginx并启动nginx服务。
[root@client ~ 14:51:19]# for host in 10.1.8.{20,21,22,23}; do echo ;ssh root@$host yum install -y nginx; done
[root@client ~ 14:52:15]# for host in 10.1.8.{20,21,22,23}; do echo ;ssh root@$host rpm -q nginx; done
nginx-1.20.1-10.el7.x86_64
nginx-1.20.1-10.el7.x86_64
nginx-1.20.1-10.el7.x86_64
nginx-1.20.1-10.el7.x86_64
# 启动并启用服务
[root@client ~ 14:53:06]# for host in 10.1.8.{20,21,22,23}; do echo ;ssh root@$host systemctl enable nginx –now; done
# 准备主页-其他节点
[root@client ~ 14:56:12]# for host in nginx{1..3}.lz.cloud; do ssh root@$host "echo Welcome to $host > /usr/share/nginx/html/index.html"; done
Warning: Permanently added 'nginx1.lz.cloud' (ECDSA) to the list of known hosts.
Warning: Permanently added 'nginx2.lz.cloud' (ECDSA) to the list of known hosts.
Warning: Permanently added 'nginx3.lz.cloud' (ECDSA) to the list of known hosts.
# 客户端测试
[root@client ~ 14:57:25]# curl http://nginx1/
Welcome to nginx1.lz.cloud
[root@client ~ 14:57:36]# curl http://nginx2/
Welcome to nginx2.lz.cloud
[root@client ~ 14:57:41]# curl http://nginx3/
Welcome to nginx3.lz.cloud
前端 proxy 服务器配置
# 准备主页-代理节点
[root@proxy ~ 14:47:28]# mkdir /var/nginx
[root@proxy ~ 15:07:09]# echo Welcome to www.lz.cloud > /usr/share/nginx/html/index.html
[root@proxy ~ 15:08:03]# echo "Hello,Nginx" > /var/nginx/index.html
[root@proxy ~ 15:08:38]# echo "Hello,liz" > /var/nginx/test.txt
[root@proxy ~ 15:09:21]# cp /usr/share/nginx/html/nginx-logo.png /var/nginx/
[root@proxy ~ 15:09:48]# ls /var/nginx/
index.html nginx-logo.png test.txt
[root@proxy ~ 15:09:54]# vim /etc/nginx/conf.d/proxy.conf
server {
listen 80;
server_name www.lz.cloud;
# 匹配根位置
location / {
root /var/nginx;
index index.html;
}
}
# 重新加载nginx配置
[root@proxy ~ 15:10:49]# systemctl restart nginx
测试
[root@proxy ~ 15:11:58]# curl http://www.lz.cloud/
Hello,Nginx
[root@proxy ~ 15:12:22]# curl http://www.lz.cloud/test.txt
Hello,liz
访问 www.lz.cloud/nginx-logo.png,系统会返回以下页面:

思考:如果在Nginx目录中创建一个test.nn的文件,通过www.lz.cloud/test.nn访问该文件时,系统是否会返回对应文件内容?为什么?
答:不可以,因为“nn”类型的文件不在mime.type中,因此nginx无法对该文件进行解析,此时将下载文件到本地。
5.反向代理基础实践-代理本地
环境准备
[root@proxy ~ 15:16:46]# yum -y install tree
[root@proxy ~ 15:16:57]# mkdir /var/nginx/nginx{1,2}
[root@proxy ~ 15:17:29]# echo "Hello, I'm here /var/nginx/nginx1" > /var/nginx/nginx1/index.html
[root@proxy ~ 15:17:36]# echo "Hello, I'm here /var/nginx/nginx2" > /var/nginx/nginx2/index.html
[root@proxy ~ 15:17:45]# mkdir /var/nginx{1,2}
[root@proxy ~ 15:17:52]# echo "Hello, Nginx1" > /var/nginx1/index.html
[root@proxy ~ 15:18:00]# echo "Hello, Nginx2" > /var/nginx2/index.html
[root@proxy ~ 15:18:07]# tree /var/nginx*
/var/nginx
├── index.html
├── nginx1
│?? └── index.html
├── nginx2
│?? └── index.html
├── nginx-logo.png
└── test.txt
/var/nginx1
└── index.html
/var/nginx2
└── index.html
2 directories, 7 files
[root@proxy ~ 15:42:54]# \\
> for path1 in www{1..2}
> do
> for path2 in nginx{1..2}
> do
> mkdir -p /var/$path1/$path2
> echo "Hello, I'm here /var/$path1/$path2" > /var/$path1/$path2/index.html
> done
> done
[root@proxy ~ 22:13:15]# tree /var/www*
/var/www1
├── nginx1
│?? └── index.html
└── nginx2
└── index.html
/var/www2
├── nginx1
│?? └── index.html
└── nginx2
└── index.html
4 directories, 4 files
基本测试
[root@client ~ 15:41:47]# curl http://www.lz.cloud/
Hello,Nginx
# 显示结果是目录/var/nginx/nginx1中内容
[root@client ~ 15:41:40]# curl http://www.lz.cloud/nginx1/
Hello, I'm here /var/nginx/nginx1
# 显示结果是目录/var/nginx/nginx2中内容
[root@client ~ 15:41:40]# curl http://www.lz.cloud/nginx2/
Hello, I'm here /var/nginx/nginx2
实践1:无符号匹配
[root@proxy ~ 15:40:09]# cd /etc/nginx/conf.d/
[root@proxy conf.d 15:40:29]# vim proxy.conf
server {
listen 80;
server_name www.lz.cloud;
# 匹配根位置
location / {
root /var/nginx;
index index.html;
}
# 匹配/nginx1时,/var目录下找nginx1,完整路径是/var/nginx1
location /nginx1 {
root /var;
# 等效于下面的 alias 语句,必须使用绝对路径
# alias /var/nginx1;
index index.html;
}
}
# 重新加载nginx配置
[root@proxy conf.d 15:41:07]# nginx -s reload
访问测试
# nginx1 后面必须添加 / 符号
[root@client ~ 15:51:11]# curl http://www.lz.cloud/nginx1/
Hello, Nginx1
# 显示结果是目录/var/nginx1中内容
# nginx2 后面必须添加 / 符号
[root@client ~ 15:51:25]# curl http://www.lz.cloud/nginx2/
Hello, I'm here /var/nginx/nginx2
# 显示结果是目录/var/nginx/nginx2中内容
实验结果:无符号匹配优先级高于默认的/.
实践2:正则表达式匹配
[root@proxy conf.d 15:42:46]# vim proxy.conf
server {
listen 80;
server_name www.lz.cloud;
# 匹配根位置
location / {
root /var/nginx;
index index.html;
}
# 匹配/nginx1时,/var目录下找nginx1,完整路径是/var/nginx1
location /nginx1 {
root /var;
# 等效于下面的 alias 语句,必须使用绝对路径
# alias /var/nginx1;
index index.html;
}
# 正则表达式匹配 /nginx.*
location ~ /nginx.* {
root /var/www1;
index index.html;
}
}
# 重新加载nginx配置
[root@proxy conf.d 15:45:18]# nginx -s reload
访问测试
# nginx1 后面必须添加 / 符号
[root@client ~ 15:51:31]# curl http://www.lz.cloud/nginx1/
Hello, I'm here /var/www1/nginx1
# 显示结果是目录/var/www1/nginx1中内容
# nginx2 后面必须添加 / 符号
[root@client ~ 15:51:55]# curl http://www.lz.cloud/nginx2/
Hello, I'm here /var/www1/nginx2
# 显示结果是目录/var/www1/nginx2中内容
实验结果:正则表达式匹配优先级高于无符号
实践3:精确匹配
[root@proxy conf.d 15:45:40]# vim proxy.conf
server {
listen 80;
server_name www.lz.cloud;
# 匹配根位置
location / {
root /var/nginx;
index index.html;
}
# 匹配/nginx1时,/var目录下找nginx1,完整路径是/var/nginx1
location /nginx1 {
root /var;
# 等效于下面的 alias 语句,必须使用绝对路径
# alias /var/nginx1;
index index.html;
}
# 正则表达式匹配 /nginx.*
location ~ /nginx.* {
root /var/www1;
index index.html;
}
# 精确匹配
location = /nginx2/index.html {
root /var/www2;
index index.html;
}
}
# 重新加载nginx配置
[root@proxy conf.d 15:48:22]# nginx -s reload
访问测试
# nginx1 后面必须添加 / 符号
[root@client ~ 15:51:57]# curl http://www.lz.cloud/nginx1/
Hello, I'm here /var/www1/nginx1
# 显示结果是目录/var/www1/nginx1中内容
# nginx2 后面必须添加 / 符号
[root@client ~ 15:52:19]# curl http://www.lz.cloud/nginx2/
Hello, I'm here /var/www2/nginx2
# 显示结果是目录/var/www2/nginx2中内容
实验结果:精确匹配优先级高于正则表达式。
6.反向代理基础实践-代理远端
实践1:无符号匹配
[root@proxy conf.d 15:49:16]# vim proxy.conf
server {
listen 80;
server_name www.lz.cloud;
# 匹配根位置
location / {
root /var/nginx;
index index.html;
}
# 匹配 /nginx1/ 开头,代理到nginx1.lz.cloud,/nginx1/不组合到后端服务器
# 访问 /nginx1/ 开头,相当于直接访问http://nginx1.lz.cloud/
location /nginx1/ {
# 后端服务
proxy_pass http://nginx1.lz.cloud/; # 注意:代理后端后面有 /。
index index.html;
}
}
# 重新加载nginx配置
[root@proxy conf.d 15:50:16]# nginx -s reload
访问测试
# nginx1 后面必须添加 / 符号
[root@client ~ 15:52:21]# curl http://www.lz.cloud/nginx1/
Welcome to nginx1.lz.cloud
# 显示结果是服务器 nginx1.lz.cloud 内容
# nginx2 后面必须添加 / 符号
[root@client ~ 16:11:19]# curl http://www.lz.cloud/nginx2/
Hello, I'm here /var/nginx/nginx2
# 显示结果是目录/var/nginx/nginx2中内容
实验结果:无符号匹配优先级高于默认的/。
实践2:正则表达式匹配
[root@proxy conf.d 15:50:18]# vim proxy.conf
server {
listen 80;
server_name www.lz.cloud;
# 匹配根位置
location / {
root /var/nginx;
index index.html;
}
# 匹配 /nginx1/ 开头,代理到nginx1.lz.cloud,/nginx1/不组合到后端服务器
# 访问 /nginx1/ 开头,相当于直接访问http://nginx1.lz.cloud/
location /nginx1/ {
# 后端服务
proxy_pass http://nginx1.lz.cloud/; # 注意:代理后端后面有 /。
index index.html;
}
# 正则表达式匹配 /nginx.*
location ~ /nginx[123].* {
# 手动重写路径:去掉 /nginx 前缀,转发到目标服务器
# ^/nginx[123](.*)$ 匹配 /nginx[123] 开头的完整路径,$1表示 /nginx[123] 后的所有内容
# break 表示重写后不再匹配其他 rewrite 规则
rewrite ^/nginx[123](.*)$ $1 break;
# proxy_pass 不带 URI(无末尾的 /),配合 rewrite 实现路径替换
proxy_pass http://nginx2.lz.cloud;
index index.html;
}
}
# 重新加载nginx配置
[root@proxy conf.d 15:51:48]# nginx -s reload
访问测试
# nginx 后面必须添加 / 符号
[root@client ~ 16:11:31]# curl http://www.lz.cloud/nginx1/
Welcome to nginx2.lz.cloud
# 显示结果是服务器 nginx2.lz.cloud 内容
# nginx 后面必须添加 / 符号
[root@client ~ 16:12:40]# curl http://www.lz.cloud/nginx2/
Welcome to nginx2.lz.cloud
# 显示结果是服务器 nginx2.lz.cloud 内容
# nginx 后面必须添加 / 符号
[root@client ~ 16:12:44]# curl http://www.lz.cloud/nginx3/
Welcome to nginx2.lz.cloud
# 显示结果是服务器 nginx2.lz.cloud 内容
实验结果:正则表达式匹配优先级高于无符号。
实践3:精确匹配
[root@proxy conf.d 15:52:15]# vim proxy.conf
server {
listen 80;
server_name www.lz.cloud;
# 匹配根位置
location / {
root /var/nginx;
index index.html;
}
# 匹配 /nginx1/ 开头,代理到nginx1.lz.cloud,/nginx1/不组合到后端服务器
# 访问 /nginx1/ 开头,相当于直接访问http://nginx1.lz.cloud/
location /nginx1/ {
# 后端服务
proxy_pass http://nginx1.lz.cloud/; # 注意:代理后端后面有 /。
index index.html;
}
# 正则表达式匹配 /nginx.*
location ~ /nginx[123].* {
# 手动重写路径:去掉 /nginx 前缀,转发到目标服务器
# ^/nginx[123](.*)$ 匹配 /nginx[123] 开头的完整路径,$1表示 /nginx[123] 后的所有内容
# break 表示重写后不再匹配其他 rewrite 规则
rewrite ^/nginx[123](.*)$ $1 break;
# proxy_pass 不带 URI(无末尾的 /),配合 rewrite 实现路径替换
proxy_pass http://nginx2.lz.cloud;
index index.html;
}
# 精确匹配
location = /nginx3/ {
proxy_pass http://nginx3.lz.cloud/;
index index.html;
}
}
# 重新加载nginx配置
[root@proxy conf.d 16:10:50]# nginx -s reload
访问测试
# nginx1 后面必须添加 / 符号
[root@client ~ 16:12:48]# curl http://www.lz.cloud/nginx1/
Welcome to nginx2.lz.cloud
# 显示结果是服务器 nginx2.lz.cloud 内容
# nginx2 后面必须添加 / 符号
[root@client ~ 16:13:32]# curl http://www.lz.cloud/nginx2/
Welcome to nginx2.lz.cloud
# 显示结果是服务器 nginx2.lz.cloud 内容
# nginx3 后面必须添加 / 符号
[root@client ~ 16:13:35]# curl http://www.lz.cloud/nginx3/
Welcome to nginx3.lz.cloud
# 显示结果是服务器 nginx3.lz.cloud 内容
实验结果:精确匹配优先级高于正则表达式。



