Nginx traffic is going to wrong upsteam when mixing named servers and default servers
- by Morgan
I have the below config file for nginx. The problem is all traffic is going to upstream clustera. How do I configure nginx to only send traffic for example.com to clustera and all the rest to clusterb?
user www-data;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
log_format cache '\n*** $remote_addr [$time_local] '
'[$upstream_cache_status] $upstream_response_time '
'$host "$request" ($status) $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'Cache-Control: $upstream_http_cache_control '
'Expires: $upstream_http_expires '
;
access_log /var/log/nginx/access.log cache;
sendfile on;
keepalive_timeout 65;
gzip on;
gzip_vary on;
gzip_comp_level 6;
gzip_proxied any;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_buffers 16 8k;
include /etc/nginx/conf.d/*.conf;
proxy_cache_key "$scheme$host$request_uri";
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=main:10m max_size=1g inactive=30m;
upstream clustera {
ip_hash;
server a.example.com:80;
}
upstream clusterb {
ip_hash;
server b.example.com:80;
}
client_max_body_size 20m;
client_body_buffer_size 128k;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
# host for example.com should send traffic to clustera
server {
listen 80;
server_name example.com;
location ~*(png|jpeg|jpg|gif|ico|css|js)$ {
proxy_pass http://clustera;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache main;
proxy_cache_valid 200 5m;
proxy_cache_valid 302 1m;
}
location / {
proxy_pass http://clustera;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
# host for everyone else. traffic goes to clusterb
server {
listen 80;
server_name _;
if ( $http_user_agent ~* (spider|crawler|slurp) ) {
return 503;
}
set $slow 0;
if ( $http_user_agent ~* (bot) ) {
set $slow 1;
}
if ( $slow ) {
set $limit_rate 1k;
}
location ~*(png|jpeg|jpg|gif|ico|css|js)$ {
proxy_pass http://clusterb;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache main;
proxy_cache_valid 200 5m;
proxy_cache_valid 302 1m;
}
location /images {
proxy_pass http://clisterb;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache main;
proxy_cache_valid 200 5m;
proxy_cache_valid 302 1m;
}
location / {
proxy_pass http://clusterb;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}