linux下 nginx + tomcat 服务器集群 (2)

来源:互联网 发布:wineskin for mac 编辑:程序博客网 时间:2024/05/01 18:15

在上一节中我们熟悉了linux环境,并成功搭建了nginx服务器和两个tomcat服务器,现在我们需要将他们连接起来。

注意,我们是将tomcat连接到nginx的service虚拟目录,这样nginx即可以做tomcat的负载均衡,同时还兼顾静态页面、图片等处理。

为了简化配置,我们在上一节中,将tomcat的ROOT服务也配置到了相同的目录。

整个系统的编码配置为:utf-8


配置nginx,增加upstream,作为tomcat均衡负载器

vi /usr/local/nginx/conf


错误说明:

403 :   需设置正确的用户名 / 密码。

proxypass 路径设置,如果加后缀: http://localhost/ ,则转发后的路径为: http://localhost:8080/

                                如果不添加后缀:http://localhost , 则转发后的路径为:http://localhost:8080/service/



#user  nobody;
user   root root;
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;

    #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;
    upstream localhost {
         server  localhost:8080;
         server  localhost:8081;
     }

    server {
        listen       80;
        server_name  localhost;
        charset      utf-8;

        location / {
            root   /home/Server/domain;
            index  index.html index.htm;
        }

        location /service/ {
            root                        /home/Server/domain/service;
            index                       index.html index.htm index.jsp;
            proxy_pass                  http://localhost/;
            proxy_set_header X-Real-IP $remote_addr;
            client_max_body_size 100m;
        }

        location ~^/(WEB-INF)/ {
            deny all;
        }

        #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 ssl;
    #    server_name  localhost;

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

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

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

}


0 0
原创粉丝点击