开发者社区> 问答> 正文

nginx代理tomcat下访问慢?报错

我在tomcat下放了一个jenkins.war,启动tomcat, 可以正常访问jenkins,不慢。

现在我想使用nginx代理tomcat,当访问http://localhost/jenkins/的时候,自动转向hppt://locahost:8080/jenkins/ ,

出现的问题:访问太慢太慢了。可以排除是tomcat的问题。应该是nginx配置的问题。

nginx配置文件如下:


#user  nobody;
worker_processes  1;


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


events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    #server_names_hash_bucket_size 64;
    #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;


    #开启压缩功能,节省带宽,提高响应,但是会占CPU
    gzip              on;
    gzip_min_length      1000;
    #压缩指定的文件类型
    gzip_types        text/plain text/css application/x-javascript;


    large_client_header_buffers 4 16k;
    client_max_body_size    50m;
    client_body_buffer_size 256k;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    proxy_connect_timeout   300s;
    proxy_send_timeout      300s;
    proxy_read_timeout      300s;
    proxy_buffer_size       64k;
    proxy_buffers           4 32k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size 128k;
    proxy_ignore_client_abort on;
    server {
        listen       80 ;
        server_name  127.0.0.1;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;

        location /jenkins {
            #代理到本机上的8080端口,没有做负载
            proxy_pass http://localhost:8080;
            proxy_redirect          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;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
           root   html;
        }
    }
}

是不是我的location配置错了?还是proxy配置错了。

报错信息如下

2015/11/30 10:00:32 [error] 2688#4960: *89 upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while connecting to upstream, client: 127.0.0.1, server: 127.0.0.1, request: "POST /jenkins/ajaxExecutors HTTP/1.1", upstream: "http://[::1]:8080/jenkins/ajaxExecutors", host: "localhost", referrer: "http://localhost/jenkins/"

部门请求资源会报这个错误。


展开
收起
爱吃鱼的程序员 2020-06-10 10:25:42 760 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB


    #user nobody;
    worker_processes 1;


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


    #pid    logs/nginx.pid;




    events{
      worker_connections 1024;
    }




    http{
      include   mime.types;
      default_type application/octet-stream;
      #server_names_hash_bucket_size64;


      #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;


      #开启压缩功能,节省带宽,提高响应,但是会占CPU
      gzip       on;
      gzip_min_length   1000;
      #压缩指定的文件类型
      gzip_types    text/plaintext/cssapplication/x-javascript;


    upstreamjenkins {
    server127.0.0.1:8080; 
    }


    upstreamsolr {
    server127.0.0.1:8080; 
    }






    server{


    listen   80;
    server_name 127.0.0.1;


    #charsetkoi8-r;


    #access_log logs/host.access.log main;


    large_client_header_buffers416k;
    client_max_body_size  50m;
    client_body_buffer_size256k;
    client_header_timeout3m;
    client_body_timeout3m;
    send_timeout3m;






    location/nginx{
    roothtml;
    indexindex.html;
    }





    location/solr{
    proxy_passhttp://solr;
    proxy_redirect     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;
    proxy_connect_timeout 600s;
    proxy_send_timeout   600s;
    proxy_read_timeout   600s;
    proxy_buffer_size   64k;
    proxy_buffers     432k;
    proxy_busy_buffers_size64k;
    proxy_temp_file_write_size128k;
    proxy_ignore_client_aborton;
    }


    location /jenkins{
    proxy_passhttp://jenkins;
    proxy_redirect     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;


    proxy_connect_timeout 600s;
    proxy_send_timeout   600s;
    proxy_read_timeout   600s;
    proxy_buffer_size   64k;
    proxy_buffers     432k;
    proxy_busy_buffers_size64k;
    proxy_temp_file_write_size128k;
    proxy_ignore_client_aborton;
    }




    error_page 500502503504 /50x.html;
    location=/50x.html{
      root html;
    }
    }

    }


    加一下upstram就好了。很奇怪。

    看日志是超时。

    有防火墙的话,你再看下本地访问8080端口有问题没

    你把worker_processes改成auto;

    是linux服务器吗?是的话events里面加上useepoll;

    把proxy_passhttp://localhost:8080;中的localhost改成127.0.0.1就行了。就是本机测试的。设了负载均hen就好了?不用设,将localhost改成ip地址就好了。
    2020-06-10 10:26:00
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
《Nginx 代理系统常用手册》 立即下载
CentOS Nginx PHP JAVA 多语言镜像使用手 立即下载
CentOS Nginx PHP JAVA多语言镜像使用手册 立即下载