I have nginx ingress controller deployed using helm, a simple nginx pod, service and ingress:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: all-app-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
rules:
- host: example.com # I have my own domain name
http:
paths:
- path: /nginx-test
pathType: Prefix
backend:
service:
name: nginx-test
port:
number: 80
describe svc and ingress:
kubectl describe svc nginx-test
Name: nginx-test
Namespace: default
Labels: run=nginx-test
Annotations: cloud.google.com/neg: {"ingress":true}
Selector: run=nginx-test
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.46.128.187
IPs: 10.46.128.187
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 10.46.1.2:80
Session Affinity: None
Events: <none>
kubectl describe ingress all-app-ingress
Name: all-app-ingress
Labels: <none>
Namespace: default
Address: 34.122.42.46
Ingress Class: nginx
Default backend: <default>
Rules:
Host Path Backends
---- ---- --------
example.com
/nginx-test nginx-test:80 (10.46.1.2:80)
Annotations: nginx.ingress.kubernetes.io/rewrite-target: /nginx-test
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal AddedOrUpdated 13m (x29 over 2d1h) nginx-ingress-controller Configuration for default/all-app-ingress was added or updated
If I try to access the app http://example.com/nginx-test I get 404 Not Found
But if I use the root prefix path:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: all-app-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
rules:
- host: example.com # I have my own domain name
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-test
port:
number: 80
Now I can access the the Welcome to nginx! page. Hmmm.. Not sure why the root path works well here, but prefix subpath doesn't?
Get logs from nginx ingress controller when use /nginx-test path:
kubectl logs nginx-controller-01-nginx-ingress-6bb9c56f6d-dvm8c | tail
3.135.248.93 - - [01/Mar/2023:23:09:31 +0000] "GET /resources/.git/config HTTP/1.1" 404 153 "-" "python-requests/2.28.2" "-"
3.135.248.93 - - [01/Mar/2023:23:09:31 +0000] "GET /system/.git/config HTTP/1.1" 404 153 "-" "python-requests/2.28.2" "-"
3.135.248.93 - - [01/Mar/2023:23:09:31 +0000] "GET /src/.git/config HTTP/1.1" 404 153 "-" "python-requests/2.28.2" "-"
2023/03/01 23:18:39 [error] 209#209: *2800 "/etc/nginx/html/index.html" is not found (2: No such file or directory), client: 167.250.111.166, server: example.com, request: "GET / HTTP/1.1", host: "example.com"
167.250.111.166 - - [01/Mar/2023:23:18:39 +0000] "GET / HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36" "-"
76.37.41.134 - - [01/Mar/2023:23:18:47 +0000] "GET / HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36" "-"
2023/03/01 23:18:47 [error] 209#209: *2801 "/etc/nginx/html/index.html" is not found (2: No such file or directory), client: 76.37.41.134, server: example.com, request: "GET / HTTP/1.1", host: "example.com"
2023/03/01 23:19:04 [error] 209#209: *2802 "/etc/nginx/html/index.html" is not found (2: No such file or directory), client: 138.68.47.85, server: example.com, request: "GET / HTTP/1.1", host: "example.com"
138.68.47.85 - - [01/Mar/2023:23:19:04 +0000] "GET / HTTP/1.1" 404 555 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36" "127.0.0.1"
138.68.47.85 - - [01/Mar/2023:23:19:04 +0000] "GET / HTTP/1.1" 404 555 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36" "-"
Logs from nginx ingress controller when "/" root path used:
kubectl logs nginx-controller-01-nginx-ingress-6bb9c56f6d-dvm8c |
tail
73.202.54.6 - - [01/Mar/2023:23:37:47 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36" "-"
73.202.54.6 - - [01/Mar/2023:23:37:49 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36" "-"
73.202.54.6 - - [01/Mar/2023:23:37:50 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36" "-"
73.202.54.6 - - [01/Mar/2023:23:37:53 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36" "-"
P.S. domain masked everywhere with example.com