kubernetes / ingress-nginx

Ingress-NGINX Controller for Kubernetes
https://kubernetes.github.io/ingress-nginx/
Apache License 2.0
17.28k stars 8.21k forks source link

Getting unknown directive "root" when trying to add configuration to serve content from specific directory #11086

Closed Velan987 closed 6 months ago

Velan987 commented 6 months ago

What happened:

I am trying to configure nginx to provide content from specific directory, i.e If i invoke root path (/) I need to show index.html. while configuring server-snippet for this I am getting below error.

Below is my ingress yaml file

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
  annotations:
    nginx.ingress.kubernetes.io/server-snippet: |
      location =/{
        alias /usr/share/nginx/html;
        try_files $uri $uri/ /index.html;
      }
spec:
  ingressClassName: nginx
  rules:
  - host: test.com 
    http:
      paths:
      - path: /test
        pathType: Prefix
        backend:
          service:
            name: test-svc
            port:
              number: 80

2024/03/11 06:56:57 [emerg] 955#955: unknown directive "root" in /tmp/nginx/nginx-cfg3003441674:276 nginx: [emerg] unknown directive "root" in /tmp/nginx/nginx-cfg3003441674:276 nginx: configuration file /tmp/nginx/nginx-cfg3003441674 test failed

What you expected to happen:

I am configuring below code in ingress resource:

annotations:
    nginx.ingress.kubernetes.io/server-snippet: |
      location =/{
        alias /usr/share/nginx/html;
        try_files $uri $uri/ /index.html;
      }

I have placed landing page files in /usr/share/nginx/html. When I hit root path I need to show index.html.

NGINX Ingress controller version (exec into the pod and run nginx-ingress-controller --version.):

Kubernetes version (use kubectl version): v1.29.1

Environment:

NAME                                        READY   STATUS      RESTARTS   AGE
ingress-nginx-admission-create-gh86m        0/1     Completed   0          2d14h
ingress-nginx-admission-patch-2zwsg         0/1     Completed   0          2d14h
ingress-nginx-controller-847d74c887-dc4m7   1/1     Running     0          2d14h

I am configuring below code in ingress resource:

annotations:
    nginx.ingress.kubernetes.io/server-snippet: |
      location =/{
        alias /usr/share/nginx/html;
        try_files $uri $uri/ /index.html;
      }

I have placed landing page files in /usr/share/nginx/html. When I hit root path I need to show index.html.

How to reproduce this issue:

Anything else we need to know:

k8s-ci-robot commented 6 months ago

This issue is currently awaiting triage.

If Ingress contributors determines this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.
longwuyuan commented 6 months ago

/remove-kind bug

I don't understand anything about the environment or the use case or practical steps to reproduce the problem. There is no clear definition of the problem. Wait till someone can understand and triage the issue, so the problem description is more clear

/kind support /triage needs-information

Velan987 commented 6 months ago

I am trying to configure nginx to provide content from specific directory, i.e If i invoke root path (/) I need to show index.html. while configuring server-snippet for this I am getting below error.

Below is my ingress yaml file

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
  annotations:
    nginx.ingress.kubernetes.io/server-snippet: |
      location =/{
        root /usr/share/nginx/html;
        try_files $uri $uri/ /index.html;
      }
spec:
  ingressClassName: nginx
  rules:
  - host: test.com 
    http:
      paths:
      - path: /test
        pathType: Prefix
        backend:
          service:
            name: test-svc
            port:
              number: 80

For this ingress I am getting below error Unknown directive "root" in /tmp/nginx/nginx-cfg3003441674:276 nginx: [emerg] unknown directive "root" in /tmp/nginx/nginx-cfg3003441674:276 nginx: configuration file /tmp/nginx/nginx-cfg3003441674 test failed.

I also went into nginx ingress controller pod, and added below snippet

 location =/{
        root /usr/share/nginx/html;
        try_files $uri $uri/ /index.html;
      }

Getting same error (Unknown directive "root") while reloadin nginx using "nginx -s reload"

longwuyuan commented 6 months ago

At the very least, you should look at the questions that are asked in the new bug report template.

You have not answered any of the questions asked in the new issue template so readers have to guess what the heck is going on here, to begin with.

And then you don't care to explain why you are talking about providing content from inside the controller. The controller has nothing to do providing your content from inside the controller pod

Once you have edited the issue description here and answered all the questions that are asked in a new bug report template, please write a detailed description of why you are trying to provide content from inside the ingress-controller pod. This project is a ingress controller so look at the documentation and write proper issue description that is related to some feature mentioned in the documentation https://kubernetes.github.io/ingress-nginx/

After that you can re-open the issue so that readers can make comments based on useful data posted here.

/close

k8s-ci-robot commented 6 months ago

@longwuyuan: Closing this issue.

In response to [this](https://github.com/kubernetes/ingress-nginx/issues/11086#issuecomment-1990988816): >At the very least, you should look at the questions that are asked in the new bug report template. > >You have not answered any of the questions asked in the new issue template so readers have to guess what the heck is going on here, to begin with. > >And then you don't care to explain why you are talking about providing content from inside the controller. The controller has nothing to do providing your content from inside the controller pod > >Once you have edited the issue description here and answered all the questions that are asked in a new bug report template, please write a detailed description of why you are trying to provide content from inside the ingress-controller pod. This project is a ingress controller so look at the documentation and write proper issue description that is related to some feature mentioned in the documentation https://kubernetes.github.io/ingress-nginx/ > >After that you can re-open the issue so that readers can make comments based on useful data posted here. > >/close Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.
zengyuxing007 commented 2 months ago

please look at this : https://github.com/kubernetes/ingress-nginx/pull/8624