nginx实现反向代理

场景:比如我们在两台机器上各有一个tomcat分别为A和B,A的端口为8080;B的端口为9090;两个服务器上放入同一个项目TestNgix,那么我们在访问A服务器上的项目使用链接http://192.168.10.11:8080/TestNgix访问B服务器上的项目使用链接http://192.168.10.12:9090/TestNgix,在实际应用当中我们会做集群配置,在不同的机器上部署同一个项目,通过访问一个链接实现访问A和B服务上的同一个项目,如果A服务挂了,可以自动切换到B服务器上,正常访问项目。如何才能实现访问一个链接能够访问不同端口的项目呢?很简单,Nginx就可以实现。

修改ngix.conf文件

#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;
    upstream  ngixDemo{
    server 192.168.10.11:8080;
    server 192.168.10.12:9090;
    }

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

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://ngixDemo;  
        }

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

 在http中加入

upstream  ngixDemo{
    server 192.168.10.11:8080;
    server 192.168.10.12:9090;
    }

在location中加入

 proxy_pass http://ngixDemo;  

 然后重启nginx服务

在url中输入http://ngixDemo便可以随机切换的访问两台服务器上的项目,但是这样以来会出现一个问题,由于每次访问的时候两台机器的sessionID不同,大多数程序猿在做用户登录的时候都是通过cookie中的sessionID判断用户是否已经登录,这样以来,在访问另一台机器上的服务的时候sessionID变了就要重新登录。如何解决这个问题呢?

①可以让每次访问的时候只能访问一台机器上的服务,不让来回跳转于两台服务之间,nginx有一个算法叫做轮询算法,就可以成功实现;在http中加入ip_hash;

upstream  ngixDemo{
    server 192.168.10.11:8080;
server 192.168.10.12:9090;
ip_hash;
    }

那么如果访问的这台机器挂了呢?那么就会自动分配到另一台机器,这个切换回有时间延迟,如果在这个延迟期间访问了服务的话就会出现服务中断的情况发生。解决这个问题可以使用redis解决。

②解决服务中断的问题,可以通过session共享的方法实现,如何实现呢?

将每个tomcat的server.xml配置文件中的这句话去掉注释

<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>

 然后在项目的web.xml中加入标签

<distributable/>

 如果此时如果访问的A服务中的项目,那么在访问B服务上的项目的时候会自动将A服务中的sessionID共享到B服务上。此方法不推荐使用,因为共享session的话,会占用网络请求,tomcat并发请求是一定的,这样性能会降低。

 

 

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值