侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

nginx的反向代理和负载均衡配置方法

2023-12-09 星期六 / 0 评论 / 0 点赞 / 101 阅读 / 4591 字

由于需要测试分布式会话是否生效,于是我们又要涉及到nginx的负载均衡了,通过部署多个实例由nginx来进行轮询调度,从而测试session是否实现共享,这是测试办法 #user nobody;wo

由于需要测试分布式会话是否生效,于是我们又要涉及到nginx的负载均衡了,通过部署多个实例由nginx来进行轮询调度,从而测试session是否实现共享,这是测试办法

#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;    #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;   #在这里 我们将两个实例部署好,分别部署在8080 8090两个端口的tomcat中,upstrem是负载均衡的关键,默认是轮询upstream  sessiontest{    #ip_hash;配置此项则不使用轮询策略,通过用户的ip计算出哈希,从而匹配固定的运行实例    server 127.0.0.1:8080;    server 127.0.0.1:8090;}    server {        #我们让nginx开启80端口的监听,默认就开启了        listen       80;        server_name  localhost;//我们的请求域名        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {           root   html;#root是指nginx下的文件夹根目录           #index:默认访问页面,因此这也是我们直接访问localhost就直接看到欢迎页面的原因,           #实际上是转发给/html/index.html            index  index.html index.htm;       }               #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;        }         #这里我们对我们部署项目的配置,这里是反向代理接受/RedisDemo打头的请求转发给proxy_pass中配置的服务器         #这里选择负载均衡的配置,如果不需要负载均衡,可直接填写代理的地址即可达到效果        location /RedisDemo {                      #index  index.html index.htm;           #这里的sessiontest对应上面配置的upstream,还记得吗;注意http://也是要的            proxy_pass http://sessiontest;        }        # 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;    #    }    #}}

配置完这些,启动nginx

我们的8080请求地址为:http://localhost:8080/RedisDemo/getUser?id=1

我们的8090请求地址为:http://localhost:8090/RedisDemo/getUser?id=1

然后我们访问nginx ,则访问 :http://localhost/RedisDemo/getUser?id=1,他会轮询访问上面两个

我们可以看到每次请求都会在8080 8090中轮询访问,sessionid是一样的,nice 我们要的效果就达到了

 

 

 

广告 广告

评论区