nginx
2026.10.28(周三)第三天
什么是Nginx?
Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(Polev.net)开发的,第一个公开版本0.1.0发布于2004年10月4日。2011年6月1日,nginx 1.0.4发布。
特点:占用内存少,并发能力强。国内百度、京东、新浪、网易、腾讯、淘宝都在使用。全球活跃网站使用率约12.18%,大约2220万个网站。
Nginx安装简单、配置简洁、bug少;支持7*24不间断运行,支持不停机升级。
Nginx全部使用C语言编写,官方测试最高支持50000个并发连接响应。
正向代理和反向代理
- 正向代理:代理客户端,典型例子:VPN
- 反向代理:代理服务器,接收客户端请求,转发到后端不同服务器
负载均衡内置策略:
- 轮询:按顺序轮流分发请求
- 加权轮询:性能更好的服务器设置更高权重,提升命中率
- ip hash:根据客户端ip做hash,同一ip固定访问同一后端,解决session共享问题
还有很多扩展负载均衡策略
动静分离
软件开发中,请求分为静态资源请求(css、html、jpg、js等)和需要后台处理的动态请求。
动静分离:区分静态、动态资源,静态资源交给Nginx缓存处理,动态请求转发后端,提升访问速度。
Nginx常用命令
bash
# 进入sbin目录
cd /usr/local/nginx/sbin/
# 启动
./nginx
# 停止
./nginx -s stop
# 安全退出
./nginx -s quit
# 重载配置(修改配置后使用)
./nginx -s reload
# 查看nginx进程
ps aux|grep nginx
Conf配置文件结构
- 全局块:文件开头到 events 块之前,设置 worker 进程、日志路径、pid 文件等全局参数
- events块:网络连接相关,最大连接数、事件模型等
- http块 :核心配置块,包含代理、缓存、mime类型等,内部可以包含多个server块
- server块:虚拟主机,一个server代表一个网站
- location块:在server内部,匹配请求uri,处理转发、静态资源、重定向等
基础完整 nginx.conf 示例
nginx
worker_processes 1;
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
Nginx 常用优化配置
1. 前端vue history路由模式(解决刷新404)
nginx
location / {
try_files $uri $uri/ /index.html;
}
作用:url找不到对应文件时,返回index.html,由前端路由接管页面
2. 反向代理(解决跨域)
思路:开发用proxy代理,生产环境使用nginx反向代理
nginx
location /police/ {
proxy_pass http://192.168.1.182:8852/police/;
proxy_redirect default;
proxy_http_version 1.1;
proxy_connect_timeout 60;
proxy_send_timeout 60;
proxy_read_timeout 60;
}
说明:访问
/police/开头接口,转发到后端192.168.1.182:8852,后端返回结果经由nginx交给浏览器
3. 开启gzip压缩(减小静态资源体积,加快加载)
nginx
gzip on;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_static on;
gzip_proxied any;
gzip_vary on;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_min_length 1k;
4. 网站维护页面
nginx
# 系统临时维护请打开下面这行,维护完毕注释,再重载nginx
# rewrite ^(.*)$ /maintenance.html break;
5. 单IP部署多个网站(多虚拟主机)
一个server块对应一个站点,修改listen端口、root资源路径即可
nginx
# 第一个网站:个人博客项目配置
server {
listen 8080;
root /data/www/hexo;
index index.html;
location / {
try_files $uri $uri/ /index.html; # history路由适配
}
}
# 第二个网站:直播网站项目配置
server {
listen 8081;
root /data/www/geov;
index index.html;
location / {
}
}
6. 动静分离说明
在Web开发中,动态资源是后台接口;静态资源为 HTML、JavaScript、CSS、img 等文件。
- 静态资源请求:Nginx直接读取本地静态文件返回
- 动态接口请求:Nginx反向代理转发给后端服务
前后端分离项目打包出来的dist包属于纯静态资源,可以直接交给Nginx托管;接口请求走反向代理转发后端,提升访问速度,前后端可以并行开发。
补充:CORS跨域配置
注意:跨域头只在一处配置,Nginx加了后端就不要重复添加
开发快速版(允许全部源,生产禁止使用)
nginx
location /api/ {
if ($request_method = 'OPTIONS') {
add_header Access-Control-Allow-Origin * always;
add_header Access-Control-Allow-Methods "GET,POST,PUT,DELETE,OPTIONS" always;
add_header Access-Control-Allow-Headers "Content-Type,Authorization,X-Requested-With" always;
add_header Access-Control-Max-Age 86400 always;
return 204;
}
add_header Access-Control-Allow-Origin * always;
add_header Access-Control-Allow-Methods "GET,POST,PUT,DELETE,OPTIONS" always;
add_header Access-Control-Allow-Headers "Content-Type,Authorization,X-Requested-With" always;
proxy_pass http://127.0.0.1:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
生产安全版(指定域名,支持Authorization、Cookie凭证)
nginx
location /api/ {
if ($request_method = 'OPTIONS') {
add_header Access-Control-Allow-Origin https://xxx-front.com always;
add_header Access-Control-Allow-Methods "GET,POST,PUT,DELETE,OPTIONS" always;
add_header Access-Control-Allow-Headers "Content-Type,Authorization,X-Requested-With" always;
add_header Access-Control-Allow-Credentials true always;
add_header Access-Control-Max-Age 86400 always;
return 204;
}
add_header Access-Control-Allow-Origin https://xxx-front.com always;
add_header Access-Control-Allow-Credentials true always;
proxy_pass http://127.0.0.1:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}