配置负载均衡器后Nginx无法正常工作

时间:2019-03-26 11:33:48

标签: tomcat nginx load-balancing

我一直在尝试使用nginx为两个tomcat服务器配置负载均衡器。首先,我在不同网络端口(8080和8081)上的两个不同的tomcat服务器上启动了项目,并且该项目可以正常工作。 然后,我在Windows 10上下载了nginx,启动了它,并在本地主机上获得了“欢迎使用nginx页面”:80。

然后我在nginx conf文件上添加了一些修改,以配置负载均衡器

 upstream new_servers {
            least_conn;
            server 127.0.0.1:8081;
            server 127.0.0.1:8080;
    }

和位置

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

我尝试执行“ nginx -s reload”,我停止了nginx并重新启动它,但似乎没有任何效果,我总是得到

  

400 http错误

这是所有conf文件

#user  nobody;
worker_processes  1;

 error_log  logs/error.log debug;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
    debug_connection 127.0.0.1;
}


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 new_servers {
            least_conn;
            server 127.0.0.1:8081;
            server 127.0.0.1:8080;
    }

    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://new_servers;
        }

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

}

,日志上什么也没有显示。

我的问题是: 是什么造成了此错误?

一旦nginx可以工作?会将我重定向到其中一台服务器吗?

我已经在这个问题上工作了两天,所以我非常感谢您的帮助。

提前谢谢

1 个答案:

答案 0 :(得分:0)

更改nginx默认端口即可解决此问题。

要回答问题,是的,nginx确实将我重定向到其中一台服务器。

如果您想查看我们重定向到的服务器,只需添加

log_format main 'to: $upstream_addr : $remote_addr - $remote_user [$time_local] "$request" '
            '$status $body_bytes_sent "$http_referer" '
            '"$http_user_agent" "$http_x_forwarded_for"';

然后转到acces文件,查看我们重定向到的服务器“ 到“