【问题标题】:NGINX location directive in stream流中的 NGINX 位置指令
【发布时间】:2020-03-26 19:13:36
【问题描述】:

我在我的一台服务器上安装了 Nginx,以便用作我的 Rancher 应用程序的负载平衡器。 我的配置基于此处找到的配置:https://rancher.com/docs/rancher/v2.x/en/installation/ha/create-nodes-lb/nginx/

所以我的配置是:

load_module /usr/lib/nginx/modules/ngx_stream_module.so;

worker_processes 4;
worker_rlimit_nofile 40000;

events {
    worker_connections 8192;
}

stream {
    upstream rancher_servers_http {
        least_conn;
        server <ipnode1>:80 max_fails=3 fail_timeout=5s;
        server <ipnode2>:80 max_fails=3 fail_timeout=5s;
        server <ipnode3>:80 max_fails=3 fail_timeout=5s;
    }
    server {
        listen     80;

        proxy_pass rancher_servers_http;
    }

    upstream rancher_servers_https {
        least_conn;
        server <ipnode1>:443 max_fails=3 fail_timeout=5s;
        server <ipnode2>:443 max_fails=3 fail_timeout=5s;
        server <ipnode3>:443 max_fails=3 fail_timeout=5s;
    }
    server {
        listen     443;
        proxy_pass rancher_servers_https;
    }
}

我的配置按预期工作,但我最近在我的集群上安装了Nextcloud。这给了我以下错误:

  • 您的 Web 服务器未正确设置以解析“/.well-known/caldav”。更多信息可以在 文档。

  • 您的 Web 服务器未正确设置以解析“/.well-known/carddav”。更多信息可以在 文档。

所以我想添加一个“位置”指令,但我做不到。 我尝试如下更新我的配置:

...

stream {
    upstream rancher_servers_http {
        ...
    }
    server {
        listen     80;
        proxy_pass rancher_servers_http;

        location /.well-known/carddav {
            return 301 $scheme://$host:$server_port/remote.php/dav;
        }
        location /.well-known/caldav {
            return 301 $scheme://$host:$server_port/remote.php/dav;
        }
    }

    upstream rancher_servers_https {
        ...
    }
    server {
        listen     443;
        proxy_pass rancher_servers_https;

        location /.well-known/carddav {
            return 301 $scheme://$host:$server_port/remote.php/dav;
        }
        location /.well-known/caldav {
            return 301 $scheme://$host:$server_port/remote.php/dav;
        }
    }
}

但它告诉我

/etc/nginx/nginx.conf:21 中不允许使用“location”指令

假设在流配置中不允许使用 location 指令,我尝试添加这样的 http 块:

...

stream {
    ...
}

http {
  server {
      listen 443;

      location /.well-known/carddav {
        return 301 $scheme://$host:$server_port/remote.php/dav;
      }
      location /.well-known/caldav {
        return 301 $scheme://$host:$server_port/remote.php/dav;
      }
   }
  server {
      listen 80;

      location /.well-known/carddav {
        return 301 $scheme://$host:$server_port/remote.php/dav;
      }
      location /.well-known/caldav {
        return 301 $scheme://$host:$server_port/remote.php/dav;
      }
   }
}

但后来我收到了这条消息:

bind() 到 0.0.0.0:443 失败(98:地址已在使用中)

(端口 80 相同)。

有人可以帮我解决这个问题吗?如何在不影响实际配置的情况下添加 location 指令?

感谢您的阅读。

编辑

看来stream 指令阻止我添加其他标准指令。我试图在server 中添加client_max_body_size,但我遇到了同样的问题:

此处不允许使用指令

【问题讨论】:

    标签: nginx


    【解决方案1】:

    现在您的设置使用 nginx 作为 TCP 代理。 nginx 的这种配置无需分析即可通过流量 - 它可以是 ssh、rdp 等任何流量,并且无论协议如何,它都可以正常工作,因为 nginx 不会尝试检查流内容。

    这就是为什么 location 指令在流的上下文中不起作用的原因 - 它是与 http 协议相关的功能。

    要利用高级协议分析,nginx 需要了解通过它的协议,即配置为 HTTP 反向代理。

    为了让它工作,服务器指令应该放在 http 范围而不是流范围中。

    http {
      server {
        listen 0.0.0.0:443 ssl;
        include /etc/nginx/snippets/letsencrypt.conf;
        root /var/www/html;
        server_name XXXX;
    
        location / {
            proxy_pass http://rancher_servers_http;
        }
        location /.well-known/carddav {
          proxy_pass http://$host:$server_port/remote.php/dav;
        }
        location /.well-known/caldav {
          proxy_pass http://$host:$server_port/remote.php/dav;
        }
      }
    
      server {
        listen 80 default_server;
        listen [::]:80 default_server;
    
        location ^~ /.well-known/acme-challenge/ {
            default_type "text/plain";
            root /var/www/letsencrypt;
        }
        root /var/www/html;
        server_name xxxx;
    
        location / {
            proxy_pass http://rancher_servers_http;
        }
      }
    }
    

    这种方法的缺点是您需要重新配置证书管理。 但是您将加载 ssl 加密到 nginx 并基于 http 查询获得智能平衡。

    【讨论】:

      猜你喜欢
      • 2012-11-09
      • 1970-01-01
      • 1970-01-01
      • 2012-04-22
      • 1970-01-01
      • 1970-01-01
      • 2014-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多