nginx使用-(想学nginx,这篇就够了)

nginx使用-(想学nginx,这篇就够了)

upstream wgz{
       
        server 127.0.0.1:8081 ;
        server 127.0.0.1:8082 ;
       fair;
    }

反向代理

动静分离

负载均衡

高可用集群配置

反向代理

  • upstream要转发的地址的配置
  • proxy_pass请求转发的地址


 	location /user{
            proxy_pass http://127.0.0.1:8082;
        }


   location /html{
            proxy_pass http://127.0.0.1:8081;
        }

原先的时候我们需要请求的地址是127.0.0.1:8081才能请求到我们后端的地址,此时请求127.0.0.1:80/user就可以将请求转发到后端的接口中。

2.负载均衡

轮询,权重,后端响应最快

1.情景:

  • 解决服务器性能的瓶颈
  • 其实就是和反向代理是差不多的
  • 区别在于服务器的数量是不一样的

默认的就是轮询,可以设置加权轮询。

2.1轮询

2.案例1

权重是一样的可以平均进行分配

最主要就是proxy_pass的配置和上有集合的配置

 # 上流配置
    upstream wgg {
        server 127.0.0.1:8081 weight=1;
        server 127.0.0.1:8082 weight=1;
    }
    # server可以有多个
    server {
        listen 82;
        server_name localhost;

        location /hello{
            proxy_pass http://wgg;
        }

}

 此时请求的后台接口是一半一半。

2.2权重轮询

设置不同的权重

可以根据权重分配请求的多少

 # 上流配置
    upstream wgg {
        server 127.0.0.1:8081 weight=5;
        server 127.0.0.1:8082 weight=1;
    }
    # server可以有多个
    server {
        listen 82;
        server_name localhost;

        location /hello{
            proxy_pass http://wgg;
        }

}

2.3fair

需要自己下载:https://blog.csdn.net/LeonJinhaiSun/article/details/121153174

根据后台服务器的响应信息进行分配

upstream wgz{
       
        server 127.0.0.1:8081 ;
        server 127.0.0.1:8082 ;
       fair;
    }

3.iphash

解决session共享。

保证session不丢失,推荐使用redis

 # 上流配置
    upstream wgz{
        ip_hash;
        server 127.0.0.1:8081 ;
        server 127.0.0.1:8082 ;
    }
    # server可以有多个
    server {
        listen 81;
        server_name localhost;
 	    location /user{
            proxy_pass http://wgz;
        }
}

4.常用命令

4.1常用命令

https://blog.csdn.net/weixin_52556583/article/details/129226706

nginx -s stop 停止

nginx -s quit 安全退出

start nginx  开启nginx

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

nginx -v 查看版本号

nginx -V 产看版本号比较详细

ps ef |grep nginx 查看nginx的进程的信息

tasklist /fi "imagename eq nginx.exe"  查看运行的nginx

taskkill /f /t /im nginx.exe 关闭打开的nginx

nginx -t 验证配置是不是正确的

4.2配置文件详解

1.并发处理的值,值越大处理并发的能力就越强。

worker_processes 1;

2.events配置和用户连接的部分。1024是最大的连接量。

events {
    worker_connections 1024;
}

3.http块。频繁配置的地方全局快。

配置最频繁的。内部server

  • 日志
  • 超时时间
  • 等等
   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;

    #gzip  on;

4.server块

  • listen 监听端口
  • server_name服务器的地址
  • location 映射的地址
 server {
        listen 82;
        server_name localhost;

        location /hello{
            proxy_pass http://wgg;
        }


        # location / {
        #     root html;
        #     index index.html index.htm;
        # }
        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
        #

        # 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;
        #}
    }

4.3location

主要就是匹配路径的设置

= uri严格匹配

~正则但是区分大小写

~*正则不区分大小写

~~不含正则,找的是匹配最高的

有正则的一定有~*或者~

4.4nginx原理

master和worker

在nginx启动的时候有两个进程,一个master和一个worker

  • master就是领导
  • worker表示进行工作的进程

当client发起请求的时候master获取到前端的请求,worker就抢任务,调用不同的地方处理client的请求。

master有一个,worker多个。

  • 利于热部署,reload
  • 每个worker都是独立的进程,不需要加锁
  • 一个进程退出了不影响其他的进程。提高可用。

多少个worker是最合适的?

  • io多路复用进程。在windows下没有这个玩意。
  • 异步非阻塞

worker和cpu数量是最合适的。

worker的连接数?

  • 每个worker支持1024

5.动静分离

静态资源部署到 nginx可以加快静态资源的解析的速度

不是单纯的将静态文件和动态文件的分离而是请求的分离。

可以设置nginx的缓存。

 server {
        listen 83;
        server_name localhost;
        #  autoindex on;
        location /dff {
            # 静态资源的位置
            root E:/cxl/mytools/other/nginx;

        }
        # 以列表的形式展示文件夹中的内容
        location /mytools {
            root E:/cxl/;
            autoindex on;
        }
    }

6.集群配置

