nginx 配置负载均衡

【主服务器】

upstream myserver {
        #把请求转发给连接数比较少的服务器
        least_conn;
        server 192.168.1.1:8080;
        server 192.168.1.2:8080;
    server 192.168.1.3:8080;
    }
    
server {
    listen 80;
    server_name www.test.com;

    location / {
        proxy_pass http://myserver;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}


server {

  listen 8080;
  listen 443;
  server_name  www.test.com;
  root /data/www.test.com;
  index index.php index.html;
  charset utf-8;
  
  access_log  /var/log/nginx/access_www.test.com.log;
  error_log  /var/log/nginx/error_www.test.com.log;

  location = /robots.txt { allow all; access_log off; log_not_found off; }
  location = /favicon.ico { allow all; access_log off; log_not_found off; }

  error_page 401 /401.html;
  error_page 403 /403.html;
  error_page 404 /404.html;
  error_page 500 502 503 504 /50x.html;

  location ~ .*\.(php|php5)?($|/)
  {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_split_path_info ^(.+\.php)(.*)$;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
  }

  location ~ /\.ht {
    deny  all;
  }

}

【从服务器】

server {

  listen 8080;
  listen 443;
  server_name  www.test.com;
  root /data/www.test.com;
  index index.php index.html;
  charset utf-8;
  
  access_log  /var/log/nginx/access_www.test.com.log;
  error_log  /var/log/nginx/error_www.test.com.log;

  location = /robots.txt { allow all; access_log off; log_not_found off; }
  location = /favicon.ico { allow all; access_log off; log_not_found off; }

  error_page 401 /401.html;
  error_page 403 /403.html;
  error_page 404 /404.html;
  error_page 500 502 503 504 /50x.html;

  location ~ .*\.(php|php5)?($|/)
  {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_split_path_info ^(.+\.php)(.*)$;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
  }

  location ~ /\.ht {
    deny  all;
  }

}

你可能感兴趣的:(nginx 配置负载均衡)