首页 > nginx反向代理到本机nodejs应用时速度很慢

nginx反向代理到本机nodejs应用时速度很慢

我用nodejs搭了一个网站,但是同网的其他的机器无法访问,我上网查,有人说需要一个web服务器发布出来,所以我本机又搭了nginx进行反向代理到我nodejs上,但是很慢。每次第一次访问都得等好长时间。但是直接访问nodejs的端口就很快。项目很小,不应该,所以我感觉是反向代理哪里设置的不对。求解?

#user  nobody;
worker_processes 2;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  65535;
}


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 300m;           #增加
    tcp_nopush     on;      #修改为on
    keepalive_timeout  60;  #修改为60
    tcp_nodelay on;        #增加
    server_tokens off;     #增加,不显示nginx版本信息
    gzip  on;  #修改为on
    gzip_min_length  1k;      #增加
    gzip_buffers     4 16k;   #增加
    gzip_http_version 1.1;    #增加
    gzip_comp_level 2;        #增加
    gzip_types       text/plain application/x-javascript text/css application/xml;  #增加
    gzip_vary on;  #增加
    #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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    #keepalive_timeout  65;

    #gzip  on;
     map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }

    server {
        listen       9080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #location / {
         #   root   html;
         #   index  index.html index.htm;
        #}
        location / {
            proxy_pass http://localhost:8080; #nodejs 
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            #time out settings
            proxy_connect_timeout 159s;
            proxy_send_timeout   600;
            proxy_read_timeout   600;
            proxy_buffer_size    64k;
            proxy_buffers     16 32k;
            proxy_busy_buffers_size 64k;
            proxy_temp_file_write_size 64k;
            proxy_pass_header Set-Cookie;
            proxy_redirect     off;
            proxy_hide_header  Vary;
            proxy_set_header   Accept-Encoding '';
            proxy_ignore_headers Cache-Control Expires;
            proxy_set_header   Referer $http_referer;
            proxy_set_header   Host   $host;
            proxy_set_header   Cookie $http_cookie;
            proxy_set_header   X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

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

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

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


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

你用 nginx 设置了这么多 HTTP header 做什么?

只需要这三个,其他的删掉:

proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

把这一段也删掉:

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

很可能是你设了 upgrade 的 header 造成的问题。


在server下增加下面的配置项,看看是否有效果

ip_hash;
【热门文章】
【热门文章】