Nginx–负载配置实例
负载配置实例
[root@lb01 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream backend {
server 172.16.1.7:80 weight=1;
server 172.16.1.8:80 weight=1;
}
server {
listen 80;
server_name blog.etiantian.org;
location / {
index index.html index.htm;
proxy_pass http://backend;
include proxy.conf;
}
}
}
[root@lb01 conf]# cat proxy.conf
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_connect_timeout 60;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
动静分离配置
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream static_pools {
server 10.0.0.9:80 weight=1;
}
upstream upload_pools {
server 10.0.0.10:80 weight=1;
}
upstream default_pools {
server 10.0.0.11:80 weight=1;
}
server {
listen 80;
server_name www.etiantian.org;
location / {
proxy_pass http://default_pools;
include proxy.conf;
}
location /static/ {
proxy_pass http://static_pools;
include proxy.conf;
}
location /upload/ {
proxy_pass http://upload_pools;
include proxy.conf;
}
}
}
基于URI的调度 LVS是不支持的
配置信息参考
[root@lb01 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream upload_pools {
server 10.0.0.8:80 weight=1;
}
upstream default_pools {
server 10.0.0.8:8080 weight=1;
}
upstream static_pools {
server 10.0.0.7:80 weight=1;
}
server {
listen 80;
server_name www.etiantian.org;
location / {
proxy_pass http://default_pools;
include proxy.conf;
}
location /static/ {
proxy_pass http://static_pools;
include proxy.conf;
}
location /upload/ {
proxy_pass http://upload_pools;
include proxy.conf;
}
}
}
方案2:以if语句实现
if ($request_uri ~* "^/static/(.*)$")
{
proxy_pass http://static_pools/$1;
}
if ($request_uri ~* "^/upload/(.*)$")
{
proxy_pass http://upload_pools/$1;
}
location / {
proxy_pass http://default_pools;
include proxy.conf;
}
基于user-agent配置文件
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#upload server
upstream android_pools {
server 10.0.0.8:80 weight=1;
}
upstream pc_pools {
server 10.0.0.8:8080 weight=1;
}
#static
upstream iphone_pools {
server 10.0.0.7:80 weight=1;
}
server {
listen 80;
server_name www.etiantian.org;
location / {
if ($http_user_agent ~* "android")
{
proxy_pass http://android_pools;
}
if ($http_user_agent ~* "iphone")
{
proxy_pass http://iphone_pools;
}
proxy_pass http://pc_pools;
include proxy.conf;
}
}
}
实践配置
[root@lb01 conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream static_pools {
server 10.0.0.9:80 weight=1;
}
upstream upload_pools {
server 10.0.0.10:80 weight=1;
}
upstream default_pools {
server 10.0.0.11:80 weight=1;
}
server {
listen 80;
server_name www.etiantian.org;
location / {
if ($http_user_agent ~* "MSIE")
{
proxy_pass http://static_pools;
}
if ($http_user_agent ~* "Chrome")
#if ($http_user_agent ~* "Firefox")
{
proxy_pass http://upload_pools;
}
proxy_pass http://default_pools;
include proxy.conf;
}
access_log off;
}
}
http故障服务器自动请求下一台。
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;