Nginx 介绍
Nginx(发音为 "engine X")是一款高性能、轻量级的开源Web服务器 和反向代理服务器。由俄罗斯程序员Igor Sysoev于2004年首次发布,最初旨在解决C10K问题(即同时处理一万个并发连接)。
核心特点
1. 高性能与高并发
- 采用事件驱动的异步架构,而非传统的多线程模型
- 单个进程可以处理成千上万的并发连接
- 内存占用极低(通常在几MB到几十MB)
2. 多用途能力
Nginx不仅可以作为Web服务器,还能扮演多种角色:
| 功能 | 描述 |
|---|---|
| Web服务器 | 托管静态文件(HTML、CSS、JS、图片等) |
| 反向代理 | 接收客户端请求并转发到后端服务器(如Tomcat、Node.js) |
| 负载均衡 | 在多个后端服务器之间分发流量 |
| HTTP缓存 | 缓存响应内容,减轻后端压力 |
| SSL/TLS终止 | 处理HTTPS加密/解密 |
主要使用场景
-
静态资源托管
高效处理图片、CSS、JavaScript等静态文件
-
反向代理与负载均衡
text
客户端 → Nginx → 后端服务器集群 -
API网关
路由、限流、认证等前置处理
-
微服务架构入口
作为服务的统一入口点
-
CDN边缘节点
内容分发网络的核心组件
核心概念
进程模型
- Master进程:读取配置、管理工作进程
- Worker进程:实际处理请求,数量通常等于CPU核心数
配置文件结构
nginx
# 全局块
user nginx;
worker_processes auto;
# events块
events {
worker_connections 1024;
}
# http块
http {
# 虚拟主机配置
server {
listen 80;
server_name example.com;
location / {
root /var/www/html;
index index.html;
}
}
}
常见配置示例
1. 静态文件服务
nginx
server {
listen 80;
server_name static.example.com;
location / {
root /data/www;
expires 30d; # 缓存30天
}
}
2. 反向代理
nginx
server {
listen 80;
server_name api.example.com;
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
3. 负载均衡
nginx
upstream backend {
server 192.168.1.10 weight=3; # weight越高,分配越多请求
server 192.168.1.11;
server 192.168.1.12 backup; # 备用服务器
}
server {
location / {
proxy_pass http://backend;
}
}
优势对比
| 特性 | Nginx | Apache | IIS |
|---|---|---|---|
| 并发处理 | 异步非阻塞 | 进程/线程 | 异步 |
| 静态文件性能 | 极高 | 中等 | 中等 |
| 配置灵活性 | 中等 | 高 | 中等 |
| 内存占用 | 极低 | 较高 | 较高 |
| 动态内容处理 | 通过代理 | 内置模块 | 内置 |
扩展功能
Nginx通过模块系统支持丰富功能:
- 安全模块:访问控制、限流、WAF
- 缓存模块:FastCGI缓存、代理缓存
- 压缩模块:gzip压缩减少传输量
- 日志模块:访问日志、错误日志
- 第三方模块:如Lua集成(OpenResty)、HTTP/3支持
总结
Nginx已成为现代Web架构中不可或缺的组件,凭借其高性能、低资源消耗和多功能性,被全球超过三分之一的网站使用(包括Netflix、Dropbox、GitHub等巨头)。无论是小型个人项目还是大型分布式系统,Nginx都能提供稳定可靠的解决方案。
掌握Nginx对于Web开发者和运维工程师来说,是一项极具价值的技能。
实战
一、准备
1.下载软件
bash
[root@Nginx ~]# wget https://nginx.org/download/nginx-1.28.1.tar.gz
2.解压
bash
[root@Nginx ~]# wget https://nginx.org/download/nginx-1.28.1.tar.gz
3.检测环境
bash
#安装依赖性
[root@Nginx ~]# dnf install gcc openssl-devel.x86_64 pcre2-devel.x86_64 zlib-devel -y
[root@Nginx nginx-1.28.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
4.编译
bash
[root@Nginx nginx-1.28.1]# make
[root@Nginx nginx-1.28.1]# make install
5.nginx启动
bash
#设定环境变量
[root@Nginx sbin]# vim ~/.bash_profile
export PATH=$PATH:/usr/local/nginx/sbin
[root@Nginx sbin]# source ~/.bash_profile
[root@Nginx logs]# useradd -s /sbin/nologin -M nginx
[root@Nginx logs]# nginx
[root@Nginx logs]# ps aux | grep nginx
root 44012 0.0 0.1 14688 2356 ? Ss 17:01 0:00 nginx: master process nginx
nginx 44013 0.0 0.2 14888 3892 ? S 17:01 0:00 nginx: worker process
root 44015 0.0 0.1 6636 2176 pts/0 S+ 17:01 0:00 grep --color=auto nginx
#测试
[root@Nginx logs]# echo pengchen > /usr/local/nginx/html/index.html
[root@Nginx logs]# curl 172.25.254.100
pengchen
二、反向代理
Nginx反向代理
1.实验环境
bash
#172.25.254.10 RS1 172.25.254.20 RS2
[root@RSX ~]# dnf install httpd -y
[root@RSX ~]# systemctl enable --now httpd
[root@RSX ~]# echo 172.25.254.20 > /var/www/html/index.html
#测试 在Nginx主机中
[root@Nginx ~]# curl 172.25.254.10
172.25.254.10
[root@Nginx ~]# curl 172.25.254.20
172.25.254.20
2.简单的代理方法
bash
[root@RS2 ~]# mkdir /var/www/html/web
[root@RS2 ~]# echo 172.25.254.20 web > /var/www/html/web/index.html
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location / {
proxy_pass http://172.25.254.10:80;
}
location /web {
proxy_pass http://172.25.254.20:80;
}
}
[root@Nginx ~]# nginx -s reload
#测试
[root@Nginx ~]# curl 172.25.254.20/web/
172.25.254.20 web
[root@Nginx ~]# curl 172.25.254.10
172.25.254.10
3.proxy_hide_header filed
bash
[Administrator.DESKTOP-VJ307M3] ➤ curl -v lee.timinglee.org
* Trying 172.25.254.100:80...
* TCP_NODELAY set
* Connected to lee.timinglee.org (172.25.254.100) port 80 (#0)
> GET / HTTP/1.1
> Host: lee.timinglee.org
> User-Agent: curl/7.65.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: nginx/1.28.1
< Date: Tue, 03 Feb 2026 06:31:03 GMT
< Content-Type: text/html; charset=UTF-8
< Content-Length: 14
< Connection: keep-alive
< Keep-Alive: timeout=100
< Last-Modified: Tue, 03 Feb 2026 06:20:50 GMT
< ETag: "e-649e570e8a49f" #可以看到ETAG信息
< Accept-Ranges: bytes
<
172.25.254.10
* Connection #0 to host lee.timinglee.org left intact
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location / {
proxy_pass http://172.25.254.10:80;
proxy_hide_header ETag;
}
location /web {
proxy_pass http://172.25.254.20:80;
}
}
[root@Nginx ~]# nginx -s reload
#测试
[Administrator.DESKTOP-VJ307M3] ➤ curl -v lee.timinglee.org
* Trying 172.25.254.100:80...
* TCP_NODELAY set
* Connected to lee.timinglee.org (172.25.254.100) port 80 (#0)
> GET / HTTP/1.1
> Host: lee.timinglee.org
> User-Agent: curl/7.65.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: nginx/1.28.1
< Date: Tue, 03 Feb 2026 06:33:11 GMT
< Content-Type: text/html; charset=UTF-8
< Content-Length: 14
< Connection: keep-alive
< Keep-Alive: timeout=100
< Last-Modified: Tue, 03 Feb 2026 06:20:50 GMT
< Accept-Ranges: bytes
<
172.25.254.10
4.proxy_pass_header
bash
[Administrator.DESKTOP-VJ307M3] ➤ curl -v lee.timinglee.org
* Trying 172.25.254.100:80...
* TCP_NODELAY set
* Connected to lee.timinglee.org (172.25.254.100) port 80 (#0)
> GET / HTTP/1.1
> Host: lee.timinglee.org
> User-Agent: curl/7.65.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: nginx/1.28.1 #默认访问不透传server信息
< Date: Tue, 03 Feb 2026 06:35:35 GMT
< Content-Type: text/html; charset=UTF-8
< Content-Length: 14
< Connection: keep-alive
< Keep-Alive: timeout=100
< Last-Modified: Tue, 03 Feb 2026 06:20:50 GMT
< Accept-Ranges: bytes
<
172.25.254.10
* Connection #0 to host lee.timinglee.org left intact
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location / {
proxy_pass http://172.25.254.10:80;
proxy_pass_header Server;
}
location /web {
proxy_pass http://172.25.254.20:80;
}
}
[root@Nginx ~]# nginx -s reload
Administrator.DESKTOP-VJ307M3] ➤ curl -v lee.timinglee.org
* Trying 172.25.254.100:80...
* TCP_NODELAY set
* Connected to lee.timinglee.org (172.25.254.100) port 80 (#0)
> GET / HTTP/1.1
> Host: lee.timinglee.org
> User-Agent: curl/7.65.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Tue, 03 Feb 2026 06:37:25 GMT
< Content-Type: text/html; charset=UTF-8
< Content-Length: 14
< Connection: keep-alive
< Keep-Alive: timeout=100
< Server: Apache/2.4.62 (Red Hat Enterprise Linux) #透传结果
< Last-Modified: Tue, 03 Feb 2026 06:20:50 GMT
< Accept-Ranges: bytes
<
172.25.254.10
* Connection #0 to host lee.timinglee.org left intact
5.透传信息
bash
[root@RS1 ~]# vim /etc/httpd/conf/httpd.conf
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{X-Forwarded-For}i\"" combined
[root@RS1 ~]# systemctl restart httpd
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location / {
proxy_pass http://172.25.254.10:80;
proxy_set_header X-Forwarded-For $remote_addr;
}
location /web {
proxy_pass http://172.25.254.20:80;
}
[root@Nginx ~]# nginx -s reload
[Administrator.DESKTOP-VJ307M3] ➤ curl lee.timinglee.org
172.25.254.10
[root@RS1 ~]# cat /etc/httpd/logs/access_log
172.25.254.100 - - [03/Feb/2026:14:47:37 +0800] "GET / HTTP/1.0" 200 14 "-" "curl/7.65.0" "172.25.254.1"
三、负载均衡
bash
Nginx负载均衡算法
```
[root@Nginx ~]# vim /usr/local/nginx/conf/upstream/loadbalance.conf
upstream webserver {
#ip_hash;
#hash $request_uri consistent;
#least_conn;
hash $cookie_lee;
server 172.25.254.10:80 weight=1 fail_timeout=15s max_fails=3;
server 172.25.254.20:80 weight=1 fail_timeout=15s max_fails=3;
#server 172.25.254.100:8888 backup;
}
server {
listen 80;
server_name www.timinglee.org;
location ~ / {
proxy_pass http://webserver;
}
}
#
[root@Nginx ~]# curl -b lee=20 www.timinglee.org
[root@Nginx ~]# curl www.timinglee.org/web1/index.html
[root@Nginx ~]# curl www.timinglee.org/
```