Nginx 学习

Nginx 学习,第1张

Nginx 学习(三).Nginx 实践

本章节 主机Windows

虚拟机192.168.141.131

1. 多虚拟站点 1.1 前期准备工作

Windows主机:hosts 文件修改,模拟域名解析;

## 学习nginx 
192.168.141.131 www.nginx.com
192.168.141.131 aaa.nginx.com
192.168.141.131 bbb.nginx.com
192.168.141.131 ccc.nginx.com
192.168.141.131 ddd.nginx.com
192.168.141.131 eee.nginx.com

Linux 虚拟机 : 创建目录 /website

[root@localhost website]# pwd
/website  ## 创建目录
[root@localhost website]# ls
aaa  bbb  ccc  ## 对应文件夹
[root@localhost website]# cd aaa
[root@localhost aaa]# ls
index.html ###每个文件夹都创建 index.html 文件
[root@localhost aaa]# cat index.html   
<h1>AAA website<h1>   ###内容 不一样 AAA /BBB /CCC

2. 多端口测试

一个server 表示一个虚拟主机

配置文件

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   /website/aaa;
            index  index.html index.htm;
        }
 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
	
	server {
        listen       81;
        server_name  localhost;

        location / {
            root   /website/bbb;
            index  index.html index.htm;
        }
 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
	
	server {
        listen       82;
        server_name  localhost;

        location / {
            root   /website/ccc;
            index  index.html index.htm;
        }
 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

效果

3. 多域名测试

配置文件

 server {
        listen       80;
        server_name  aaa.nginx.com;

        location / {
            root   /website/aaa;
            index  index.html index.htm;
        }
 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
	
	server {
        listen       80;
        server_name  bbb.nginx.com;

        location / {
            root   /website/bbb;
            index  index.html index.htm;
        }
 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
	server {
        listen       80;
        server_name  ccc.nginx.com;

        location / {
            root   /website/ccc;
            index  index.html index.htm;
        }
 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }	
	 

效果

4. 虚拟主机 server

一个server 表示一个虚拟主机:解析配置文件

  • 多个server ,安装顺序匹配,匹配到第一个 后续就不匹配了
  • listen + server_name 虚拟主机唯一性
  • server_name: bbb.nginx.com ddd.nginx.com eee.nginx.com; 可以配置多个域名
  • server_name: 域名可以是通配符 *.nginx.*;
  • server_name: 域名可以是正则表达式

	server {
        listen       80;
        
        server_name  ccc.nginx.com;

        location / {
            ## ## rewrite     
            root   /website/ccc;
            index  index.html index.htm;
        }
 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

4.1 location

两种方式 二选一

/ 表示匹配所有

方式一

location / {	
            root   /website/ccc;
            index  index.html index.htm;
 }

方式二

location / {	
	## 注意 https 需要额外的配置
    proxy_pass  http://www.baidu.com
 }
  • proxy_pass 后面可以直接是地址 proxy_pass http://www.baidu.com
  • 也可以是一个自定义的 服务 示例
	upstream myserver {
		server localhost:81;
		server localhost:82;
		server localhost:83;
	}
	
    server {
        listen       80;
        server_name  www.nginx.com;

        location / {
            proxy_pass http://myserver;
        }
    }
5. 功能(一):反向代理, 网关

nginx 服务转发的功能其实就是反向代理;

反向代理:反向代理服务器位于用户与目标服务器之间,但是对于用户而言,反向代理服务器就相当于目标服务器,即用户直接访问反向代理服务器就可以获得目标服务器的资源;

区别于反向代理,什么是正向代理:正向代理伪造client,反向代理伪造server,正向代理隐藏客户端,反向代理隐藏服务端;

网关: 所有数据经过nginx服务器

6. 功能(二):负载均衡

后端业务服务器是一个集群,nginx g根据负载均衡算法来控制服务区访问哪一个

6.1 复制均衡算法
## server 配置
    server {
        listen       80;
        server_name  www.nginx.com;

        location / {
            proxy_pass http://myserver;
        }
    }

1.轮训 默认

	upstream myserver {
		server localhost:81 ;
		server localhost:82 ;
		server localhost:83 ;
	}
6.2 权重
	upstream myserver {
		server localhost:81  weight=10;
		server localhost:82  weight=5;
		server localhost:83  weight=1;
		server localhost:84  weight=20 down; ### 表示不参与负载均衡
		server localhost:85  weight=20 backup; ### 备用 当都不可用时启用  
	}

3.ip_hash

根据ip 地址Hash分配节点

upstream myserver {
     ## ip_hash;    least_conn   fari
	server localhost:81 ;
	server localhost:82 ;
	server localhost:83 ;
}

4.least_conn

最少连接访问

5.fari

根据服务响应时间

7. 功能(三):动静分离

一个项目不仅仅有动态资源,还有静态资源;静态资源可以放在nginx下面;

location ~*/(js|img|css) {	
            root   html; #### js|img|css 静态数据放到该文件夹下面
            index  index.html index.htm;
 }
8. 功能(四):URL ReWrite
  • 隐藏真正的URL
  • 模拟静态页面,搜索引擎友好
location = /{
   ## rewrite     
   proxy_pass http://myserver;
}

flag :

  • last 本条匹配完成后,不继续 location URI 匹配
  • break 不在任何的匹配
  • redirect 302 临时重定向 (爬虫看,可以继续爬数据)
  • permanent 301 永久重定向
8. 功能(五):防盗链

本站点的数据 禁止浏览器以外的访问:校验http header 里面的 Referer

location = /{
   valid_refers  none|block www.nginx.com;
   if($valid_refer){ //对 Referer错误  请求直接 403
   		return 403;
   }
   proxy_pass http://myserver;
}
  • none : 没有refer 可以访问

  • block:浏览器地址栏带https和 http

9. 功能(六):配置错误页面
### 这里配置的是50* 的异常  也可以转发其他的异常
error_page   500 502 503 504  /50x.html;
location = /50x.html {
     root   html;
}
10.高可用配置

keepalived + Nginx 配置HA 高可用

现在的生产环境中一般是多个节点来共同工作,当一个节点失效,需要可以快速节点的切换;

10.1 简介

nginx 自身是做不到高可用的,需要借助一个外部软件 keepalived 来保证;

keepalived 软件的基本原理就是多个机器中的 keepalived + 虚拟的IP地址(对外提供服务的IP) 保持心跳检测,当心跳检测不到就开始投票,存活的节点获取修改自己IP为虚拟IP;

缺点:可能是由于两台服务器之前是不连通的,两台服务器都可以向外部提供服务;

解决方案就是:添加脚本,脚本对我们后台服务检测(如MySQL ,Tomcat ),如果服务不可用,就立刻杀死 keepalived 进程;存活的keepalived 的节点可以迅速的切换;

10.2 架构图

10.3 安装准备

修改hosts

## 指向虚拟地址
192.168.141.150 www.nginx.com  

两台虚拟机:

  • 192.168.141.131 主节点

  • 192.168.141.132 备用节点

yum 安装 keepalived

## 其中有一台机器安装了mysql  yum install -y keepalived 执行报错   
yum install -y keepalived 

## 如果报错 添加 --skip-broken
yum install -y keepalived  --skip-broken

两台机器nginx 配置

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on; 
    keepalive_timeout  65;

	upstream myserver {
		server 192.168.141.131:81;
		server 192.168.141.132:81;   
	}
	
    server {
        listen       80;
        server_name  www.nginx.com;

        location / {
            proxy_pass http://myserver;
        }
    }
	
	server {
        listen       81;
        server_name  localhost;

        location / {
            root   /website/aaa;  
            index  index.html index.htm;  ### 131内容 131 : AAA website   132 内容 132: AAA website
        }
    }	
}

主节点keepalived 配置

! Configuration File for keepalived

global_defs {
   router_id ng131
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.141.150
    }
}

备用节点keepalived 配置

! Configuration File for keepalived

global_defs {
   router_id ng132
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.141.150
    }
}

测试成功;master 节点关闭后,BACKUP 节点切换IP 为 150 继续提供服务;

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/872494.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存