nginx基本配置
1 worker_processes 2; 2 events { 3 worker_connections 1024; 4 } 5 http { 6 include mime.types; 7 default_type application/octet-stream; 8 #upstream配置被代理的服务器,localServer为自定义的服务器别名 9 upstream localServer{ 10 server 127.0.0.1:9002; 11 } 12 server { 13 listen 80; 14 server_name localhost; 15 <!-- 以/api/开头的url都被此location拦截--> 16 location ^~ /api/ { 17 proxy_set_header Host $http_host; 18 proxy_set_header X-Real-Ip $remote_addr; 19 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 20 proxy_set_header X-Forworded-For $http_x_forwarded_for; 21 proxy_pass http://localServer; 22 #root html; 23 #index index.html index.htm; 24 } 25 <!-- 默认访问到html目录下的静态资源--> 26 location / { 27 root html; 28 index index.html index.htm; 29 } 30 error_page 500 502 503 504 /50x.html; 31 location = /50x.html { 32 root html; 33 } 34 } 35 }
如果不采用默认的轮询机制,nginx还提供了ip_hash策略和权重策略,配置风格如:
ip_hash负载均衡策略配置: upstream lagouServer{ ip_hash; server 127.0.0.1:8080; server 127.0.0.1:8082; } 权重策略配置: upstream lagouServer{ server 127.0.0.1:8080 weight=1; server 127.0.0.1:8082 weight=2; }
探索不以困难而止,学习不以艰苦而终