Web安全
基础漏洞
01前端基础【HTML】
02前端基础【CSS】
03后端基础【PHP速通】
04后端基础【PHP面向对象】
05MySQL基础操作
06前后端联动【代码练习】
07SQL注入【1】
07SQL 注入【2】
08SQL注入 Labs
08SQL注入速查表
09XSS
09跨站脚本攻击【XSS】
09XSS Labs
10跨站请求伪造【CSRF】
11服务器端请求伪造【SSRF】
12XML 外部实体注入【XXE】
13代码执行漏洞
14命令执行漏洞
15文件包含漏洞
16文件上传漏洞
17反序列化漏洞
18业务逻辑漏洞
19未授权访问漏洞集合
20跨源资源共享【CORS】
21SSTI模板注入
22并发漏洞
23点击劫持【Clickjacking 】
24请求走私
25路径遍历
26访问控制
27身份验证漏洞
28WebSocket
29Web缓存中毒
30HTTP 主机头攻击
31信息泄露漏洞
32原型污染
33NoSQL注入
API 安全
01web应用程序
02HTTP协议
03API概述
04分类类型
05交换格式
06身份验证
07常见API漏洞
08crAPI靶场
09JWT
10OAuth 2.0身份验证
11GraphQL【1】
11GraphQL【2】
12DVGA靶场
13服务器端参数污染
14API文档
15API Labs
16OAuth Labs
17GraphQL API Labs
18JWT Labs
小程序
小程序抓包
数据库
MySQL
Oracle
MongoDB
Redis
PostgreSQL
SQL server
中间件
Nginx
Apache HTTP Server
IIS
Tomcat
框架
ThinkPHP
Spring
Spring Boot
Django
访问控制
-
+
首页
Nginx
 ## 概述 Nginx (发音为 "engine-x") 是一个高性能的 **开源 Web 服务器**。同时,它也可用作**反向代理**、**负载均衡器**、**邮件代理** 和 **HTTP 缓存**。 由俄罗斯程序员伊戈尔·赛索耶夫为解决著名的 **C10K 问题**(即单机同时处理一万个连接的问题)而开发,并于2004年首次公开发布。 Nginx 以其**高并发性能、低内存占用、高稳定性和丰富的功能集**而闻名,如今已成为全球最流行的 Web 服务器之一(据 Netcraft 统计,2024 年全球约 35% 的活跃网站使用 Nginx)。 官网地址:https://nginx.org 官方文档地址:https://docs.nginx.com ## 特点 **事件驱动、异步架构**: - 与传统的 Apache 等使用多进程或多线程(一个连接对应一个进程/线程)的服务器不同,Nginx 使用一个单一的**主进程**和多个**工作进程**的模型。 - 工作进程使用**非阻塞、事件驱动**的机制来处理多个连接。这使得它在处理高并发连接时,内存和 CPU 占用率极低,效率远超传统架构。 **高性能、高并发**:专为优化高并发连接而设计,能够在单台服务器上轻松处理数万个并发连接,是构建高性能网站的首选。 **低内存消耗**:其高效的事件驱动模型意味着即使在高负载下,它也能保持极少的内存消耗。 **高扩展性**:基于模块化的设计。核心功能由官方模块提供,用户还可以通过第三方模块来扩展其功能。 **高可靠性与稳定性**:以长时间稳定运行而著称,即使在重负载下也能平稳工作,通常无需重启。 **跨平台兼容性**:支持 Linux、Windows、FreeBSD 等主流操作系统,在 Linux 环境下性能最优(充分利用 epoll IO 多路复用)。 ## 核心功能 **静态内容服务:**高效地提供 HTML、CSS、JavaScript、图像等静态文件。这是其最基本也是最擅长的功能。 **反向代理:** - 作为中间代理服务器,接收客户端请求,并将其转发到后端的一台或多台服务器(如 Node.js, Tomcat, Gunicorn 等),然后将响应返回给客户端。 - 好处:隐藏后端服务器信息、实现负载均衡、提高安全性。 **负载均衡:** - 作为反向代理的一部分,Nginx 可以将流量分发到多个后端服务器,以避免任何单一服务器过载。 - 支持算法:轮询、加权轮询、最少连接、IP Hash 等。 **SSL/TLS 终止:** - 可以处理 HTTPS 连接的加密和解密工作。客户端与 Nginx 建立安全的 HTTPS 连接,而 Nginx 可以以普通的 HTTP 协议与后端服务器通信,从而减轻后端服务器的计算压力。 **HTTP 缓存:** - 可以缓存后端服务器的响应。对于相同的请求,Nginx 可以直接从缓存中返回内容,极大减轻后端压力并加速响应速度。 **压缩:** - 使用 Gzip 等方法压缩响应数据,减少网络传输时间。 **虚拟主机:** - 基于域名或 IP,在单一服务器上配置和运行多个网站。 **访问控制与安全:** - 支持基于 IP 的访问限制、HTTP 基本身份验证、限制请求速率(防 DDoS)等功能。 ## 适用场景 Nginx 的灵活性使其适用于从个人博客到大型分布式系统的各类场景: **静态网站托管**:个人博客、企业官网、前端单页应用(Vue/React 打包产物)等,直接通过 Nginx 提供服务,无需后端参与。 **反向代理与负载均衡**:微服务架构中作为入口网关,转发请求到各业务服务;高并发系统(如电商、社交平台)中分发流量到多台应用服务器。 **API 网关前置**:在专业 API 网关(如 Spring Cloud Gateway)前作为 "前置层",处理静态资源、HTTPS 终结、基础限流,减轻 API 网关压力。 **CDN 节点**:作为 CDN(内容分发网络)的边缘节点,缓存静态资源并向用户就近提供服务(如 Cloudflare 部分节点基于 Nginx 改造)。 **高并发场景**:秒杀活动、直播平台、API 服务等需要支撑数万级并发的场景,依赖 Nginx 的高性能特性。 ## 优劣势 | 优势 | 劣势 | | ----------------------------------- | -------------------------------------------- | | 高并发支持(万级 + 并发连接) | 配置语法特殊(非类 C 语法),初学者需适应 | | 资源消耗低(内存 / CPU 利用率优) | 动态模块支持有限(部分模块需重新编译) | | 热部署能力(无中断更新配置 / 程序) | 复杂功能(如高级缓存策略)配置较繁琐 | | 模块化设计,可按需扩展 | Windows 环境下性能弱于 Linux(不支持 epoll) | | 社区活跃,文档丰富,生态成熟 | 对动态内容(如 PHP)的原生支持需依赖 FastCGI | ## 目录结构 Nginx 的目录结构在不同操作系统(如 Linux、Windows)上略有差异,**以 Linux 系统(如 Ubuntu/Debian)为例**,其目录结构遵循 FHS(Filesystem Hierarchy Standard)规范,核心目录和文件的功能清晰且模块化。 Nginx 的主要目录集中在 /etc/nginx(配置文件)、/var/log/nginx(日志)、/usr/sbin(执行程序)等路径,整体结构如下:。 ```text /etc/nginx/ # 主配置目录(核心) ├── nginx.conf # 主配置文件(全局设置) ├── sites-available/ # 所有可用的站点配置(存放未启用或备用的站点) │ └── default # 默认站点配置(安装后自带) ├── sites-enabled/ # 已启用的站点配置(通过软链接指向sites-available中的文件) ├── conf.d/ # 额外的配置片段(可被主配置引用,通常用于自定义server块) ├── snippets/ # 可复用的配置片段(如SSL通用配置、限流规则) ├── modules-available/ # 可用的模块(Nginx 1.9.11+支持动态模块) └── modules-enabled/ # 已启用的模块(软链接指向modules-available) /var/log/nginx/ # 日志目录 ├── access.log # 访问日志(记录客户端请求信息:IP、URL、响应时间等) └── error.log # 错误日志(记录Nginx运行错误:配置错误、连接失败等) /usr/sbin/nginx # Nginx主程序(可执行文件) /usr/share/nginx/ # 静态资源和文档目录 └── html/ # 默认静态文件目录(存放欢迎页index.html、错误页50x.html) /var/cache/nginx/ # 缓存目录(如proxy_cache、fastcgi_cache的缓存文件) /var/run/nginx.pid # PID文件(存放Nginx主进程ID,用于进程管理) ``` **日志目录**:存放 Nginx 的运行日志,是排查问题的核心依据。 access.log:记录所有客户端请求信息,包括客户端 IP、请求时间、请求 URL、响应状态码(如 200/404)、响应大小、用户代理(浏览器标识)等。 ```txt 192.168.1.1 - - [12/Sep/2025:10:00:00 +0800] "GET /index.html HTTP/1.1" 200 1234 "https://example.com" "Mozilla/5.0..." ``` error.log:记录 Nginx 运行中的错误(如配置错误、文件不存在、后端服务连接失败等),包含错误级别(info/warn/error/crit)。 ```txt 2025/09/12 10:05:00 [error] 1234#1234: *5 open() "/var/www/html/missing.html" failed (2: No such file or directory), client: 192.168.1.1, server: example.com, request: "GET /missing.html HTTP/1.1", host: "example.com" ``` ## nginx.conf **nginx.conf**:主配置文件,Nginx 服务启动时读取的第一个文件,包含 Nginx 的核心参数,如进程数、连接数、日志格式、模块引用等并通过 include 指令组织其他配置文件。 语法规则: - 指令以分号 ; 结尾。 - 指令和参数之间用空格分隔。 - 使用 #{ } 来组织并创建一个新的配置上下文。 - 使用 # 进行注释。 上下文层次: ```nginx # 全局上下文 (最外层) directive; events { # events上下文 directive; } http { # http上下文 directive; server { # Server上下文 (虚拟主机) directive; location / { # Location上下文 (URI匹配) directive; } } } mail { # mail上下文 (邮件代理) directive; } ``` 指令会从外层上下文**继承**到内层上下文,但内层上下文可以覆盖外层的指令。 示例: ```nginx # 第一部分:全局上下文 (Main Context) # 这里的指令影响整个Nginx服务器。 user www-data; worker_processes auto; pid /run/nginx.pid; include /etc/nginx/modules-enabled/*.conf; # 第二部分:Events 上下文 # 配置影响Nginx如何处理连接的参数。 events { worker_connections 768; # multi_accept on; } # 第三部分:Http 上下文 # 这是配置HTTP服务器所有方面的核心上下文。它可以嵌套多个Server和Location上下文。 http { ## # 基础设置 ## sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; # server_tokens off; # server_names_hash_bucket_size 64; # server_name_in_redirect off; include /etc/nginx/mime.types; default_type application/octet-stream; ## # SSL 设置 ## ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE ssl_prefer_server_ciphers on; ## # 日志格式设置 ## access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; ## # Gzip 压缩设置 ## gzip on; # gzip_vary on; # gzip_proxied any; # gzip_comp_level 6; # gzip_buffers 16 8k; # gzip_http_version 1.1; # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; ## # 虚拟主机配置 # 通过include指令引入其他配置文件,这是模块化配置的关键! ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; # 这行引入了我们定义的网站配置! } ``` **1. 全局上下文 (Main Context):** - user www-data; - 定义 Nginx **工作进程** 的运行用户和组。www-data 是 Ubuntu 上 web 服务的标准用户,权限较低,有利于安全。 - worker_processes auto; - 定义 Nginx 使用的**工作进程**数量。 - auto 表示自动设置为与 CPU 核心数相同。这是性能调优的关键设置。 - pid /run/nginx.pid; - 指定存储 Nginx **主进程** ID 的文件路径。系统工具(如 systemctl)通过这个文件来管理服务。 - include /etc/nginx/modules-enabled/*.conf; - 动态加载在 modules-enabled 目录中启用的模块配置文件。 **2. Events 上下文:** - worker_connections 768; - **每个工作进程**能够同时处理的**最大连接数**。 - **最大总连接数** = worker_processes * worker_connections。本例中,如果是 4 核 CPU,最大并发连接数约为 4 * 768 = 3072。 - multi_accept on;(注释状态) - 如果启用,一个工作进程可以同时接受所有的新连接。否则,一次只接受一个新连接。 **3. Http 上下文** - sendfile on;:启用高效的文件传输模式,减少在用户空间和内核空间之间的上下文切换,提升静态文件服务性能。 - tcp_nopush on;:与 sendfile on 配合使用。它会在一个数据包中发送响应头和数据,减少网络数据包的数量。 - tcp_nodelay on;:禁用 Nagle 算法,允许立即发送小数据包,对于实时应用很有用。 - keepalive_timeout 65;:保持连接的超时时间(秒)。客户端在这个时间内可以复用同一个连接发送后续请求,减少 TCP 握手开销。 - include /etc/nginx/mime.types;:引入一个文件,该文件定义了文件扩展名到 MIME 类型(如 text/html, image/jpeg)的映射。这样 Nginx 才知道如何设置 Content-Type 响应头。 - default_type application/octet-stream;:如果文件无法匹配任何 MIME 类型,则使用此默认类型(即作为二进制流下载)。 - access_log /var/log/nginx/access.log;:定义默认的访问日志路径和格式(如果没有在 server 块中重新定义)。 - error_log /var/log/nginx/error.log;:定义错误日志的路径和级别。 - gzip on;:启用 Gzip 压缩,压缩文本类资源(HTML, CSS, JS, XML等),显著减少传输数据大小。 - 其他被注释的 gzip_* 指令允许进行更精细的压缩控制(如压缩级别、压缩类型)。 - include /etc/nginx/conf.d/*.conf;:包含 conf.d 目录下的所有 .conf 文件。通常用于不属于虚拟主机的全局 HTTP 配置片段。 - include /etc/nginx/sites-enabled/*;:**包含所有已启用的虚拟主机(网站)配置**。这是我们自定义网站配置的地方。这些文件通常是符号链接,指向 /etc/nginx/sites-available/ 目录中的实际文件。 ## 安装 本次实验,操作系统为:Ubuntu。 1、更新软件源列表并进行安装。 ```bash apt update apt install nginx -y ``` 2、检查 Nginx 服务状态。 ```bash root@test-VMware-Virtual-Platform:/home/test# systemctl status nginx ● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled) Active: active (running) since Fri 2025-09-12 15:48:24 CST; 3min 25s ago Docs: man:nginx(8) Process: 14754 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 14756 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 14788 (nginx) Tasks: 5 (limit: 4540) Memory: 3.7M (peak: 8.5M) CPU: 29ms CGroup: /system.slice/nginx.service ├─14788 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;" ├─14790 "nginx: worker process" ├─14791 "nginx: worker process" ├─14792 "nginx: worker process" └─14793 "nginx: worker process" 9月 12 15:48:24 test-VMware-Virtual-Platform systemd[1]: Starting nginx.service - A high performance web server and a reverse proxy server... 9月 12 15:48:24 test-VMware-Virtual-Platform systemd[1]: Started nginx.service - A high performance web server and a reverse proxy server. ``` 如果服务没有自动启动,请启动它: ```bash systemctl start nginx ``` 设置 Nginx 开机自启: ```bash systemctl enable nginx ``` 3、使用浏览器访问当前实验机器的IP地址即可。  4、网站页面的源文件默认位于/var/www/html。 ```bash root@test-VMware-Virtual-Platform:/var/www/html# ll 总计 12 drwxr-xr-x 2 root root 4096 9月 12 15:48 ./ drwxr-xr-x 3 root root 4096 9月 12 15:48 ../ -rw-r--r-- 1 root root 615 9月 12 15:48 index.nginx-debian.html root@test-VMware-Virtual-Platform:/var/www/html# cat index.nginx-debian.html <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> ``` 5、配置文件。 ```bash root@test-VMware-Virtual-Platform:/var/www/html# cd /etc/nginx/ root@test-VMware-Virtual-Platform:/etc/nginx# ll ./ 总计 84 drwxr-xr-x 8 root root 4096 9月 12 15:48 ./ drwxr-xr-x 141 root root 12288 9月 12 15:48 ../ drwxr-xr-x 2 root root 4096 8月 22 20:45 conf.d/ -rw-r--r-- 1 root root 1125 12月 1 2023 fastcgi.conf -rw-r--r-- 1 root root 1055 12月 1 2023 fastcgi_params -rw-r--r-- 1 root root 2837 12月 1 2023 koi-utf -rw-r--r-- 1 root root 2223 12月 1 2023 koi-win -rw-r--r-- 1 root root 5465 12月 1 2023 mime.types drwxr-xr-x 2 root root 4096 8月 22 20:45 modules-available/ drwxr-xr-x 2 root root 4096 8月 22 20:45 modules-enabled/ -rw-r--r-- 1 root root 1446 12月 1 2023 nginx.conf -rw-r--r-- 1 root root 180 12月 1 2023 proxy_params -rw-r--r-- 1 root root 636 12月 1 2023 scgi_params drwxr-xr-x 2 root root 4096 9月 12 15:48 sites-available/ drwxr-xr-x 2 root root 4096 9月 12 15:48 sites-enabled/ drwxr-xr-x 2 root root 4096 9月 12 15:48 snippets/ -rw-r--r-- 1 root root 664 12月 1 2023 uwsgi_params -rw-r--r-- 1 root root 3071 12月 1 2023 win-utf root@test-VMware-Virtual-Platform:/etc/nginx# ``` 初始化的nginx.conf配置文件: ```bash root@test-VMware-Virtual-Platform:/etc/nginx# cat nginx.conf user www-data; worker_processes auto; pid /run/nginx.pid; error_log /var/log/nginx/error.log; include /etc/nginx/modules-enabled/*.conf; events { worker_connections 768; # multi_accept on; } http { ## # Basic Settings ## sendfile on; tcp_nopush on; types_hash_max_size 2048; # server_tokens off; # server_names_hash_bucket_size 64; # server_name_in_redirect off; include /etc/nginx/mime.types; default_type application/octet-stream; ## # SSL Settings ## ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE ssl_prefer_server_ciphers on; ## # Logging Settings ## access_log /var/log/nginx/access.log; ## # Gzip Settings ## gzip on; # gzip_vary on; # gzip_proxied any; # gzip_comp_level 6; # gzip_buffers 16 8k; # gzip_http_version 1.1; # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; ## # Virtual Host Configs ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; } #mail { # # See sample authentication script at: # # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript # # # auth_http localhost/auth.php; # # pop3_capabilities "TOP" "USER"; # # imap_capabilities "IMAP4rev1" "UIDPLUS"; # # server { # listen localhost:110; # protocol pop3; # proxy on; # } # # server { # listen localhost:143; # protocol imap; # proxy on; # } #} root@test-VMware-Virtual-Platform:/etc/nginx# ``` ## 卸载 1、首先停止该服务。 ```bash root@test-VMware-Virtual-Platform:/etc/nginx# systemctl stop nginx root@test-VMware-Virtual-Platform:/etc/nginx# systemctl status nginx ○ nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled) Active: inactive (dead) since Fri 2025-09-12 15:59:47 CST; 25s ago Duration: 11min 23.405s Docs: man:nginx(8) Process: 14754 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 14756 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 14905 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS) Main PID: 14788 (code=exited, status=0/SUCCESS) CPU: 35ms 9月 12 15:48:24 test-VMware-Virtual-Platform systemd[1]: Starting nginx.service - A high performance web server and a reverse proxy server... 9月 12 15:48:24 test-VMware-Virtual-Platform systemd[1]: Started nginx.service - A high performance web server and a reverse proxy server. 9月 12 15:59:47 test-VMware-Virtual-Platform systemd[1]: Stopping nginx.service - A high performance web server and a reverse proxy server... 9月 12 15:59:47 test-VMware-Virtual-Platform systemd[1]: nginx.service: Deactivated successfully. 9月 12 15:59:47 test-VMware-Virtual-Platform systemd[1]: Stopped nginx.service - A high performance web server and a reverse proxy server. root@test-VMware-Virtual-Platform:/etc/nginx# ``` 2、使用 apt purge 命令。与 apt remove 不同,purge 会**同时删除软件包及其配置文件**。 ```bash sudo apt purge nginx nginx-common nginx-core -y ``` - nginx: 主软件包 - nginx-common: 包含一些共享文件和配置 - nginx-core: Nginx 的核心二进制文件 3、删除残留的目录和文件,即使使用 purge,一些运行时创建的数据和日志文件可能仍然存在。手动删除它们以进行彻底清理: ```bash # 删除网站文件(如果确定不需要了) sudo rm -rf /var/www/html # 删除日志文件 sudo rm -rf /var/log/nginx # 删除残留的配置和缓存文件(通常 purge 会处理,但确保一下) sudo rm -rf /etc/nginx sudo rm -rf /var/lib/nginx sudo rm -rf /var/cache/nginx ``` 4、清除未使用的依赖包。 ```bash apt autoremove ``` 5、检查 Nginx 是否已完全移除。 ```bash #检查进程,应该只有 grep 进程本身,没有 nginx 主进程或工作进程。 ps aux | grep nginx #检查软件包状态 dpkg -l | grep nginx # 检查端口 netstat -tulpn | grep :80 ```
毛林
2025年9月12日 20:35
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
PDF文档(打印)
分享
链接
类型
密码
更新密码