当前位置: 首页 > news >正文

Nginx(详解以及如何使用)

目录

1. 什么是Nginx?

2. 为什么使用nginx?

3. 安装nginx

3.1?安装nginx的依赖插件

3.2 下载nginx

?3.3?创建一个目录作为nginx的安装路径

?3.4?解压

?3.5?进入解压后的目录

3.6?指定nginx的安装路径

?3.7?编译和安装nginx

3.8 启动nginx

?3.9?访问nginx

?4.?nginx目录结构

?5.nginx配置文件

6. nginx的核心功能

6.1 nginx反向代理功能

6.2 nginx的负载均衡

6.3 nginx动静分离

7. nginx的HA高可用的搭建

7.1 高可用的原理–keepalived

7.2 搭建ha高可用


1. 什么是Nginx?

Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好。

并发能力: 50,000 。

2. 为什么使用nginx

  • Nginx 是高性能的 HTTP 和反向代理的web服务器,处理高并发能力是十分强大的,能经受高负 载的考验,有报告表明能支持高达 50,000 个并发连接数。
  • Nginx支持热部署,启动简单,可以做到7*24不间断运行。几个月都不需要重新启动。

3. 安装nginx

nginx可以独立安装在一台服务器–也可以和项目在同一个服务器。

3.1安装nginx的依赖插件

yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

如果yum这个命令不能用,请看这篇:Linux查看端口号命令以及yum源无法使用的解决方法-CSDN博客

3.2 下载nginx

nginx: download

3.3创建一个目录作为nginx的安装路径

mkdir /usr/nginx

3.4解压

tar -zxvf nginx-1.26.1.tar.gz

3.5进入解压后的目录

cd nginx-1.26.1

3.6指定nginx的安装路径

./configure --prefix=/usr/nginx

3.7编译和安装nginx

make install

3.8 启动nginx

在sbin目录下操作或者配置环境使在任何地方都可以使用:

nginx 启动 nginx -s stop 关闭 nginx -s reload 重新加载配置文件

3.9访问nginx

http://nginx所在的ip:nginx的端口/

默认端口号:80

4.nginx目录结构

5.nginx配置文件

在/usr/nginx/conf/nginx.conf中。

#user nobody; #工作的线程数 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; #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 logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; server { listen 81; server_name localhost; location /{ root static; index main.html; } } #gzip on; server { listen 80; # 监听的端口号 server_name localhost; # 监听的主机名.域名 #charset koi8-r; #access_log logs/host.access.log main; # 资源/ location / { root html; #根目录 index index.html main.html; # 资源 } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ .php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ .php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }

6. nginx的核心功能

6.1 nginx反向代理功能

正向代理

代理的为客户端,对于服务器不知道真实客户的信息。例如:翻墙软件。

反向代理服务器

代理的为服务器端。对于客户来说不知道服务器的信息。例如: nginx。

项目部署例:

准备web项目

准备nginx

启动web项目:

配置nginx:

