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

linux学习笔记整理: 关于linux:nginx服务器 2024/7/20;

nginx服务器:

自理解:

nginx是一种分发式服务器,统一进入的接口,并将进入的用户进行指定性分发给不同服务器地址交互;

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

京东,淘宝,12306,新浪等都在使用nginx相关服务器

安装nginx:

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

  1. 安装nginx的依赖插件

    前提已安装阿里云插件

    yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
  2. 下载nginx
    nginx: download
  3. 创建一个目录作为nginx的安装路径
    mkdir /usr/nginx
  4. 解压
    tar -zxvf nginx-1.26.1.tar.gz
  5. 进入解压后的目录
    cd nginx-1.26.1
  6. 指定nginx的安装路径
    ./configure --prefix=/usr/nginx
  7. 编译和安装nginx
    make install

启用nginx:

nginx目录结构

conf 配置目录

html 静态资源

logs 日志目录 (用于排错)

sbin 脚本目录(启动和关闭nginx)

启动nginx
  • ./nginx 启动

  • ./nginx -s stop 关闭

  • ./nginx -s reload 重新加载配置文件

访问nginx 80

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

若无法连接注意端口放行

配置nginx:

.../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;
​}
}
server {#访问的端口号listen 82;server_name localhost;location /{# 代理的服务器地址proxy_pass   http://192.168.111.132:8080;}
}
​
#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;
#    }
#}
​
}
负载均衡(轮询制):

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

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

启用springBoot项目/TomCat部署:

将springBoot项目打包放入Linux系统中;端口号放行

使用以下代码启动springBoot打包好的文件;

java -jar 文件名.jar
配置nginx:

.../nginx/conf/nginx.conf

upstream gzx{server 172.16.7.17:8080;server 192.168.85.126:8080;}server {listen 82;server_name localhost;location /{proxy_pass   http://gzx;
​}}
重新加载nginx配置文件:

.../nginx/sbin/nginx -s reload

负载均衡(权重制):

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

upstream gzx{#通过使用weight=3设置比重,访问3次172才会访问1次192;server 172.16.7.17:8080 weight=3;server 192.168.85.126:8080 weight=1;
}
server {listen 82;server_name localhost;location /{proxy_pass   http://gzx;
​}
}
负载均衡(IP制):
根据访问者客户的ip固定访问对应的web服务器。
upstream gzx{server 172.16.7.17:8080;server 192.168.85.126:8080;#通过访问者IP固定其访问接口;ip_hash;
}
server {listen 82;server_name localhost;location /{proxy_pass   http://gzx;
​}
}
动静分离:

动:动态资源[接口] 静:静态资源 [css js image]。

分离:把静态资源放入nginx服务器下。动态资源web服务器下。

作用: 不需要将不会改变的东西重复的从服务器调用至nginx在发送给客户;而是直接从nginx中发送给客户;

操作:

首先创建对应目录并将静态资源放入nginx中,/nignx/static/js等目录下

然后打开nginx配置文件

