Ingress-nginx: Annotation server-snippet not working

Created on 31 Aug 2018  路  4Comments  路  Source: kubernetes/ingress-nginx

Is this a request for help? (If yes, you should use our troubleshooting guide and community support channels, see https://kubernetes.io/docs/tasks/debug-application-cluster/troubleshooting/.):

I dont have idea

What keywords did you search in NGINX Ingress controller issues before filing this one? (If you have found any duplicates, you should instead reply there.):

annotation, configuration snippet

Is this a BUG REPORT or FEATURE REQUEST? (choose one):

NGINX Ingress controller version:
nginx-ingress-controller:0.18.0

Kubernetes version (use kubectl version):
Client Version: version.Info{Major:"1", Minor:"11", GitVersion:"v1.11.1", GitCommit:"b1b29978270dc22fecc592ac55d903350454310a", GitTreeState:"clean", BuildDate:"2018-07-18T11:37:06Z", GoVersion:"go1.10.3", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.8", GitCommit:"c138b85178156011dc934c2c9f4837476876fb07", GitTreeState:"clean", BuildDate:"2018-05-21T18:53:18Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"linux/amd64"}

Environment:

  • Cloud provider or hardware configuration: AWS
  • OS (e.g. from /etc/os-release): Debian GNU/Linux 8
  • Kernel (e.g. uname -a): 4.4.121-k8s
  • Install tools: kops
  • Others:

What happened:

I already add annotation configuration snippet in my ingress like

    nginx.ingress.kubernetes.io/server-snippet: |
      if ($http_user_agent ~* "(iPhone|iPod|Opera Mini|Android.*Mobile|NetFront|PSP|BlackBerry|Windows Phone)"){
       set $agentflag 1;
      }

      if ( $agentflag != 1 ) {
       return 301 https://example.com
      }

but I can still access websites from the desktop, and do not redirect to https://example.com.

What you expected to happen:

This website only can access from mobile, If we use the desktop to access the website, will be redirected to https://example.com

How to reproduce it (as minimally and precisely as possible):

Anything else we need to know:
https://github.com/kubernetes/ingress-nginx/blob/master/docs/user-guide/nginx-configuration/annotations.md#server-snippet

Most helpful comment

Seems like a few things are syntactically incorrect:

You are missing the apiVersion, ServiceName is misspelled, make sure the dev namespace exists. Also the spacing for the annotation should be different.

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: website
  namespace: default
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
    nginx.ingress.kubernetes.io/server-snippet: |
      if ($http_user_agent ~* "(Mobile)" ){
        set $agentflag 1;
      } 

      if ( $agentflag != 1 ) {
        return 301 https://example.com;
      }
spec:
  rules:
  - host: "abcd.com"
    http:
      paths:
      - path:
        backend:
          serviceName: website
          servicePort: 8000

works for me.

All 4 comments

Hey @invaleed can you post a sample of the nginx config and the logs?

Logs:
$ kubectl logs -n

Nginx Conf
$ kubectl exec -it -n cat /etc/nginx/nginx.conf

I cant edit ingress resource online with kubectl edit ingress "name_of_ingress" with error:

_# * : Invalid value: "The edited file failed validation": [yaml: line 11: could not find expected ':', [invalid character 'a' looking for beginning of value, invalid character 'a' looking for beginning of value]]_

If i used yml file to create ingress resource, i got error :

_error: error parsing ingress.yml: error converting YAML to JSON: yaml: line 11: could not find expected ':'_

This is my yml file

```apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: website
namespace: dev
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
nginx.ingress.kubernetes.io/server-snippet: |
if ($http_user_agent ~* "(Mobile)" ){
set $agentflag 1;
}

if ( $agentflag != 1 ) {
  return 301 https://example.com;
}

spec:
rules:

  • host: "abcd.com"
    http:
    paths:

    • path:

      backend:

      serviceNam: website

      servicePort: 8000

Seems like a few things are syntactically incorrect:

You are missing the apiVersion, ServiceName is misspelled, make sure the dev namespace exists. Also the spacing for the annotation should be different.

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: website
  namespace: default
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
    nginx.ingress.kubernetes.io/server-snippet: |
      if ($http_user_agent ~* "(Mobile)" ){
        set $agentflag 1;
      } 

      if ( $agentflag != 1 ) {
        return 301 https://example.com;
      }
spec:
  rules:
  - host: "abcd.com"
    http:
      paths:
      - path:
        backend:
          serviceName: website
          servicePort: 8000

works for me.

Ok got it..

My problem is no space before "if"

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: website
  namespace: default
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
    nginx.ingress.kubernetes.io/server-snippet: |
    if ($http_user_agent ~* "(Mobile)" ){
      set $agentflag 1;
    } 

    if ( $agentflag != 1 ) {
      return 301 https://example.com;
    }
spec:
  rules:
  - host: "abcd.com"
    http:
      paths:
      - path:
        backend:
          serviceName: website
          servicePort: 8000

The .yml file should be like yours, its works for me too...

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: website
  namespace: default
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
    nginx.ingress.kubernetes.io/server-snippet: |
      if ($http_user_agent ~* "(Mobile)" ){
        set $agentflag 1;
      } 

      if ( $agentflag != 1 ) {
        return 301 https://example.com;
      }
spec:
  rules:
  - host: "abcd.com"
    http:
      paths:
      - path:
        backend:
          serviceName: website
          servicePort: 8000

SOLVED, sorry my bad...

Was this page helpful?
0 / 5 - 0 ratings