配置负载均衡器后 Nginx 不工作

Nginx does not work after configuring a load balancer

我一直在尝试使用 nginx 为两个 tomcat 服务器配置负载平衡器。首先,我在不同网络端口(8080 和 8081)上的两个不同 tomcat 服务器上启动了我的项目,并且它工作正常。 然后,我在 windows 10 上下载了 nginx,启动了它,我在本地主机上得到了 "welcome to nginx page":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 error

这里是所有的配置文件

#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 会工作一次吗?它会将我重定向到其中一台服务器吗?

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

提前致谢

更改 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 文件以查看我们被重定向到哪个服务器" 到“