server{listen 81;server_name localhost;location /{proxy_pass http://使用静态资源的IP:端口;}location ~ \.js|.css|.jpg|....|.png$ {root static;}
}
HA高可用搭建:
高可用的原理keepalived:

通过部署多个nginx服务器防止主服务器崩溃无法访问;设置备用服务器

| 服务器:132 |

| nginx服务器:88 |-> |----------------|

客户->虚拟IP->:|---------------------| | 服务器:133 |...

| nginx服务器:89 |...

搭建方式:

安装keepalived

yum install -y keepalived

默认安装至/etc/keepalived下

修改keepalived.conf配置文件

主机:

global_defs {notification_email {acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.loc# ip的地址smtp_ server 192.168.111.188smtp_connect_timeout 30router_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 1authentication {auth type PASSauth pass 1111}virtual_ipaddress { 192.168.111.50 # 虚拟ip. 使用逗号隔开}track_script {chk_http_port # 追踪nginx脚本}
​
}

备用机:

global_defs {notification_email {acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.loc# ip的地址smtp_ server 192.168.111.189smtp_connect_timeout 30router_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 1authentication {auth type PASSauth pass 1111}virtual_ipaddress { 192.168.111.50 # 虚拟ip. 使用逗号隔开}track_script {chk_http_port # 追踪nginx脚本}
​
}

(上方配置文件内地址)放于/usr/local/src/nginx_check.sh

#!/bin/bash
# 检查是否开启nginx---统计nginx进程的个数
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];thenpkill -9 keepalived
fi

解释文件:

A=`ps -C nginx --no-header |wc -l`        
if [ $A -eq 0 ];then    #如果nginx没有启动就启动nginx                        /app/nginx/sbin/nginx                #重启nginxif [ `ps -C nginx --no-header |wc -l` -eq 0 ];then    #nginx重启失败,则停掉keepalived服务,进行VIP转移pkill keepalived                    fi
fi

修改权限: chmod 777 nginx_check.sh

启动:

.../nginx/sbin/nginx

keepalived systemctl start|stop keepalived

访问时直接访问虚拟地址即可

yum卸载

yum remove keepalived

删除相关文件
  1. find / -name keepalived

  2. /etc/selinux/targeted/tmp/modules/100/keepalived

  3. /etc/selinux/targeted/active/modules/100/keepalived

  4. rm -rf /etc/selinux/targeted/tmp/modules/100/keepalived

  5. rm -rf /etc/selinux/targeted/active/modules/100/keepalived

卸载keeplived工作路径
  1. cd /home/hd/keepalived-1.2.18

  2. make uninstall

删除相关文件

查看相关文件: find / -name keepalived

  1. 删除相关文件

  2. rm -rf /run/lock/subsys/keepalived rm -rf /etc/keepalived rm -rf /etc/sysconfig/keepalived rm -rf /etc/rc.d/init.d/keepalived rm -rf /usr/sbin/keepalived rm -rf /usr/local/keepalived rm -rf /home/hd/keepalived-1.2.18/keepalived rm -rf /home/hd/keepalived-1.2.18/keepalived/etc/keepalived rm -rf /home/hd/keepalived-1.2.18/bin/keepalived

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Ubuntu Grub引导优化
  • 基于微信小程序+SpringBoot+Vue的校园自助打印系统(带1w+文档)
  • Flowable-SpringBoot项目集成
  • idea2019版本创建JavaWeb项目并配置Tomcat步骤
  • apollo9.0park and go-adjust
  • python-爬虫实例(5):将进酒,杯莫停!
  • Covalent(CXT)运营商网络规模扩大 42%,以满足激增的需求
  • Java练习题 (2024.7.23)
  • C++面试题之判断一个变量是不是指针
  • JavaWeb(4)JavaScript入门2—— JS的对象和JSON
  • 【附源码】Python :校园导航与最短路径算法
  • 【数学建模】——前沿图与网络模型:新时代算法解析与应用
  • gitee的怎么上传项目
  • PowerShell 使用介绍
  • 【C++中的IO流和文件操作精讲】
  • Babel配置的不完全指南
  • flask接收请求并推入栈
  • Flex布局到底解决了什么问题
  • JavaScript 是如何工作的:WebRTC 和对等网络的机制!
  • JavaScript设计模式之工厂模式
  • JavaSE小实践1:Java爬取斗图网站的所有表情包
  • Java教程_软件开发基础
  • JSONP原理
  • js写一个简单的选项卡
  • Redis 中的布隆过滤器
  • Spring Cloud Feign的两种使用姿势
  • Spring-boot 启动时碰到的错误
  • Theano - 导数
  • Vue--数据传输
  • 半理解系列--Promise的进化史
  • 当SetTimeout遇到了字符串
  • 对超线程几个不同角度的解释
  • 浅谈web中前端模板引擎的使用
  • 一份游戏开发学习路线
  • 移动端 h5开发相关内容总结(三)
  • ​MySQL主从复制一致性检测
  • ​人工智能书单(数学基础篇)
  • # 学号 2017-2018-20172309 《程序设计与数据结构》实验三报告
  • #AngularJS#$sce.trustAsResourceUrl
  • (10)工业界推荐系统-小红书推荐场景及内部实践【排序模型的特征】
  • (day6) 319. 灯泡开关
  • (pycharm)安装python库函数Matplotlib步骤
  • (web自动化测试+python)1
  • (多级缓存)多级缓存
  • (二)fiber的基本认识
  • (附表设计)不是我吹!超级全面的权限系统设计方案面世了
  • (附源码)ssm基于jsp高校选课系统 毕业设计 291627
  • (六)库存超卖案例实战——使用mysql分布式锁解决“超卖”问题
  • (收藏)Git和Repo扫盲——如何取得Android源代码
  • (四) 虚拟摄像头vivi体验
  • (新)网络工程师考点串讲与真题详解
  • (转) Android中ViewStub组件使用
  • (转)http-server应用
  • (转)setTimeout 和 setInterval 的区别
  • .Net 4.0并行库实用性演练