Score:0

how to shrink nginx.conf with multiple hosts

cl flag

I have inherited an old nginx-controller with over 100 servers in a blue/green deployment scenario. Right now the single nginx.conf file is over 10k lines. Looking at each server section for each they all appear to be the same for each with the exeception of of the "server_name", "service_name" and "proxy_upstream_name" sections. The rest is all the same for each. I tried to apply the entire config a a ConfigMap, but it is too big. Is there a way to shrink the config instead of duplicating the same config over 100 times? If not how can I can get this extra large config into the new ingress controller?

This is just one of over 100 server sections.

    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;

            }

    }
in flag
Hi zerobit welcome to S.F. What does "I don't have access to it" mean in this context? Your question is a lot of words, and not a lot of code or error messages. Please don't use the comments, instead [edit your question](https://serverfault.com/posts/1117702/edit) and include more specifics. Good luck!
pt flag
The correct way to build a custom image is to use a `Dockerfile` to generate the new image, using the existing upstream image as the base (and then push your new image to an image repository and update your nginx deployments to reference the new image).
pt flag
Another solution would be to use an [initContainer](https://kubernetes.io/docs/concepts/workloads/pods/init-containers/) to populate a shared volume with the configuration, and then mount that in the appropriate place on your nginx container.
in flag
Based on your newly posted snippet, it sure does look like you're trying to hand-roll your own Ingress controller, which is a fine reason it's not working for you. You also seem to be mixing nginx and haproxy, is that on purpose?
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.