1 ## Nginx
2
3 - 反向代理
4 > 反向代理方式指服务器接收internet的链接请求 然后根据请求转化给内部网络服务器上
5 - 负载均衡
6 1.每个请求按时间顺序逐一分配到后端服务器,
7 ``` 简单配置
8 upstream test {
9 server localhost:8080;
10 server localhost:8081;
11
12 }
13
14 server {
15 listen 80;
16 server_name:localhost;
17
18 location / {
19 proxy_pass http://test;
20
21 }
22 }
23
24 ```
25
26 2. 权重 ;指定轮询机率,weight 和访问比率成正向比,用于后端服务器鑫能不均衡情况
27
28 ```
29 upstream test {
30 server localhost:8080 weight=9;
31 server localhost:8081 weight=1;
32
33 }
34 //10次访问1次8081 9次8080
35 ```
36
37 3. ip_hash 程序请求不是无状态时候(采用session保存数据)我们需要一个客户访问一个服务器,ip_hash的每个请求按访问ip的 hash结果分配这样每个访客固定一个后端服务器
38
39 ```
40 upstream test {
41 ip_hash;
42 server localhost:8080 weight=9;
43 server localhost:8081 weight=1;
44
45 }
46 ```
47
48 4. fair (第三方) 按后端响应时间来分配请求 响应时间短的优先分配
49 5. url_hash (第三方) 按访问url的hash结果来分配请求使每个url定向到同一个后端服务器 在upstream中加入hash语句 server 语句中不能写入weight 等其他参数 hash_method是使用hash的算法
50
51 ```
52 upstream test {
53 hash: $request_uri;
54 hash_method crc32;
55 server localhost:8080 ;
56 server localhost:8081 ;
57
58 }
59 ```
60
61 - HTPP服务器 (动静分离)
62 Nginx本身也是一个资源服务器,只有在静态资源的时候可以使用Nginx来做代理 同时实现动静分离
63
64 ```
65 server {
66 listen 80;
67 server_name:localhost;
68
69 location / {
70 root e:\html;
71 index index.html;
72 }
73 }
74 ```
75 > 访问时会默认访问E盘html目录下index.html 页面
76
77
78 ```
79 server {
80 listen 80;
81 server_name:localhost;
82
83 location / {
84 root e:\html;
85 index index.html;
86 }
87
88 localhost ~ \.(gif|jpg|jpge)$ {
89 root e:\static;
90 }
91 }
92 ```
93 > 所有静态文件都有nginx 处理,存放目录为html
94
95 - 正向代理