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

标签 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;
        }

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"';

然后转到访问文件以查看我们被重定向到哪个服务器“ 到“

关于tomcat - 配置负载均衡器后 Nginx 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55356147/

相关文章:

python - 如何使 environ ['PATH_INFO' ] 在我的 nginx + uwsgi 环境中可用?

Azure 网站和粘性负载平衡

java - 如何让 Tomcat 在启动时预编译 JSP?

java - 在 tomcat 中部署 GWT

java - 来自 servlet 的 Java 异步处理

amazon-web-services - 在 AWS EC2 负载均衡器上安装通配符证书

azure - 枚举 Azure 中云服务的所有实例

Tomcat 不会从 Grails 启动 .war 文件

nginx proxy_pass 404 错误,不明白为什么

node.js - Pm2、Nginx 或 Nodejs 不提供已部署路由的最新版本