2018-12-16 15:48:39 2451次浏览 7条回答 0 悬赏 100 金钱

写了个简单的nginx负载均衡,发现ie,火狐,edge浏览器可以正常,可是谷歌浏览器就是不行
过程如下:我是ubuntu系统(虚拟机),ip是192.168.10.100
现在有2个项目,一个是basic2一个是basic3,nginx对应的配置文件如下:
basic2项目配置文件是test2.conf,配置了8078端口

server {
        listen 8078;
        listen [::]:8078;

        root /var/www/html/basic2/web;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html  index.php;


        location / {
                root /var/www/html/basic2/web;
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                #try_files $uri $uri/ =404;
                if (!-e $request_filename){
                  rewrite ^/(.*) /index.php last;
                }
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
                root /var/www/html/basic2/web;
                include snippets/fastcgi-php.conf;
        #
        #       # With php7.0-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php7.0-fpm:
                fastcgi_pass unix:/run/php/php7.1-fpm.sock;
        }

        location ~ /\.ht {
                deny all;
        }
}

basic3项目配置文件是test3.conf,配置了8079端口

server {
        listen 8079;
        listen [::]:8079;

        root /var/www/html/basic3/web;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html  index.php;

        location / {
                root /var/www/html/basic3/web;
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                #try_files $uri $uri/ =404;
                if (!-e $request_filename){
                  rewrite ^/(.*) /index.php last;
                }
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
                root /var/www/html/basic3/web;
                include snippets/fastcgi-php.conf;
        #
        #       # With php7.0-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php7.0-fpm:
                fastcgi_pass unix:/run/php/php7.1-fpm.sock;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
                deny all;
        }
} 

在浏览器分别访问,结果如下图:
8078.png
8079.png

至此都可以访问,现在我配置个负载均衡的服务,其配置文件是test.conf,内容如下

server {
        listen 80;
        listen [::]:80;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html  index.php;

        server_name www.yiibasic.cn;

        location / {
            proxy_pass   http://webservers;
       }
}

还有nginx的nginx.conf配置如下:

http {

        sendfile off;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##
        upstream webservers {
            server 192.168.10.100:8078 weight=1;
            server 192.168.10.100:8079 weight=3;
        }

        include /etc/nginx/conf.d/*.conf;
}

使用权重 1:3 的模式,然后在本机host文件加入 192.168.10.100 www.yiibasic.cn
当我在火狐,ie下访问 http://www.yiibasic.cn/ 可以看到 3 次 basic3 出现一次 basic2 出现的周期现象,说明成功了,可是在谷歌上始终是 basic3,偶尔一下 basic3 一下 basic2,缓存也清除了,没有作用,不知道是什么原因??

您需要登录后才可以回答。登录 | 立即注册
xyf90314
副总裁

xyf90314

注册时间:2015-03-04
最后登录:2023-03-13
在线时长:95小时23分
  • 粉丝21
  • 金钱5257
  • 威望40
  • 积分6607

热门问题