Kubernetes-Ingress Server-Snippet 503 for mobile user agent

7/2/2019

I am trying to create a server-snippet, that will return a 503 for mobile user. I am doing that, by checking the user agent. The problem: Server-Snippet is not returning 503 in case of mobile user agent. All over, the 503 is returned when the user agent is NOT a mobile devide. Mobile itself, sends a 200. I cannot understand what is done by the Ingress. It seems, as the server-snipper code is somehow parsing it "in a hard-coded way". Maybe someone got similar issues. If someone might give a hint on such server-snippers, it would be nice. Thanks

I tried several server-snippets. Please check code below.

  1. Try This is a official code snipper from github.

nginx.ingress.kubernetes.io/server-snippet: set $agentflag 0;

if ($http_user_agent ~* "(Mobile|ios|android)" ){ set $agentflag 1; }

if ( $agentflag = 1 ) { return 503; }

As json:

"nginx.ingress.kubernetes.io/server-snippet": "| set $agentflag 0; if ($http_user_agent ~* \"(Mobile|ios|android)\" ) { set $agentflag 1; } if ( $agentflag = 1 ) { return 503;}"

  1. Try

nginx.ingress.kubernetes.io/server-snippet: if ($http_user_agent ~* "(Mobile|ios|android)" ){ return 503; }

As json:

  "nginx.ingress.kubernetes.io/server-snippet": "    if ($http_user_agent ~* (Mobile|ios|android) ) {\n      return 503; }"
-- Olli
kubernetes
kubernetes-ingress

2 Answers

7/2/2019

Seems like a few things are syntactically incorrect. space before the if condition.

Refer this snippet

kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
nginx.ingress.kubernetes.io/server-snippet: |
  set $agentflag 0;

  if ($http_user_agent ~* "(Mobile)" ){
    set $agentflag 1;
  } 

  if ( $agentflag = 1 ) {
    return 503;
  }
-- Harsh Manvar
Source: StackOverflow

7/2/2019

A short working solution is

   annotations: 
      kubernetes.io/ingress.class: private-nginx 
      kubernetes.io/tls-acme: "true" 
      nginx.ingress.kubernetes.io/server-snippet: | 
      if ($http_user_agent ~* "(Mobile)" ) { 
        return 503; 
      }
-- Olli
Source: StackOverflow