首页 > nginx做tomcat的负载均衡代理,为什么不能正确解析网页数据

nginx做tomcat的负载均衡代理,为什么不能正确解析网页数据

最近做的实验环境,在一台机器上部署两个tomcat目录,使用同一个war包的docBase,不同端口号,前面部署了一个nginx,做了负载均衡配置,将用户请求随机分配到两个跑着相同应用的tomcat,刚开始是静态图片等不能分流至tomcat,后来在server配置里使用local /{ ... }设置全部分流,现象就成了登录界面都解析不了了,图片也不能正常显示,而且速度明显没直连一个tomcat快,请教大神们该怎样修改配置达到负载均衡效果。

操作系统版本

[root@rhel68 ~]# uname -a
Linux rhel68 2.6.32-642.el6.x86_64 #1 SMP Wed Apr 13 00:51:26 EDT 2016 x86_64 x86_64 x86_64 GNU/Linux 

tomcat 版本

[tomcat@rhel68 bin]$ ./version.sh
Using CATALINA_BASE:   /home/tomcat/tomcat-aos_1
Using CATALINA_HOME:   /home/tomcat/tomcat-aos_1
Using CATALINA_TMPDIR: /home/tomcat/tomcat-aos_1/temp
Using JRE_HOME:        /usr/java/default
Using CLASSPATH:       /home/tomcat/tomcat-aos_1/bin/bootstrap.jar:/home/tomcat/tomcat-aos_1/bin/tomcat-juli.jar
Server version: Apache Tomcat/8.0.35
Server built:   May 11 2016 21:57:08 UTC
Server number:  8.0.35.0
OS Name:        Linux
OS Version:     2.6.32-642.el6.x86_64
Architecture:   amd64
JVM Version:    1.7.0_101-b14
JVM Vendor:     Oracle Corporation  

tomcat server 的Host配置片段

 <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      <Context path="/aos" reloadable="true" docBase="/home/tomcat/war/aos.war" />
      </Host> 

nginx版本

[nginx@rhel68 sbin]$ ./nginx -v
nginx version: nginx/1.11.1 

nginx具体配置

[nginx@rhel68 conf]$ cat nginx.conf
#运行用户  
#user  nobody;  
#开启进程数 <=CPU数  
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 {  
    #Linux下打开提高性能  
    use epoll;  
    #每个进程最大连接数(最大连接=连接数x进程数)  
    worker_connections  1024;  
}  
   
   
http {  
    #文件扩展名与文件类型映射表  
    include       mime.types;  
    #默认文件类型  
    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  logs/access.log  main;  
       
    #设定请求缓冲  
    client_header_buffer_size 1k;  
    large_client_header_buffers 4 4k;  
    server_names_hash_bucket_size 128; 
    #打开发送文件  
    sendfile        on;  
    #tcp_nopush     on;  
 
    tcp_nodelay     on; 
    #keepalive_timeout  0;  
    keepalive_timeout  65;  
       
    #客户端上传文件大小控制  
    client_max_body_size 8m;  
       
    #打开gzip压缩  
    #gzip  on;  
       
    #设定负载均衡的服务器列表  
    upstream aos_server {  
        #weigth参数表示权值,权值越高被分配到的几率越大  
        #本机上的Squid开启3128端口  
        server 10.0.2.68:6060 weight=1;  
        server 10.0.2.68:7060 weight=1;  
        #server 192.168.8.3:80 weight=6;  
    }  
    #第一个虚拟主机  
    server {  
        #监听IP端口  
        listen       5060;  
        #主机名  
        server_name  localhost;  
        #root    
           
        #设置字符集  
        #charset koi8-r;  
        #本虚拟server的访问日志 相当于局部变量  
        #access_log  logs/host.access.log  main;  
        #日志文件输出格式  
        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
        #                  '$status $body_bytes_sent "$http_referer" '  
        #                  '"$http_user_agent" "$http_x_forwarded_for"';  
           
        #静态文件缓存时间设置  
        #location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {           
        #    expires 30d;  
        #}  
           
        #静态文件缓存时间设置  
        #location ~ .*\.(js|css)?$ {           
        #    expires 1h;  
        #}  
           
        #对本server"/"启用负载均衡  
        location / {  
        #    root   html;
        #    index  index.html index.htm login.jhtml index.jhtml;
            proxy_pass http://aos_server;  
            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;  
            client_max_body_size 10m;  
            client_body_buffer_size 128k;  
            proxy_connect_timeout 90;  
            proxy_send_timeout 90;  
            proxy_read_timeout 90;  
            #proxy_buffering off;
            proxy_buffer_size 4k;  
            proxy_buffers 4 32k;
            proxy_busy_buffers_size 64k;  
            proxy_temp_file_write_size 64k;  
        }  
           
        #设定查看Nginx状态的地址  
        #location /NginxStatus {  
        #    stub_status on;  
        #    access_log on;  
        #    auth_basic “NginxStatus”;  
        #    auth_basic_user_file conf/htpasswd;  
        #}  
   
   
   
        #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       localhost:8666;  
        #主机名  
     #  server_name  xx;  
        #WEB文件路径  
     #  root         xx;  
        #默认首页  
     #  index        HomePage.html;          
        #location / {  
        #    #这里相当于局部变量  
        #    root   xx;  
        #    index  HomePage.html;  
        #}  
    #}  
   
   
    # HTTPS server HTTPS SSL加密服务器  
    #  
    #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  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;  
    #    ssl_prefer_server_ciphers   on;  
   
    #    location / {  
    #        root   html;  
    #        index  index.html index.htm;  
    #    }  
    #}  
   
} 

补充一点,tomcat和nginx都是建立的独立用户,有自己的用户组,软件等全套都在/home目录下

[root@rhel68 ~]# cd /home
[root@rhel68 home]# la
total 28
drwxr-xr-x.  7 root   root     4096 Jun  2 15:28 .
drwx------.  9 zabbix zabbix   4096 Jun  3 13:50 zabbix
drwx------.  4 oracle oinstall 4096 Jun  8 14:31 oracle
drwxrwxr-x.  7 redis  net      4096 Jun  9 22:10 redis
dr-xr-xr-x. 26 root   root     4096 Jun 11 09:39 ..
drwx------. 12 tomcat net      4096 Jun 11 09:52 tomcat
drwxrwxr-x.  5 nginx  net      4096 Jun 11 10:00 nginx
【热门文章】
【热门文章】