6.1准备工作

1.解决nginx宕机的准备。

  • 一个主:master
  • 一个副:node

2.主从模式

虚拟ip不是存在的但是需要进行绑定,前台进行访问的就是虚拟的ip。

3.centos下nginx的安装

./configure 
make 
make install

4.keepalived安装教程

基础环境准备

yum -y install gcc gcc-c++ autoconf automake make 
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
 
 
yum install keepalived –y

主:192.168.88.129

副:192.168.88.130

虚拟服务器:192.168.88.131

  • 主配置配置文件。修改 /etc/keepalived/keepalivec.conf 配置文件

global_defs { 
	notification_email { 
		acassen@firewall.loc 
		failover@firewall.loc 
		sysadmin@firewall.loc 
	} 
	notification_email_from Alexandre.Cassen@firewall.loc 
	smtp_server 192.168.17.129 
	smtp_connect_timeout 30 
	router_id LVS_DEVEL 
} 
 
vrrp_script chk_http_port { 
	script "/usr/local/src/nginx_check.sh" 
	interval 2  
	weight 2 
} 
 
vrrp_instance VI_1 { 
	state MASTER  
	interface ens33 
	virtual_router_id 51 
	priority 90  
	advert_int 1 
	authentication { 
		auth_type PASS 
		auth_pass 1111 
	} 
	virtual_ipaddress { 
		192.168.88.131 
	} 
}

  • 副结点
global_defs { 
	notification_email { 
		acassen@firewall.loc 
		failover@firewall.loc 
		sysadmin@firewall.loc 
	} 
	notification_email_from Alexandre.Cassen@firewall.loc 
	smtp_server 192.168.17.129 
	smtp_connect_timeout 30 
	router_id LVS_DEVEL 
} 
 
vrrp_script chk_http_port { 
	script "/usr/local/src/nginx_check.sh" 
	interval 2  
	weight 2 
} 
 
vrrp_instance VI_1 { 
	state BACKUP  
	interface ens33 
	virtual_router_id 51 
	priority 70  
	advert_int 1 
	authentication { 
		auth_type PASS 
		auth_pass 1111 
	} 
	virtual_ipaddress { 
		192.168.88.131 
	} 
}

在主和副的** /usr/local/src 添加检测脚本

#!/bin/bash 
A=`ps -C nginx – no-header |wc -l` 
if [ $A -eq 0 ];then 
	/usr/local/nginx/sbin/nginx 
	sleep 2 
	if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then 
		killall keepalived 
	fi 
fi

5.启动主和副

把两台服务器上 Nginx 和 keepalived 启动

  • 启动 Nginx: ./nginx
  • 启动 keepalived: systemctl start keepalived.service
 ./nginx 
systemctl start keepalived.service

查看是不是启动成功了

[root@lxz sbin]#  ps -ef |grep keepalived 
root       1600      1  0 15:53 ?        00:00:00 /usr/sbin/keepalived -D 
root       1601   1600  0 15:53 ?        00:00:00 /usr/sbin/keepalived -D 
root       1602   1600  0 15:53 ?        00:00:00 /usr/sbin/keepalived -D 
root       1690   1493  0 16:10 pts/0    00:00:00 grep --color=auto keepalived

最终测试:虚拟结点。http://192.168.88.131

6.关闭主结点

./nginx -s stop 
 systemctl stop keepalived.service

6.2配置信息介绍

  •  global_defs 全局配置
    • routerid LVSDEVEL。访问到主机
    • vi /etc/hosts可以打开并进行设置

 

  • vrrpscript chkhttp_port:检测脚本配置。检测的间隔是2秒。weight,设置当前服务器的权重。
  • vrrpinstance VI1 {
     state BACKUP //设置主还是副服务器
     interface ens33 //当前网卡的名称
     virtualrouterid 51 //标注主和备虚拟主机的id。要求主和备是一致的
     priority 70 //优先级,主的优先级大
     advertint 1
     authentication {
     authtype PASS
     authpass 1111
     }
     virtualipaddress {
     192.168.88.131 //虚拟ip的配置
     }
    }
global_defs { 
	notification_email { 
		acassen@firewall.loc 
		failover@firewall.loc 
		sysadmin@firewall.loc 
	} 
	notification_email_from Alexandre.Cassen@firewall.loc 
	smtp_server 192.168.17.129 
	smtp_connect_timeout 30 
	router_id LVS_DEVEL 
} 
 
vrrp_script chk_http_port { 
	script "/usr/local/src/nginx_check.sh" 
	interval 2  
	weight 2 
} 
 
vrrp_instance VI_1 { 
	state BACKUP  //设置主还是副服务器 
	interface ens33 
	virtual_router_id 51 
	priority 70  
	advert_int 1 
	authentication { 
		auth_type PASS 
		auth_pass 1111 
	} 
	virtual_ipaddress { 
		192.168.88.131 
	} 
}

脚本介绍

要是主服务器挂掉了就关闭备服务器的所有的内容。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

简单点了

谢谢大佬

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值