nginx之反向代理、负载均衡、http服务器、动静分离的配置
2018-02-24 14:34
525 查看
反向代理
server { listen 80; server_name localhost; client_max_body_size 1024M; location / { proxy_pass http://localhost:8080; proxy_set_header Host $host:$server_port; } }localhost的时候,就相当于访问localhost:8080了
HTTP服务器
server { listen 80; server_name localhost; client_max_body_size 1024M; location / { root e:wwwroot; index index.html; } }这样如果访问http://localhost 就会默认访问到E盘wwwroot目录下面的index.html,如果一个网站只是静态页面的话,那么就可以通过这种方式来实现部署。
动静分离
server { listen 80; server_name localhost; location / { root e:wwwroot; index index.html; 4000 } # 所有静态请求都由nginx处理,存放目录为html location ~ .(gif|jpg|jpeg|png|bmp|swf|css|js)$ { root e:wwwroot; } # 所有动态请求都转发给tomcat处理 location ~ .(jsp|do)$ { proxy_pass http://test; } error_page 500 502 503 504 /50x.html; location = /50x.html { root e:wwwroot; } }当访问gif,jpeg时 直接访问
e:wwwroot;,正则自行配置
负载均衡
RR(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除upstream test { server localhost:8080; server localhost:8081; } server { listen 81; server_name localhost; client_max_body_size 1024M; location / { proxy_pass http://test; proxy_set_header Host $host:$server_port; } }当访问calohost:81时,如果localhost:8081 挂了,也不会影响正常访问
权重
upstream test { server localhost:8080 weight=9; server localhost:8081 weight=1; }10次一般只会有1次会访问到8081,而有9次会访问到8080
ip_hash
PR 和权重 在有session进行访问时,由于随机访问,导致
session无效,需要使用
ip_hash
upstream test { ip_hash; server localhost:8080; server localhost:8081; }
fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。upstream backend { fair; server localhost:8080; server localhost:8081; }
url_hash(第三方)
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。 在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法。upstream backend { hash $request_uri; hash_method crc32; server localhost:8080; server localhost:8081; }
指令-热启动
Nginx重新读取配置的命令nginx -s reload
相关文章推荐
- nginx反向代理之缓存,负载均衡,动静分离的配置详解
- nginx配置文件(反向代理+集群+动静分离)
- Nginx 反向代理,负载均衡,动静分离配置
- windows下配置Nginx+memcached+tomcat实现负载均衡以及动静资源分离
- nginx配置反向代理及动静分离
- nginx反向代理之缓存,负载均衡,动静分离的配置详解
- Nginx之反向代理与负载均衡实现动静分离实战
- nginx高级部分:动静分离、负载均衡配置
- 分布式数据库的实现,以及负载均衡和反向代理,动静分离的配置
- nginx的配置及以反向代理方式实现动静分离、负载均衡
- nginx+tomcat 实现动静分离、负载均衡
- Nginx服务器安装及详细配置(动静分离)
- Nginx 负载均衡和反向代理配置和优化
- nginx入门配置,及动静分离
- Nginx HTTP负载均衡和反向代理配置
- nginx的配置、虚拟主机、负载均衡和反向代理(1)
- Nginx动静分离经典案例配置
- Centos7安装Nginx及配置反向代理和负载均衡
- Nginx安装、以及反向代理、负载均衡、资源动静分离简介
- Nginx配置之反向代理与负载均衡