写了个简单的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;
}
}
在浏览器分别访问,结果如下图:
至此都可以访问,现在我配置个负载均衡的服务,其配置文件是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,缓存也清除了,没有作用,不知道是什么原因??
PS:我一个朋友说这个和favicon.ico有关系,其他浏览器只有发现网页的 link标签指定了 favicon 才会去请求, chrome 没看到这个标签也会去请求的
我也遇到这个问题了。我发现不能点击刷新按钮,要在浏览器回车,才有效果。edge效果正常,可以点击刷新,有效果。