V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
cryboy007
V2EX  ›  问与答

Nginx 转发比 tomcat 还慢,求大佬帮忙瞧一瞧

  •  
  •   cryboy007 · 2021-03-21 21:03:11 +08:00 · 1800 次点击
    这是一个创建于 1132 天前的主题,其中的信息可能已经有所发展或是发生改变。

    ```Nginx server { listen 80; listen 3009 ssl http2; server_name znsd.top;
    #ssl on;
    ssl_certificate /etc/nginx/ssl/5086991_znsd.top.pem;
    ssl_certificate_key /etc/nginx/ssl/5086991_znsd.top.key;
    ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    #ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
     proxy_set_header X-Forwarded-Proto  $scheme;
    #鎵嬪姩鍚敤 cipher 鍒楄〃;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
    ssl_buffer_size 4k;
    
    ssl_session_cache   shared:SSL:50m;
    

    #speed up first time. 1m ~= 4000 connections ssl_session_timeout 4h;

    location ~* /page/audio/ {
    
        root /etc/nginx  ;
        if (!-f $request_filename) { #请求的文件不存在,就反向代理
            #proxy_pass http://192.168.11.242:9527;
            #rewrite /page/audio /audio break;
            rewrite ^/page/audio/(.+?).mp3$ /audio/$1.mp3 break;
        }
    }
    
      location ~* /static/ {
    
        root /etc/nginx  ;
    }
    
    location ~* ^/page/$ {
    
        # 先找本地
        root /etc/nginx/static; # 这里的 html 是 root 安装目录的 html 文件夹,可以自定义
        if (!-f $request_filename) { #请求的文件不存在,就反向代理
            proxy_pass http://172.17.0.1:9527;
            break;
        }
    }
    
    location / {
        proxy_pass  http://172.17.0.1:9527/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_redirect     off;
        client_max_body_size 1024m;
        ### Most PHP, Python, Rails, Java App can use this header -> https ###
        proxy_set_header X-Forwarded-Proto  $scheme;
        proxy_connect_timeout 1; #后端服务器连接的超时时间_发起握手等候响应超时时间
        proxy_send_timeout 30;  #  连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理(也可以说是后端服务器处理请求的时间)
        proxy_read_timeout 60; #   后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据
    
    }
    

    }

    19 条回复    2021-03-23 18:00:49 +08:00
    cryboy007
        1
    cryboy007  
    OP
       2021-03-21 21:05:47 +08:00
    我写在 N1 上面的.使用 frp 穿透.Nginx 做转发.发现速度非常慢还做了动静分离.我在本地直接用 Nginx 请求内网也照样很慢.求大佬帮忙解答下<br/>
    (用的 Nginx):https://znsd.top:3009 .本想一起发.但是 V2EX 不让发.不知道为啥
    cryboy007
        2
    cryboy007  
    OP
       2021-03-21 21:08:12 +08:00
    Lax
        3
    Lax  
       2021-03-21 22:38:48 +08:00
    建立第一个连接消耗的实际很多。
    瀑布图上有请求各个阶段使用的实际时间,看一下哪部分用的最多。
    cryboy007
        4
    cryboy007  
    OP
       2021-03-21 22:46:33 +08:00
    @Lax 看不出来,就一个对应域名的那个慢.其他的资源是 100 毫秒内.如果直接请求 tomcat 就不会这样.十分不解..
    Lax
        5
    Lax  
       2021-03-21 23:02:01 +08:00
    鼠标放到 waterfall 下面对应请求的色条上面,看看显示的内容,具体是哪个阶段的问题
    cryboy007
        6
    cryboy007  
    OP
       2021-03-22 09:58:07 +08:00
    @Lax wating TTFB 4.9 秒.直接请求 tomcat 就不会....
    doveyoung
        7
    doveyoung  
       2021-03-22 14:54:35 +08:00
    你图上 192.168.11.242 是什么东西,还有最好也查一下 nginx 的日志
    doveyoung
        8
    doveyoung  
       2021-03-22 14:56:02 +08:00
    192.168.11.242:90
    nginx 上配置没有 90……
    cryboy007
        9
    cryboy007  
    OP
       2021-03-22 17:02:29 +08:00
    @doveyoung 那是我内网直接访问 192.168.11.242 是内网 90 是 http 3009 是 https.另外一个 9527 是内网的 tomcat
    看日志也看不出来.是正常的...
    cryboy007
        10
    cryboy007  
    OP
       2021-03-22 17:06:31 +08:00
    @doveyoung 然后用的是 frp 穿透.搞了好久还是没搞好...其他几个网站网速缺很快.比如 https://znsd.top:3008,https://znsd.top:3007
    cryboy007
        11
    cryboy007  
    OP
       2021-03-22 17:20:07 +08:00
    @doveyoung
    @Lax http://znsd.top:9527/这个是直连 tomcat 的.秒开 http://znsd.top:90 这个是 Nginx 的 10 多秒
    doveyoung
        12
    doveyoung  
       2021-03-23 10:55:30 +08:00
    1. nginx 里日志调整到 debug,抓一会儿看看 error_log logs/error.log debug;
    2. nginx 里配置没有 90 端口,但是访问使用的 90 端口,是用 frp 转发的吗,不太明白
    3. nginx 配置里加个静态的 hello world 页面看看还慢吗
    4. 虽然不太可能,但是还是看一下吧,转发给 172.17.0.1:9527 这里有没有网络问题
    cryboy007
        13
    cryboy007  
    OP
       2021-03-23 12:46:31 +08:00
    @doveyoung
    1.不太会弄.因为 nginx 实在 docker 里面安装的 /..
    2.对用的 frp 转发.
    3 试过 Nginx 直接返回静态页面非常快.
    4.网络没问题..

    感觉是因为 nginx 与 tomcat 之间的代理出啦什么问题.导致很慢
    cryboy007
        14
    cryboy007  
    OP
       2021-03-23 12:51:30 +08:00
    doveyoung
        15
    doveyoung  
       2021-03-23 13:20:10 +08:00
    @cryboy007 我上面回复的 1 的意思是
    编辑 nginx 的配置文件,不是直接用 error_log logs/error.log debug; 这个命令

    ssl_session_cache shared:SSL:50m; 这一行后面追加一行然后保存重启 nginx
    error_log logs/error.log debug;

    看起来实际提供服务的是 9527 端口,那么 nginx 里 location 部分的配置完全可以只留下一个 location / xxxxxx
    前面的几个 location 区块全都注释掉,保存,重启 nginx
    观察日志文件 error.log
    --------
    老哥要不去学一下 nginx 的配置吧……
    cryboy007
        16
    cryboy007  
    OP
       2021-03-23 14:08:05 +08:00
    @doveyoung ..我把日志加进去了,你可以看上面这个图 ![debug]( https://files.catbox.moe/lt5g0d.png)
    cryboy007
        17
    cryboy007  
    OP
       2021-03-23 14:10:22 +08:00
    @doveyoung 不折腾了.我放弃了....
    doveyoung
        18
    doveyoung  
       2021-03-23 15:21:39 +08:00
    别啊,胜利就在眼前
    为什么 https://znsd.top:3009 就很快
    http://znsd.top:90 这么慢

    3009 和 90 的端口是个什么情况,为什么 90 端口用了 frp,3009 没用 frp
    还有你上面的日志里,没抓到访问 90 端口的日志
    cryboy007
        19
    cryboy007  
    OP
       2021-03-23 18:00:49 +08:00
    @doveyoung 都是用的 frp. 3009 和 90 分别是 https 和 http.除此之外没区别了.9527 是 tomcat.非常快
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1568 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 17:23 · PVG 01:23 · LAX 10:23 · JFK 13:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.