流中的 NGINX 位置指令

MHo*_*gge 6 nginx

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

\n\n

所以我的配置是:

\n\n
load_module /usr/lib/nginx/modules/ngx_stream_module.so;\n\nworker_processes 4;\nworker_rlimit_nofile 40000;\n\nevents {\n    worker_connections 8192;\n}\n\nstream {\n    upstream rancher_servers_http {\n        least_conn;\n        server <ipnode1>:80 max_fails=3 fail_timeout=5s;\n        server <ipnode2>:80 max_fails=3 fail_timeout=5s;\n        server <ipnode3>:80 max_fails=3 fail_timeout=5s;\n    }\n    server {\n        listen     80;\n\n        proxy_pass rancher_servers_http;\n    }\n\n    upstream rancher_servers_https {\n        least_conn;\n        server <ipnode1>:443 max_fails=3 fail_timeout=5s;\n        server <ipnode2>:443 max_fails=3 fail_timeout=5s;\n        server <ipnode3>:443 max_fails=3 fail_timeout=5s;\n    }\n    server {\n        listen     443;\n        proxy_pass rancher_servers_https;\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
\n
    \n
  • 您的 Web 服务器未正确设置以解析 \xe2\x80\x9c/.well-known/caldav\xe2\x80\x9d。更多信息可以在文档中找到。\n

  • \n
  • 您的 Web 服务器未正确设置以解析 \xe2\x80\x9c/.well-known/carddav\xe2\x80\x9d。更多信息可以在文档中找到。\n

  • \n
\n
\n\n

所以我想添加一个“位置”指令,但我无法做到这一点。\n我尝试按如下方式更新我的配置:

\n\n
...\n\nstream {\n    upstream rancher_servers_http {\n        ...\n    }\n    server {\n        listen     80;\n        proxy_pass rancher_servers_http;\n\n        location /.well-known/carddav {\n            return 301 $scheme://$host:$server_port/remote.php/dav;\n        }\n        location /.well-known/caldav {\n            return 301 $scheme://$host:$server_port/remote.php/dav;\n        }\n    }\n\n    upstream rancher_servers_https {\n        ...\n    }\n    server {\n        listen     443;\n        proxy_pass rancher_servers_https;\n\n        location /.well-known/carddav {\n            return 301 $scheme://$host:$server_port/remote.php/dav;\n        }\n        location /.well-known/caldav {\n            return 301 $scheme://$host:$server_port/remote.php/dav;\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

但它告诉我

\n\n
\n

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

\n
\n\n

假设流配置中不允许使用 location 指令,我尝试添加一个 http 块,如下所示:

\n\n
...\n\nstream {\n    ...\n}\n\nhttp {\n  server {\n      listen 443;\n\n      location /.well-known/carddav {\n        return 301 $scheme://$host:$server_port/remote.php/dav;\n      }\n      location /.well-known/caldav {\n        return 301 $scheme://$host:$server_port/remote.php/dav;\n      }\n   }\n  server {\n      listen 80;\n\n      location /.well-known/carddav {\n        return 301 $scheme://$host:$server_port/remote.php/dav;\n      }\n      location /.well-known/caldav {\n        return 301 $scheme://$host:$server_port/remote.php/dav;\n      }\n   }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

但后来我收到了这样的消息:

\n\n
\n

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

\n
\n\n

(与端口 80 相同)。

\n\n

有人可以帮我弄这个吗 ?如何添加位置指令而不影响我的实际配置?

\n\n

感谢您的阅读。

\n\n

编辑

\n\n

看来该stream指令阻止我添加其他标准指令。我尝试添加client_max_body_size内部server但我遇到了同样的问题:

\n\n
\n

此处不允许使用指令

\n
\n

Max*_*hny 7

现在您的设置使用 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;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

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