nginx 负载均衡 最初级版本

本文介绍了如何配置Nginx进行最基础的负载均衡,包括使用ip_hash和weight权重设置,以及虚拟域名的配置。通过示例展示了在三台服务器之间进行负载均衡的实现方法,强调了负载均衡服务器不能提供自身服务的原则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

遇到的问题 一开始 写了2个ip 发现始终只能走其中的一个 最后才发现 原来配置负载均衡的服务器 不能自己再提供服务了 所以- -

想要负载均衡一般来说 需要3台机器 第一台 是做代理的  再提供另外2个服务器配置就可以了 

fuzai1 做提供服务 代码如下

ip_hash 则这样写

        upstream web1{

                #ip_hash;
                server 192.168.0.203;
                server 192.168.0.204;

        }
否则如下

 

http里增加下面这个
        upstream web1{

                server 192.168.0.203 weight=1;
                server 192.168.0.204 weight=1;

        }
server里增加


        location /{

                index index.php index.html index.htm;
                root  /home/wwwroot/default;
                proxy_pass http://web1;

                proxy_set_header Host $host;
}

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;
        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;



        upstream web1{

                server 192.168.0.203 weight=1;
                server 192.168.0.204 weight=1;

        }




server
    {
        listen 80;
        #listen [::]:80 default_server ipv6only=on;
        server_name www.fk1.com;
        #index index.php index.html index.htm;
        #root  /home/wwwroot/default;

        location /{

                index index.php index.html index.htm;
                root  /home/wwwroot/default;
                proxy_pass http://web1;
                proxy_set_header Host $host;

        }

        #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;
}

虚拟域名配置

[root@localhost conf]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.0.202  www.fk1.com
192.168.0.203  www.fk2.com 
192.168.0.204  www.fk3.com
 

fuzai1的 ip为192.168.0.202  

fuzai2的ip为192.168.0.203

fuzai3的ip为 192.168.0.204

 

接下来每次请求 192.168.0.202

 

请求虚拟域名也行

 

这个是ip轮询的方式 为了session 图片啥的奇葩问题 可以写成ip_hash 这样 这是最基本的负载均衡配置

后面未来还会涉及到 文件同步等等 这些问题。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值