如何缩小多个主机的 nginx.conf

如何缩小多个主机的 nginx.conf

我在蓝/绿部署方案中继承了一个旧的 nginx-controller,其中包含 100 多台服务器。目前,单个 nginx.conf 文件超过 10k 行。查看每个服务器部分,除了“server_name”、“service_name”和“proxy_upstream_name”部分外,它们似乎都相同。其余部分都相同。我尝试将整个配置应用到 ConfigMap,但它太大了。有没有办法缩小配置,而不是重复相同的配置 100 多次?如果没有,我该如何将这个超大配置放入新的入口控制器中?

这只是 100 多个服务器部分中的一个。

    server {
            server_name wwi-dev-devops-blue.domain.com ;

            listen 80  ;
            listen 443  ssl http2 ;

            set $proxy_upstream_name "-";

            ssl_certificate_by_lua_block {
                    certificate.call()
            }

            location ~* "^/*" {

                    set $namespace      "default";
                    set $ingress_name   "haproxy";
                    set $service_name   "haproxy-blue";
                    set $service_port   "80";
                    set $location_path  "/*";

                    rewrite_by_lua_block {
                            lua_ingress.rewrite({
                                    force_ssl_redirect = true,
                                    ssl_redirect = true,
                                    force_no_ssl_redirect = false,
                                    use_port_in_redirects = false,
                            })
                            balancer.rewrite()
                            plugins.run()
                    }

                    # be careful with `access_by_lua_block` and `satisfy any` directives as satisfy any
                    # will always succeed when there's `access_by_lua_block` that does not have any lua code doing `ngx.exit(ngx.DECLINED)`
                    # other authentication method such as basic auth or external auth useless - all requests will be allowed.
                    #access_by_lua_block {
                    #}

                    header_filter_by_lua_block {
                            lua_ingress.header()
                            plugins.run()
                    }

                    body_filter_by_lua_block {
                    }

                    log_by_lua_block {
                            balancer.log()

                            monitor.call()

                            plugins.run()
                    }

                    port_in_redirect off;

                    set $balancer_ewma_score -1;
                    set $proxy_upstream_name "default-haproxy-blue-80";
                    set $proxy_host          $proxy_upstream_name;
                    set $pass_access_scheme  $scheme;

                    set $pass_server_port    $server_port;

                    set $best_http_host      $http_host;
                    set $pass_port           $pass_server_port;

                    set $proxy_alternative_upstream_name "";

                    client_max_body_size                    50m;

                    proxy_set_header Host                   $best_http_host;

                    # Pass the extracted client certificate to the backend

                    # Allow websocket connections
                    proxy_set_header                        Upgrade           $http_upgrade;

                    proxy_set_header                        Connection        $connection_upgrade;

                    proxy_set_header X-Request-ID           $req_id;
                    proxy_set_header X-Real-IP              $remote_addr;

                    proxy_set_header X-Forwarded-For        $remote_addr;

                    proxy_set_header X-Forwarded-Host       $best_http_host;
                    proxy_set_header X-Forwarded-Port       $pass_port;
                    proxy_set_header X-Forwarded-Proto      $pass_access_scheme;

                    proxy_set_header X-Scheme               $pass_access_scheme;

                    # Pass the original X-Forwarded-For
                    proxy_set_header X-Original-Forwarded-For $http_x_forwarded_for;

                    # mitigate HTTPoxy Vulnerability
                    # https://www.nginx.com/blog/mitigating-the-httpoxy-vulnerability-with-nginx/
                    proxy_set_header Proxy                  "";

                    # Custom headers to proxied server

                    proxy_connect_timeout                   600s;
                    proxy_send_timeout                      600s;
                    proxy_read_timeout                      600s;

                    proxy_buffering                         off;
                    proxy_buffer_size                       4k;
                    proxy_buffers                           4 4k;

                    proxy_max_temp_file_size                1024m;

                    proxy_request_buffering                 on;
                    proxy_http_version                      1.1;

                    proxy_cookie_domain                     off;
                    proxy_cookie_path                       off;

                    # In case of errors try the next upstream server before returning an error
                    proxy_next_upstream                     error timeout;
                    proxy_next_upstream_timeout             0;
                    proxy_next_upstream_tries               3;

                    proxy_pass http://upstream_balancer;

                    proxy_redirect                          off;

            }

            location ~* "^/" {

                    set $namespace      "";
                    set $ingress_name   "";
                    set $service_name   "";
                    set $service_port   "";
                    set $location_path  "/";

                    rewrite_by_lua_block {
                            lua_ingress.rewrite({
                                    force_ssl_redirect = true,
                                    ssl_redirect = true,
                                    force_no_ssl_redirect = false,
                                    use_port_in_redirects = false,
                            })
                            balancer.rewrite()
                            plugins.run()
                    }

                    # be careful with `access_by_lua_block` and `satisfy any` directives as satisfy any
                    # will always succeed when there's `access_by_lua_block` that does not have any lua code doing `ngx.exit(ngx.DECLINED)`
                    # other authentication method such as basic auth or external auth useless - all requests will be allowed.
                    #access_by_lua_block {
                    #}

                    header_filter_by_lua_block {
                            lua_ingress.header()
                            plugins.run()
                    }

                    body_filter_by_lua_block {
                    }

                    log_by_lua_block {
                            balancer.log()

                            monitor.call()

                            plugins.run()
                    }

                    port_in_redirect off;

                    set $balancer_ewma_score -1;
                    set $proxy_upstream_name "upstream-default-backend";
                    set $proxy_host          $proxy_upstream_name;
                    set $pass_access_scheme  $scheme;

                    set $pass_server_port    $server_port;

                    set $best_http_host      $http_host;
                    set $pass_port           $pass_server_port;

                    set $proxy_alternative_upstream_name "";

                    client_max_body_size                    50m;

                    proxy_set_header Host                   $best_http_host;

                    # Pass the extracted client certificate to the backend

                    # Allow websocket connections
                    proxy_set_header                        Upgrade           $http_upgrade;

                    proxy_set_header                        Connection        $connection_upgrade;

                    proxy_set_header X-Request-ID           $req_id;
                    proxy_set_header X-Real-IP              $remote_addr;

                    proxy_set_header X-Forwarded-For        $remote_addr;

                    proxy_set_header X-Forwarded-Host       $best_http_host;
                    proxy_set_header X-Forwarded-Port       $pass_port;
                    proxy_set_header X-Forwarded-Proto      $pass_access_scheme;

                    proxy_set_header X-Scheme               $pass_access_scheme;

                    # Pass the original X-Forwarded-For
                    proxy_set_header X-Original-Forwarded-For $http_x_forwarded_for;

                    # mitigate HTTPoxy Vulnerability
                    # https://www.nginx.com/blog/mitigating-the-httpoxy-vulnerability-with-nginx/
                    proxy_set_header Proxy                  "";

                    # Custom headers to proxied server

                    proxy_connect_timeout                   600s;
                    proxy_send_timeout                      600s;
                    proxy_read_timeout                      600s;

                    proxy_buffering                         off;
                    proxy_buffer_size                       4k;
                    proxy_buffers                           4 4k;

                    proxy_max_temp_file_size                1024m;

                    proxy_request_buffering                 on;
                    proxy_http_version                      1.1;

                    proxy_cookie_domain                     off;
                    proxy_cookie_path                       off;

                    # In case of errors try the next upstream server before returning an error
                    proxy_next_upstream                     error timeout;
                    proxy_next_upstream_timeout             0;
                    proxy_next_upstream_tries               3;

                    proxy_pass http://upstream_balancer;

                    proxy_redirect                          off;

            }

    }

相关内容