nginx 通过方向代理实现负载均衡,负载均衡是大流量网站要做的措施,单从字面上的意思来理解为N台服务器平均分担负载,不会因为某一台服务器负载高宕机而影响用户访问网站,负载均衡至少需要三台服务器,
既然是负载均衡,分摊服务器压力,那么一台服务器最好就负责一个网站。所以只需要配置一个server即可
1.首先是主服务器:
upstream nginx.c-dancer.com{ server 111.111.111.111:80 weight=2;#权重为2/5 server 222.222.222.222:80 weight=3;#权重为3/5 } server{ listen 80; server_name xxx.xxx.com; location / { proxy_pass http://xxx.xxx.com; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
2.两台从服务器
server { listen 80 default_server; listen [::]:80 default_server; server_name _; #root /usr/share/nginx/html; root /var/www/html; index index.html index.php; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000;#php-fpm的默认端口是9000 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } }
注意
systemctl restart nginx时失败;原因查看systemctl status nginx;
配置默认访问文件之间使用空格分开 index index.html index.php
配置的前面和中间的尽量使用一个空格,或者一个tal键。
当启动正常时,但访问出现500错误,可能是location / {中出现错误}
原文:http://www.cnblogs.com/jackylee92/p/6284025.html