nginx代理太慢了 [英] nginx proxy too slow

查看:780
本文介绍了nginx代理太慢了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在GoDaddy XLarge Cloud Server中部署的nginx服务器,配有8GB RAM和4个CPU。我对nginx的设置是将请求代理到Google App Engine应用程序。



问题是 nginx服务于静态文件太慢,有时会断开连接呈现网站完整或损坏的图像,CSS和JS文件。现在直接访问GAE应用程序,静态文件的服务非常快。



这是我的服务器nginx.conf文件:

 用户www -数据; 
worker_processes 1;
worker_rlimit_nofile 20480; #worker_connections * 4
pid /run/nginx.pid;

events {
use epoll;
worker_connections 4096;
multi_accept on;
}

http {

##
#基本设置
##

sendfile on;
tcp_nopush on;
tcp_nodelay on;
client_body_timeout 12;
client_header_timeout 12;
keepalive_timeout 15;
send_timeout 10;
types_hash_max_size 2048;
#server_tokens off;

##
#调整
#https://www.digitalocean.com/community/tutorials/how-to-optimize-nginx-configuration
##

client_body_buffer_size 10K;
client_header_buffer_size 1k;
client_max_body_size 8m;
large_client_header_buffers 2 1k;

#server_names_hash_bucket_size 64;
#server_name_in_redirect off;

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

##
#记录设置
##

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

##
#Gzip设置
##

gzip on;
gzip_disablemsie6;

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 / x-javascript text / xml application / xml application / xml + rss text / javascript;

##
##代理设置
##
proxy_buffering off;

##
#nginx-naxsi config
##
#如果您安装了nginx-naxsi
##
$ b,请取消注释$ b #include /etc/nginx/naxsi_core.rules;

##
#nginx-passenger config
##
#如果您安装了nginx-passenger
##
$ b,请取消注释$ b #passenger_root / usr;
#passenger_ruby / usr / bin / ruby​​;

##
#虚拟主机配置
##

包括/etc/nginx/conf.d/*.conf;
包含/ etc / nginx / sites-enabled / *;
}

更新
$ b

这里是应用程序服务器的网络图:



这里NGINX服务器的网络图(很慢) b
$ b

非常有帮助。 I have a a nginx server deployed in GoDaddy XLarge Cloud Server with 8GB RAM and 4 CPU. My setup for nginx is to proxy request to a Google App Engine application.

The problem is the nginx serves the static files too slow, sometimes breaking the connection rendering the website full or broken images, CSS and JS files. Now accessing the GAE app directly the static files are served really quick.

Here is my server nginx.conf file:

user www-data;
worker_processes 1;
worker_rlimit_nofile    20480; # worker_connections * 4
pid /run/nginx.pid;

events {
    use epoll;
    worker_connections 4096;
    multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    client_body_timeout 12;
    client_header_timeout 12;
    keepalive_timeout 15;
    send_timeout 10;
    types_hash_max_size 2048;
    # server_tokens off;

    ## 
    # Tweaks
    # https://www.digitalocean.com/community/tutorials/how-to-optimize-nginx-configuration
    ##

    client_body_buffer_size 10K;
    client_header_buffer_size 1k;
    client_max_body_size 8m;
    large_client_header_buffers 2 1k;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

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

    ##
    # 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/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ## 
    ## Proxy Settings
    ##
    proxy_buffering off;

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##

    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

Update

Here the Network graph for the application server:

Here the Network graph for the NGINX server (very slow):

  • What could be causing the slowness of nginx in this configuration?
  • Is this because GoDaddy Cloud Servers are slow? or something is really wrong with the NGINX configuration?
  • What configuration can make the proxy work fast?

解决方案

Try optimise these:

1.worker process since your have a 4 core CPU and you are serving quite a lot of files in one request, So the value of it should be at least 4, or the value of this

grep processor /proc/cpuinfo | wc -l

2.use CDN for common used js files.

I see you use some common library(jquery-1.10.2.min.js, Angular-1.4.3.js, fontawesome-webfont.wotf2 etc) directly served by GAE. These files take seconds to load. you should try to serve these files through CDN instead.

3.Do a test with Google PageSpeed Tools, It's very helpful.

这篇关于nginx代理太慢了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