nginx负载均衡重定向404

时间:2014-12-06 09:45:40

标签: php apache ubuntu nginx load-balancing

我有3台服务器。第一台服务器将nginx作为负载均衡。第二和第三台服务器都有apache。在我的nginx conf中,我有以下内容:

upstream backend  {
  server ec2-54-148-248-231.us-west-2.compute.amazonaws.com;
  server ec2-54-148-34-166.us-west-2.compute.amazonaws.com;
}

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /usr/share/nginx/html;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name localhost;

        location / {
                proxy_pass  http://backend;
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }
}

问题是:当我打开第一台服务器时,它会将我重定向到第二台或第三台,但我无法在我的网站内加载任何相对URL(例如:css或js文件),并且所有链接都返回404错误。

你可以尝试我的负载balaning服务器在线url => http://ec2-54-148-96-241.us-west-2.compute.amazonaws.com/

1 个答案:

答案 0 :(得分:0)

我发现nginx具有循环或最少连接负载平衡的问题,每个后续客户端的请求都可能被分发到不同的服务器。在第一次请求ex =>之后在我的网站上http://ec2-54-148-96-241.us-west-2.compute.amazonaws.com/site/index 然后将请求http://ec2-54-148-96-241.us-west-2.compute.amazonaws.com/assets/f352c4d6/css/bootstrap.css发送到另一台服务器,但是在另一台服务器上找不到资产f352c4d6,所以返回404。

溶液: 你必须使用ip_hash;使用nginx负载平衡来保持一个ip的相同服务器 您也可以使用leas_connect将用户分配给最少的服务器流量。

相关问题