Nginx 的安装部署
目录
一、Nginx的部署安装
1、安装编译工具及库文件
2、安装pcre
3、nginx安装
二、nginx 反向代理配置
1、访问本机IP跳转至百度主页
2、访问相同主机地址,路径不同跳转不同
三、nginx 负载均衡配置
1、轮询负载(顺序分配)
2、权重负载(weight占比越高,分配越多)
3、IP 哈希(IP Hash)
4、最少连接(Least Connections)
四、nginx.conf 配置详解
1、全局块
2、event 模块
3、http 块
4、server 块(虚拟主机配置)
5.、location 块
6、upstream 块
7、mail 模块(邮件代理)
8、总结
五、nginx 基础知识
1、什么是nginx
2、Nginx的配置文件结构是怎样的?
3、Nginx支持哪些负载均衡算法
4、如何实现Nginx的热部署?
5、什么是Nginx的master-worker模式?
6、Nginx 性能优化
一、Nginx的部署安装
1、安装编译工具及库文件
sudo su - root
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
2、安装pcre
注:已安装,则跳过
PCRE 在 Nginx 中的作用包括:
- 1)正则表达式匹配:PCRE 提供了功能强大且灵活的正则表达式引擎,可以在 Nginx 的配置中使用正则表达式进行 URL 重定向、反向代理等操作。
- 2)模块功能:Nginx 的一些模块需要依赖 PCRE 来实现特定功能,如 ngx_http_rewrite_module 模块就需要 PCRE 来支持正则表达式的使用。
- 3)性能优化:PCRE 库本身经过优化,能够提高 Nginx 对正则表达式的解析速度和效率,从而加速请求处理过程。
PCRE 安装包官网下载https://sourceforge.net/projects/pcre/files/pcre/8.35/pcre-8.35.tar.gz/download?use_mirror=jaist
将安装包上传到 /usr/local/ 目录下
sudo su - root
cd /usr/local/
tar -zxvf pcre-8.35.tar.gz
./configure
# 安装
make && make install
# 验证
pcre-config --version
3、nginx安装
nginx官网下载https://nginx.org/en/download.html
sudo su - root# 创建一个 nginx 专用账号
useradd lb
passwd lb# 将 nginx 安装包上传到 /home/lb/ 目录下
su - lb
tar -zxvf nginx-1.28.0.tar.gz
cd nginx-1.28.0# 编译安装 --with-http_ssl_module 启用ngx_http_ssl_module支持
cd nginx./configure \--prefix=/home/lb/nginx \--user=lb \--group=lb \--with-http_ssl_module \--with-http_v2_module \--with-stream \--with-threadsmake
make install# 配置环境变量
echo 'export PATH=$PATH:/home/lb/nginx/sbin' >> ~/.bashrc
source ~/.bashrc# 创建必要的文件和目录
mkdir -p /home/lb/nginx/logs
touch /home/lb/nginx/logs/{access.log,error.log}
chmod -R 755 /home/lb/nginx# 查看nginx 版本
nginx -v# 更改nginx的端口,1024 以下的端口是特权端口,只有 root 用户才能直接绑定。nginx默认绑定到80端口
vi ~/nginx/conf/nginx.conf
# 启动 nginx
sudo su - root
/home/lb/nginx/sbin/nginx
# 访问:http://192.168.254.100:19280/
二、nginx 反向代理配置
1、访问本机IP跳转至百度主页
sudo su - lb# 更改配置
vi /home/lb/nginx/conf/nginx.conf# 启动nginx
nginx
更改配置如下:
此时访问 server_name(192.168.254.100) 的19280端口,访问路径 / 会跳转到 https://www.baidu.com。
访问测试:
2、访问相同主机地址,路径不同跳转不同
sudo su - lb# 更改配置
vi /home/lb/nginx/conf/nginx.conf# 启动nginx
nginx
更改配置如下:
访问测试:
http://192.168.254.100:19280/xuexitong 跳转到学习通下载界面
http://192.168.254.100:19280/baidu 跳转到百度界面(百度网站跳转需要验证)
http://192.168.254.100:19280/ 跳转到nginx初始化界面
三、nginx 负载均衡配置
1、轮询负载(顺序分配)
sudo su - lb# 更改配置
vi /home/lb/nginx/conf/nginx.conf# 启动nginx
nginx
更改配置如下:
访问测试:
http://192.168.254.100:19280/lb
手动刷新界面,请求按顺序分配到不同的后端服务器。
2、权重负载(weight占比越高,分配越多)
sudo su - lb# 更改配置
vi /home/lb/nginx/conf/nginx.conf# 启动nginx
nginx
更改配置如下:
访问测试:
http://192.168.254.100:19280/lb
多次刷新界面,请求中有分配到8080端口的次数大概是分配到19280端口次数的4倍。测试不明显的话可以一个权重再调高点。
3、IP 哈希(IP Hash)
sudo su - lb# 更改配置
vi /home/lb/nginx/conf/nginx.conf# 启动nginx
nginx
更改配置如下:
访问测试:
http://192.168.254.100:19280/lb
用不同设备或 IP 访问,观察同一 IP 的请求是否总是落到同一台服务器。
同一客户端的请求始终分配到同一台后端服务器(适合会话保持)。
4、最少连接(Least Connections)
sudo su - lb# 更改配置
vi /home/lb/nginx/conf/nginx.conf# 启动nginx
nginx
更改配置如下:
访问测试:
http://192.168.254.100:19280/lb
优先将请求分配给当前连接数最少的后端服务器。
四、nginx.conf 配置详解
1、全局块
作用:配置影响 Nginx 全局的指令,通常位于文件顶部。
常见指令:
user nginx; # 运行 Nginx 的用户和组
worker_processes auto; # 工作进程数(通常设为 CPU 核心数)
error_log /var/log/nginx/error.log warn; # 错误日志路径和级别
pid /run/nginx.pid; # 存储主进程 PID 的文件
2、event 模块
作用:配置网络连接相关的参数。
常见指令:
events {worker_connections 1024; # 每个工作进程的最大连接数use epoll; # 事件驱动模型(Linux 推荐 epoll)multi_accept on; # 允许同时接受多个连接
}
3、http 块
作用:定义 HTTP 服务器相关配置,可嵌套多个 server 块。
核心模块:
http {include mime.types; # 引入 MIME 类型定义文件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; # 启用高效文件传输模式keepalive_timeout 65; # 长连接超时时间# 启用 Gzip 压缩gzip on;gzip_types text/css application/javascript;# 引入其他配置文件(如虚拟主机)include /etc/nginx/conf.d/*.conf;# 定义 upstream 负载均衡upstream backend {server 192.168.1.100:8080;server 192.168.1.101:8080;}# 定义 server 块(虚拟主机)server {listen 80;server_name example.com;location / {proxy_pass http://backend;}}
}
4、server 块(虚拟主机配置)
作用:定义单个网站的监听和路由规则,可嵌套多个 location 块。
关键指令:
server {listen 80; # 监听端口server_name example.com; # 域名或 IProot /var/www/html; # 网站根目录# 错误页面error_page 404 /404.html;error_page 500 502 503 504 /50x.html;# 定义路由规则location / {index index.html;}location /api {proxy_pass http://backend;}
}
5.、location 块
作用:匹配特定请求路径并定义处理逻辑。
匹配规则:
-
location /
:匹配所有请求。 -
location = /exact
:精确匹配/exact
。 -
location ~ \.php$
:正则匹配.php
结尾的请求。
常见用法:
location /static {alias /data/static; # 静态文件路径(注意与 root 的区别)expires 30d; # 缓存过期时间
}location ~ \.php$ {fastcgi_pass 127.0.0.1:9000;include fastcgi_params;
}
6、upstream 块
作用:定义负载均衡的后端服务器组。
upstream backend {server 192.168.1.100:8080 weight=3;server 192.168.1.101:8080;least_conn; # 最少连接策略
}
7、mail 模块(邮件代理)
mail {server_name mail.example.com;auth_http localhost:9000/auth;imap_capabilities IMAP4rev1;
}
8、总结
# 全局块
user nginx;
worker_processes auto;# events 块
events {worker_connections 1024;
}# http 块
http {include mime.types;# server 块(虚拟主机)server {listen 80;# location 块(路由规则)location / {root /var/www/html;}}# 其他 server 或 upstream 块...
}
五、nginx 基础知识
1、什么是nginx
Nginx是一个高性能的HTTP和反向代理服务器。
2、Nginx的配置文件结构是怎样的?
主要由三部分组成:
- 全局块:配置影响nginx全局的指令
- events块:配置影响nginx服务器或与用户的网络连接
- http块:可以嵌套多个server块,配置代理、缓存、日志等大多数功能
3、Nginx支持哪些负载均衡算法
-
轮询(默认)
-
加权轮询
-
IP哈希
-
最少连接
-
响应时间(需要第三方模块)
4、如何实现Nginx的热部署?
修改配置后,使用 nginx -t 测试配置
使用 nginx -s reload 重新加载配置而不中断服务
5、什么是Nginx的master-worker模式?
master进程:读取配置、管理工作进程
worker进程:处理实际请求
6、Nginx 性能优化
(1)基础架构优化
-
合理设置 worker 进程数(通常等于 CPU 核数)
-
调整每个 worker 的最大连接数(建议 1 万左右)
-
启用高效的事件模型(如 Linux 的 epoll)
-
优化 TCP/IP 协议栈(开启 sendfile、tcp_nopush 等)
(2)静态资源加速
-
启用长期缓存策略(设置 expires 头)
-
实现智能压缩(gzip 动态内容)
-
使用文件描述符缓存(open_file_cache)
-
关闭静态资源访问日志减少 I/O 压力
(3)动态内容处理
-
精细调整代理超时时间(连接/读取/发送)
-
优化代理缓冲区设置(大小和数量)
-
合理传递必要的 HTTP 头信息
-
启用 keepalive 保持后端连接
(4)多级缓存体系
-
配置高效的反向代理缓存
-
设置合理的缓存过期策略
-
实现缓存锁防止雪崩
-
支持后台缓存更新
(5)系统级调优
-
调整 Linux 内核网络参数
-
增加最大文件描述符限制
-
优化内存分配策略
-
减少不必要的内存拷贝
(6)安全与效率平衡
-
优化 SSL/TLS 配置(会话复用等)
-
实施智能的流量限制
-
关闭非必要模块减少开销
-
保持安全性的同时提升性能
(7)监控与持续优化
-
建立性能基准
-
实施细粒度的日志记录
-
实时监控关键指标
-
渐进式调整验证效果