-
在nginx里面配置一个upstream,然后把相关的服务器ip都配置进去。然后采用轮询的方案,然后在nginx里面的配置项里,proxy-pass指向这个upstream,这样就能实现负载均衡。
nginx的负载均衡有4种模式:
1)、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
2)、weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
2)、ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
3)、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
4)、url_hash(第三方)
# 前面说明
利用3台服务器来实现nginx下的负载均衡,1主+2辅。主根据权重随机转发请求到对应服务器下。但是通常为了保证最大带宽利用,保证服务器都在一个内网里。
每次请求会根据负载权重分发到任意一台辅服务器,请确保辅服务器的配置和系统可以全部一样。另外,php系统写的日志可能在任意一台里面,不用让每台服务器都保持一样的日志。
步骤(本地虚拟机三台服务器:
192.168.1.219 主
192.168.1.247 辅1
192.168.1.241 辅2
# 在主服务上的nginx配置如下:
cd /etc/nginx
编辑 nginx.conf 文件中 http 加入 下面这段 (这里是按轮询配置(依次) weight\
ip_hash
)等请参考尾部链接
# 代到本机 8080端口
server{
listen 8080;
server_name load.com;
index index.html;
root /home/www;
}
# 负载均衡模块
upstream load.com {
server 192.168.1.241:80;
server 192.168.1.247:80;
server 127.0.0.1:8080;
}
#监听80端口的访问
server{
listen 80;
server_name load.com;
location / {
proxy_pass http://load.com;
#proxy_set_header Host $host;
#proxy_set_header X-Real-IP $remotr_addr;
#proxy_set_header X-Forwarde-For $proxy_add_x_forwarded_for;
}
}
在其它两台服务器上编辑 nginx.conf 文件
server{
listen 80;
server_name load.com;
root /home/www;
location / {
index index.html;
}
}
# 分别修改三天服务器下的 /home/www/inde.html 文件(没有请新建)
<
h4
> load2 </
h4
>
重启所有 nginx 服务
浏览器访问(分别被分发到不了不同的服务器)
-
==================================================================
多域名、多泛域名负载绑定示例:
比如绑定*.w2w2.cn、*.xk8ds2d.cn两个域名到负载上:
user www www;
worker_processes auto;
worker_cpu_affinity auto;
error_log /home/wwwlogs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
multi_accept off;
accept_mutex off;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
sendfile on;
sendfile_max_chunk 512k;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
#limit_conn_zone $binary_remote_addr zone=perip:10m;
##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.
server_tokens off;
access_log off;
# 代到本机 8080端口
server{
listen 8080;
server_name *.w2w2.cn *.xk8ds2d.cn;
index index.html index.php;
root /home/wwwroot/default;
include enable-php.conf;
}
# 负载均衡模块
upstream *.w2w2.cn {
server 103.242.133.21:80;
server 127.0.0.1:8080;
}
#监听80端口的访问
server{
listen 80;
server_name *.w2w2.cn;
location / {
proxy_pass http://*.w2w2.cn;
#proxy_set_header Host $host;
#proxy_set_header X-Real-IP $remotr_addr;
#proxy_set_header X-Forwarde-For $proxy_add_x_forwarded_for;
}
}
# 负载均衡模块
upstream *.xk8ds2d.cn {
server 103.242.133.21:80;
server 127.0.0.1:8080;
}
#监听80端口的访问
server{
listen 80;
server_name *.xk8ds2d.cn;
location / {
proxy_pass http://*.xk8ds2d.cn;
#proxy_set_header Host $host;
#proxy_set_header X-Real-IP $remotr_addr;
#proxy_set_header X-Forwarde-For $proxy_add_x_forwarded_for;
}
}
include vhost/*.conf;
}
副服务器nginx.conf代码:
user www www;
worker_processes auto;
worker_cpu_affinity auto;
error_log /home/wwwlogs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
multi_accept off;
accept_mutex off;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
sendfile on;
sendfile_max_chunk 512k;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
#limit_conn_zone $binary_remote_addr zone=perip:10m;
##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.
server_tokens off;
access_log off;
server
{
listen 80 default_server reuseport;
#listen [::]:80 default_server ipv6only=on;
server_name _;
index index.html index.htm index.php;
root /home/wwwroot/default;
#error_page 404 /404.html;
# Deny access to PHP files in specific directory
#location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
include enable-php.conf;
location /nginx_status
{
stub_status on;
access_log off;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\.
{
deny all;
}
access_log /home/wwwlogs/access.log;
}
include vhost/*.conf;
}
===========================================================
参考:
1)https://www.cnblogs.com/xdtx/p/9577436.html
2)https://blog.csdn.net/Rodgexue/article/details/79976610
-