NGINX配置没什么问题了,无法使用upstream负载均衡 - Go语言中文社区

NGINX配置没什么问题了,无法使用upstream负载均衡


这篇文章的前提是已经配置好了NGINX,而且tomcat已经配置好了,而且能能够访问了。

然后使用nginx反向代理

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://ip:18080/;
            index  index.html index.htm;
        }
        location /my{
            proxy_pass http://ip:28080/;

        }

如上配置时没有问题的访问(http://ip:80),你可以通过nginx访问两个的tomcat,但是如果你想访问(http://ip:80)根据负载均衡跳转,则要使用upstream,我卡在这里很久了,因为我一配置upstream就404的错,而且配置也没有错

 

经过多次百度,一般都是这样配的,不过我在本地这样配置使用也是可以用的,但是在linux上就不行了,没改配置文件能访问nginx首页,以及直接访问一个tomcat是可以的,但是就是不能负载均衡,但是我就是跳转不到tomcat,后来无意中增加了一个            proxy_set_header Host $host;
就能跳转了,虽然不知道原理,但是还是要记录一下这个问题,以便以后自己查看

    upstream tomcats{
        ip_hash;
        server ip:18080 weight=2 max_fails=3 fail_timeout=3;
        server ip:28080 weight=1 max_fails=3 fail_timeout=3;
    }
    
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://tomcats;
            proxy_set_header Host $host;
            index  index.html index.htm;
        }

 

最后就可以跳转了,实现了负载均衡,初次接触nginx,有问题多多指教,谢谢!

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/c1249629019/article/details/82287955
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-07 23:15:57
  • 阅读 ( 892 )
  • 分类:Go Web框架

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