server { listen 82; server_name localhost; location /{ # 代理的服务器地址 proxy_pass http://192.168.111.132:8080; } }

启动ngin:

./usr/nginx/sbin/nginx

6.2 nginx的负载均衡

负载均衡(Load Balance [4]):其意思就是把请求分摊到多个操作单元上进行执行,例如Web服务器、FTP服务器、企业关键应用服务器和其它关键任务服务器等,从而共同完成工作任务。

web项目必须搭建的为集群模式。

web服务器项目至少搭建2台以上。

运行两个web工程项目:

springboot项目:

运行springboot项目:java -jar xxx.jar

注意: 端口号别忘记放行。

配置nginx完成负载均衡:

重新加载nginx配置

/usr/nginx/sbin/nginx -s reload

测试

负载均衡的策略

默认为轮询。

权重策略: 服务器硬件配置不同时。

ip_hash策略: 根据访问者客户的ip固定访问对应的web服务器。

花钱买第三方策略插件。

6.3 nginx动静分离

动:动态资源(接口)

静:静态资源 (css js image)。

分离: 之前我们把静态资源和动态资源全部放在web服务器下。 把静态资源放入nginx服务器下。动态资源web服务器下。

7. nginx的HA高可用的搭建

7.1 高可用的原理–keepalived

7.2 搭建ha高可用

俩台机器:

安装keepalived:

yum install -y keepalived

默认安装在/etc/keepalived下

修改keepalived.conf配置文件:

global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc # ip的地址 smtp_ server 192.168.111.188 smtp_connect_timeout 30 router_id 192.168.111.188 } # 执行脚本 vrrp_script chk_http_port { script "/usr/local/src/nginx_check.sh" interval 2 # 每2s执行一次该脚本 weight -20 # keepalive宕机 权重-20 优先级 } vrrp_instance VI_1 { state MASTER # 角色 interface ens33 # 网卡名 virtual_router_id 51 # id 保证主从相同 priority 100 # 优先级 主节点大于从节点 advert_int 1 authentication { auth type PASS auth pass 1111 } virtual_ipaddress { 192.168.111.50 # 虚拟ip. 使用逗号隔开 } track_script { chk_http_port # 追踪nginx脚本 } }

从节点

global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc # ip的地址 smtp_ server 192.168.111.189 smtp_connect_timeout 30 router_id 192.168.111.189 } # 执行脚本 vrrp_script chk_http_port { script "/usr/local/src/nginx_check.sh" interval 2 # 每2s执行一次该脚本 weight -20 # keepalive宕机 权重-20 优先级 } vrrp_instance VI_1 { state BACKUP # 角色 interface ens33 # 网卡名 virtual_router_id 51 # id 保证主从相同 priority 90 # 优先级 主节点大于从节点 advert_int 1 authentication { auth type PASS auth pass 1111 } virtual_ipaddress { 192.168.111.50 # 虚拟ip. 使用逗号隔开 } track_script { chk_http_port # 追踪nginx脚本 } }

nginx_check.sh

#!/bin/bash # 检查是否开启nginx---统计nginx进程的个数 A=`ps -C nginx --no-header |wc -l` if [ $A -eq 0 ];then pkill -9 keepalived fi A=`ps -C nginx --no-header |wc -l` if [ $A -eq 0 ];then #如果nginx没有启动就启动nginx /app/nginx/sbin/nginx #重启nginx if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then #nginx重启失败,则停掉keepalived服务,进行VIP转移 pkill keepalived fi fi

修改权限:chmod 777 nginx_check.sh

启动:

nginx
keepalived systemctl start|stop keepalived

http://www.cnnetsun.cn/news/1440663.html

相关文章:

  • 2026年一文讲透|全领域适配的AI论文神器 —— 千笔ai写作
  • 交稿前一晚!8个降AIGC软件全场景通用测评与推荐
  • 开源大模型nlp_structbert_sentence-similarity_chinese-large:中文语义匹配保姆级教程
  • SenseVoice-small轻量优势:模型加载时间<3秒,冷启动响应极快
  • 基于机器学习的工业软测量技术及应用
  • 基于springboot拼车管理系统设计与开发(源码+精品论文+答辩PPT等资料)
  • ndnSIM开发环境优化(二)——VScode跨文件Intellisense配置实战
  • poi-tl表格插件深度优化:如何用子循环功能生成动态报表?
  • C++编程中const成员函数与const对象的深入探讨
  • 三层网络搭建(思科模拟器)
  • Ceph集群中安全删除OSD磁盘指南:Rocky Linux 9.6 + Ceph 17.2.9容器化环境实践
  • AdaMem:清华/微信/中科大提出 Agent 记忆系统新 SOTA
  • 力扣hot100第82题:杨辉三角
  • C++4(类与对象下篇)
  • 实战演练:如何绕过文件上传限制获取ACTF2020新生赛Flag(附.phtml木马制作教程)
  • Qwen2.5-7B微调指南:10分钟LoRA训练,让AI模型“认主”
  • Firefly RK3399刷Ubuntu18.04避坑指南:从驱动安装到系统升级全流程
  • 告别复杂配置!ANIMATEDIFF PRO保姆级部署教程,RTX 4090开箱即用
  • Windows 下安装codex
  • 基于多因子流动性模型的“黄金闪崩”解析:利率预期强化与资金再平衡驱动的金价8%下跌机制
  • 包装适配器的使用
  • 从这8道Swift题逆袭大厂:2025最新类型系统考点精讲(含泛型实战)
  • RP2040硬件PWM驱动库:纳秒级精度与多通道确定性控制
  • AI写论文?先别急着下结论——一份靠谱的AIGC检测报告该长什么样?
  • Claude Code 封号潮下,如何用 RTK 让你的 Token 省 80%?
  • 浏览器自动化与AI结合:基于谷歌浏览器插件调用Nanbeige 4.1-3B
  • 手把手教你修改miniwiggler配置信息,解决UDE连接问题(附FT_PROG工具下载)
  • 手把手教你用fscan+MSF搞定CTFshow内网靶场(附PHAR攻击技巧)
  • ESP32 Arduino平台MQTT v5.0客户端封装库详解
  • Houdini VEX实战:5个新手必学的几何体操作技巧(附代码示例)