我在 k8s 上有一个网站和入口控制器。此配置在返回静态 html / css / json 时起作用:
入口.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-com
annotations:
kubernetes.io/tls-acme: "true"
cert-manager.io/cluster-issuer: letsencrypt-prod
kubernetes.io/ingress.class: kong
spec:
tls:
- secretName: example-com
hosts:
- example.com
rules:
- host: example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: cvsite
port:
number: 80
Nginx.conf
server {
listen 80;
server_name example.com/blog;
ssl_certificate /etc/nginx/certs/tls.crt;
ssl_certificate_key /etc/nginx/certs/tls.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
include /etc/nginx/mime.types;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
但是添加这 3 行会破坏一切:
入口.yaml
paths:
- path: /cv ##CHANGE HERE
pathType: Prefix
backend:
service:
name: cvsite
port:
number: 80
Nginx.conf
location /cv { #CHANGE HERE
alias /usr/share/nginx/html/; #CHANGE HERE
index index.html index.htm;
}
curl -v https://example.com/cv
* SSL certificate verify ok.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x7fa82200a400)
> GET /cv HTTP/2
> Host: example.com
> User-Agent: curl/7.64.1
> Accept: */*
* Connection state changed (MAX_CONCURRENT_STREAMS == 128)!
< HTTP/2 404
< content-type: text/html; charset=UTF-8
< content-length: 153
< server: nginx/1.19.6
< date: Mon, 08 Feb 2021 17:40:19 GMT
< x-kong-upstream-latency: 1.9999771118164
< x-kong-proxy-latency: 1
< via: kong/2.2.1
<
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.19.6</center>
</body>
</html>
在浏览器中,https: //example.com/cv 仅返回空白页。
请问 ls /usr/share/nginx/html 显示 index.html 和静态文件时怎么会返回 404?
所以正如@mdaniel 建议的那样,从别名转移到根目录,加上在我的前端应用程序中修复路径解决了整体问题。如果您偶然发现此线程,这会有所帮助:https ://stackoverflow.com/questions/53207059/react-nginx-routing-to-subdirectory