1. Nginx 初探:从入门到精通的必经之路
第一次接触Nginx是在2013年,当时公司的一个关键项目需要处理突发的高并发请求。Apache在压力测试中频频崩溃,而换上Nginx后,服务器就像吃了定心丸一样稳如泰山。从那时起,这个轻量级的高性能Web服务器就成了我技术栈中不可或缺的利器。
Nginx(发音为"engine x")最初由俄罗斯工程师Igor Sysoev开发,如今已成为全球最受欢迎的Web服务器之一。根据W3Techs的最新统计,全球约有34%的网站使用Nginx,远超Apache的23%。它的核心竞争力在于事件驱动的异步架构,能够用极少的资源处理海量并发连接,这在高并发的互联网时代简直就是救星。
提示:Nginx不仅仅是个Web服务器,它集反向代理、负载均衡、HTTP缓存等功能于一身,是现代Web架构的瑞士军刀。
需要模型API调用? 免费领10W Token,多模型网关一键接入 Claude、DeepSeek 等主流模型。
2. Nginx 核心功能深度解析
2.1 反向代理:企业级应用的流量指挥官
反向代理是Nginx最常用的功能之一。我们的电商平台曾面临这样的困境:前端需要调用多个微服务(用户服务、商品服务、订单服务等),如果让浏览器直接访问这些服务,不仅暴露了内部架构,还会遇到跨域问题。通过Nginx配置反向代理,完美解决了这些问题:
nginx复制server {
listen 80;
server_name api.example.com;
location /user/ {
proxy_pass http://user-service:8080;
}
location /product/ {
proxy_pass http://product-service:8081;
}
location /order/ {
proxy_pass http://order-service:8082;
}
}
这个配置将所有/api/user请求转发到用户服务,/api/product转发到商品服务,对外却只暴露api.example.com这一个域名。实测下来,这种方案比在每个服务端配置CORS简单可靠得多。
2.2 负载均衡:高可用架构的基石
当单台服务器无法承受流量压力时,Nginx的负载均衡功能就派上用场了。去年双十一大促,我们通过以下配置将流量均匀分发到5台应用服务器:
nginx复制upstream app_servers {
server 192.168.1.101:8080 weight=3;
server 192.168.1.102:8080;
server 192.168.1.103:8080;
server 192.168.1.104:8080 backup;
server 192.168.1.105:8080 down;
}
这里有几个关键点:
- weight=3表示101服务器将获得3倍于其他服务器的流量
- backup标记的服务器只在其他服务器不可用时才启用
- down标记的服务器暂时不参与负载均衡
注意:Nginx默认采用轮询策略,还有ip_hash(保持会话)、least_conn(最少连接)等策略可选,根据业务特点选择合适的方式很重要。
2.3 静态资源服务:性能提升的捷径
Nginx处理静态资源(图片、CSS、JS等)的效率极高。我们曾将Apache服务的静态资源迁移到Nginx,页面加载时间直接从4.2秒降到了1.8秒。关键配置如下:
nginx复制server {
listen 80;
server_name static.example.com;
location / {
root /data/static;
expires 30d;
access_log off;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires max;
add_header Cache-Control public;
}
}
这个配置做了三件重要的事:
- 关闭访问日志减少磁盘IO
- 设置长期缓存减少重复请求
- 使用单独的域名避免携带Cookie
3. Nginx 安装与配置实战
3.1 Linux环境下的安装指南
在CentOS 7上安装Nginx的标准步骤:
bash复制# 添加EPEL仓库
sudo yum install epel-release
# 安装Nginx
sudo yum install nginx
# 启动服务
sudo systemctl start nginx
# 设置开机启动
sudo systemctl enable nginx
# 检查状态
sudo systemctl status nginx
如果是Ubuntu系统,只需将yum换成apt-get即可。但生产环境我强烈建议从官方源安装最新稳定版:
bash复制# CentOS
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
sudo yum install nginx
# Ubuntu
sudo apt-get install curl gnupg2 ca-certificates lsb-release
echo "deb http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo apt-key add -
sudo apt-get update
sudo apt-get install nginx
3.2 配置文件结构解析
Nginx的配置文件通常位于/etc/nginx/nginx.conf,其结构遵循模块化设计:
code复制main上下文 (全局配置)
├── events (连接处理配置)
└── http (HTTP服务配置)
├── server (虚拟主机配置)
│ ├── location (URI匹配配置)
│ └── location
└── server
一个完整的配置示例:
nginx复制user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
use epoll;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
gzip on;
gzip_types text/plain t
