nginx配置集群

1、准备两个Tomcat

首先在Linux机器上部署两个Tomcat,端口分别为80和8080

2、分别部署测试应用

在两个tomcat下分别部署同一个应用testapp,很简单,就是在页面显示当前系统时间:

testapp

    --WEB-INF

        --web.xml

    --index.jsp

index.jsp内容:

<html>
    <body>
        <center>Now time is :<%=new java.util.Date() %></center>
    </body>
</html>

3、启动两个Tomcat

查看是否能正常运行

4、修改nginx配置

1)配置监控端口

server {
    listen 8080;
    ......
}

2)配置服务器集群

upstream testapp {  #服务器集群名字
    server    120.78.144.82:8080  weight=2;
    server    120.78.144.82:80  weight=1;
}

3)配置URL匹配路径

location /testapp {
    proxy_pass http://testapp;
    proxy_redirect default;
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,
            X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
    }

------------------------------------------------------------

5、修改配置后重启nginx

[root@iZwz95a6wosz6klzf7o6hcZ sbin]# pwd
/usr/local/nginx/sbin
[root@iZwz95a6wosz6klzf7o6hcZ sbin]# ./nginx -s reload

6、测试

关闭8080端口的Tomcat,如果仍能访问到testapp页面,则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 {
use epoll;    
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  /gxxj/nginx/logs/access.log;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    #干线巡检的集群  
    upstream   mobile_gxxj {  #服务器集群名字   
        server    132.228.125.45:6001  weight=1;#服务器配置   weight是权重的意思,权重越大,分配的概率越大。  
        #server   132.228.228.194:6003  weight=2;  
    } 
    #光网助手的集群
    upstream   mobile_gwzs { 
        server    132.228.228.194:6005  weight=1;#服务器配置   weight是权重的意思,权重越大,分配的概率越大。
    }

    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        location /ins/mobile/gxxj {
            proxy_pass  http://mobile_gxxj;
            proxy_redirect default; 
            add_header 'Access-Control-Allow-Origin' '*';
                    add_header 'Access-Control-Allow-Credentials' 'true';
                    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        }
        location / {  
        proxy_pass http://132.228.237.107:6001;#主要是这里,这是tomcat1的端口和项目  
        add_header 'Access-Control-Allow-Origin' '*';
                    add_header 'Access-Control-Allow-Credentials' 'true';
                    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';  
        }  
        #光网助手手机端代理
        location /ins/mobile/cableCheck{
                    proxy_pass  http://mobile_gwzs;
                    proxy_redirect default;
                    add_header 'Access-Control-Allow-Origin' '*';
                    add_header 'Access-Control-Allow-Credentials' 'true';
                    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        }
        #点温代理
        location /jfxj {
            proxy_pass  http://mobile_gxxj;
            proxy_redirect default;
                    add_header 'Access-Control-Allow-Origin' '*';
                    add_header 'Access-Control-Allow-Credentials' 'true';
                    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        }

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

}
原文地址:https://www.cnblogs.com/xyhero/p/b3d3489a8022953222df82bef64bb1fe.html