Nginx 标准化全套笔记(多业务配置+模块编译+Location优先级+动静分离)

发布时间:2026/6/26 9:32:12

Nginx 标准化全套笔记(多业务配置+模块编译+Location优先级+动静分离) 一、Nginx 核心特性模块化设计1. 模块机制原理Nginx 所有功能全部基于模块实现默认仅开启基础核心模块。SSL、状态监控、Rewrite、缓存等高级功能必须在预编译阶段手动添加模块。模块编译后永久生效不支持动态加载想要拓展功能必须重新编译添加对应模块。2. 查看已编译模块bash运行nginx -V3. 实战案例开启 status 状态监控模块预编译添加模块bash运行./configure --prefix/usr/local/nginx --with-http_stub_status_module配置文件开启功能nginxlocation /status { stub_status; }访问测试bash运行curl 127.0.0.1/status二、Nginx 三大虚拟主机多业务隔离一个server{}代表一个独立网站、一套独立业务。 企业多业务隔离三种方式端口区分、域名区分、IP 区分1. 基于端口区分业务原理同一服务器、同一 IP通过不同端口区分不同站点 适用测试环境、后台系统、内部项目nginx# 80 官网业务 server { listen 80; server_name localhost; location / { root html/www; index index.html; } } # 8080 后台业务 server { listen 8080; server_name localhost; location / { root html/admin; index index.html; } }访问测试bash运行curl 192.168.91.146:80 curl 192.168.91.146:80802. 基于域名区分业务生产最常用原理同一 IP、同一端口根据域名区分不同网站 适用企业多官网、博客、商城、多项目nginx# 主站 www.test.com server { listen 80; server_name www.test.com; location / { root html/test; index index.html; } } # 博客 blog.test.com server { listen 80; server_name blog.test.com; location / { root html/blog; index index.html; } }本地 hosts 测试解析plaintext192.168.91.146 www.test.com blog.test.com3. 基于 IP 区分业务原理一台服务器多张 IP不同 IP 对应不同网站nginx# 业务A 绑定 192.168.91.146 server { listen 192.168.91.146:80; server_name localhost; root html/ip1; } # 业务B 绑定 192.168.91.147 server { listen 192.168.91.147:80; server_name localhost; root html/ip2; }临时添加虚拟 IPbash运行ifconfig eth0:0 192.168.91.147 netmask 255.255.255.0 up三、Nginx Location 匹配优先级1. 优先级从高到低精准匹配 () 前缀优先 (^~) 正则匹配 (~/~*) 普通前缀匹配 默认匹配 (/)2. 优先级说明精准完全匹配优先级最高^~前缀优先匹配成功不再执行正则~ / ~*正则匹配区分 / 不区分大小写普通字符串前缀模糊匹配/默认兜底匹配优先级最低3. 优先级实战案例nginx# 精准匹配 location /login {} # 前缀优先 location ^~ /static {} # 正则匹配 location ~* \.(jpg|png|js|css)$ {} # 普通前缀 location /admin {} # 默认匹配 location / {}四、动静分离1. 原理静态资源html、css、js、jpg、png、gifNginx 直接返回动态资源php、jspNginx 转发给 PHP、Tomcat 解析2. 标准动静分离配置nginxserver { listen 80; # 静态资源直接返回 location ~* \.(html|css|js|jpg|png|gif|ico)$ { root /usr/local/nginx/html/static; expires 7d; } # PHP 请求转发 location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } # JSP 请求转发 Tomcat location ~ \.jsp$ { proxy_pass http://127.0.0.1:8080; } }五、前后端语言区分前端语言浏览器解析HTML页面结构CSS页面样式、布局美化JavaScript页面交互、动态效果后端语言服务器运行PHP中小型网站、LNMP 架构Java大型项目、Tomcat 运行 JSP六、Nginx 企业规范禁止直接重启 Nginx会导致业务宕机 标准操作bash运行# 检查配置 nginx -t # 平滑重启 nginx -s reload查看纯净无注释配置bash运行grep -Ev ^$|# /usr/local/nginx/conf/nginx.conf七、网络测试命令区别ping三层 IP 连通测试不测端口服务telnet四层端口开放测试curl七层网站业务访问测试八、高并发优化文件句柄解决报错too many open filesbash运行# 查看 ulimit -n # 临时修改 ulimit -n 65535永久修改/etc/security/limits.confplaintext* soft nofile 65535 * hard nofile 65535九、Nginx Rewrite 跳转1. 四大场景不同域名跳转同域名路径跳转手机 / 电脑设备跳转地域访问跳转2. 语法plaintextrewrite 匹配规则 跳转地址 [标记];3. 四种标记last内部跳转重新匹配 locationbreak内部跳转终止匹配redirect302 临时跳转permanent301 永久跳转4. 手机跳转案例nginxserver { listen 80; server_name localhost; if ( $http_user_agent ~* iphone|android ){ return 301 http://www.yunjisuan.com; } }十、IP 访问控制 allow/deny规则从上至下匹配匹配即停止仅允许指定 IPnginxlocation / { allow 192.168.91.146; deny all; }黑白名单混合nginxlocation / { deny 192.168.91.100; allow 192.168.91.0/24; deny all; }全站维护禁止访问nginxserver { listen 80; deny all; }十一、常用命令汇总bash运行nginx -V # 查看编译模块 nginx -t # 检查配置 nginx -s reload # 平滑重启 ulimit -n # 查看文件句柄 grep -Ev ^$|# /usr/local/nginx/conf/nginx.conf十二、Nginx 开机自启 systemd文件路径/usr/lib/systemd/system/nginx.serviceini[Unit] DescriptionNginx High Performance Web Server Afternetwork.target sshd-keygen.target [Service] Typeforking ExecStart/usr/local/nginx/sbin/nginx ExecReload/bin/kill -HUP $MAINPID KillModeprocess Restarton-failure RestartSec42s [Install] WantedBymulti-user.target生效命令bash运行systemctl daemon-reload systemctl enable nginx systemctl start nginx

相关新闻