Nginx做负载均衡,请求获取本地静态文件速度缓慢

发布于 2022-09-07 22:51:59 字数 4092 浏览 43 评论 0

有三台服务器,一台做Nginx反向代理服务器,并且也作为静态服务器,两台作为web服务器,现在如果是使用代理服务器访问网站发现比直接访问web服务器的时间长,通过使用浏览器查看具体情况,发现获取静态资源(例如js文件,图像文件等)的时间变长,但是如果直接访问单个文件速度又是挺快的,有没有人帮忙看下我配置的Nginx的配置文件有没有问题,还是其他原因?谢谢指导!

一下是Nginx服务器有关Nginx的配置:


user  www www;
worker_processes  1; # 一般设置成cpu的总核数

error_log   logs/error.log crit; # 错误信息存储的文件
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        logs/nginx.pid;
worker_rlimit_nofile 65535;

events {
    use epoll;
    worker_connections  65535; # 每个worker进程的最大连接数
}


http {
    include       mime.types; # nginx支持的媒体文件类型,相对路径为同目录conf下的其他文件
    default_type  application/octet-stream; # 默认的媒体文件

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ' 
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"'; # 访问日志的格式

    access_log  /home/wwwlogs/access.log  main; # 访问日志

    sendfile        on; # 启用sendfile传输模式,此模式是‘零拷贝’
    tcp_nopush     on; # 只在 sendfile on 时有效。让数据包挤满到一定程度才发送出去,集满之前被堵塞

    #keepalive_timeout  0;
    keepalive_timeout  65; # keepalive 的超时时间

    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 128k;
    gzip on; # 启用gzip压缩响应文件报文
    gzip_static on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 4;
    gzip_types text/plain application/x-javascript text/css application/xml;
    gzip_vary on;

    proxy_cache_path /usr/local/nginx/cache_dir levels=1:2 keys_zone=cache_one:20m max_size=1g;
    upstream load_balance {
        #server 127.0.0.1:80;
        server 132.232.163.17:80;
        server 132.232.159.127:80;
        ip_hash;
    }
    server {
        listen       80;
        server_name  118.24.167.113;
        index index.html index.htm index.php; # 站点主页文件
        #charset koi8-r;
        #root /home/html/www; # 站点根目录
        #access_log  logs/host.access.log gzip buffer=32k main;

        location / {
           #设置反向代理
           proxy_pass  http://load_balance;
           proxy_redirect off;
           proxy_buffering off;
           proxy_set_header   Host             $host;
           proxy_set_header X-Real-IP $remote_addr; 
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
           client_max_body_size 50m; 
           client_body_buffer_size 256k; 
           proxy_connect_timeout 1; 
           proxy_send_timeout 1; 
           proxy_read_timeout 1; 
           proxy_buffer_size 256k; 
           proxy_buffers 4 256k; 
           proxy_busy_buffers_size 256k; 
           proxy_temp_file_write_size 256k; 
           proxy_next_upstream error timeout invalid_header http_500 http_503 http_404; 
           proxy_max_temp_file_size 128m;
           proxy_cache cache_one;
           proxy_cache_valid 200 1h;
           proxy_cache_valid 404 1m;
           proxy_cache_valid any 5m;
           if ($request_uri ~* \.(ico|css|js|gif|png|jpe?g)$) {
                expires 24h;
                break;
           }
        }
        location ~ \.(gif|jpg|js|png|jpeg) {
           root /home/html/www;
       #access_log off;
       #expires 1d;
           #proxy_pass  http://localhost:80;
       autoindex on;
    }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html; # 出现50x错误时,使用/50x.html页返回给客户端
        location = /50x.html {              # 定义手动输入包含/50x.html时的location
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
            deny  all; # 禁止所有网址访问
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朦胧时间 2022-09-14 22:51:59

预估 服务器下行流量太低
一般来说 静态文件 放在cdn上比较好

通过使用浏览器查看具体情况,发现获取静态资源(例如js文件,图像文件等)的时间变长
但是如果直接访问单个文件速度又是挺快的
这么说,nginx 配置是没问题的
单个文件下行流量是够的
多个在一起下载 可能下行流量就不够用了

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文