Nginx 不返回子路径的 css/js

Nginx 不返回子路径的 css/js

我在 k8s 上有一个网站和入口控制器。此配置用于返回静态 html / css / json:

入口文件

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配置文件

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;
    }

然而添加以下三行会破坏一切:

入口文件

paths:
      - path: /cv      ##CHANGE HERE
        pathType: Prefix
        backend:
          service:
            name: cvsite
            port: 
              number: 80

Nginx配置文件

location /cv {                                    #CHANGE HERE
        alias   /usr/share/nginx/html/;            #CHANGE HERE   
        index  index.html index.htm;              
    }

卷曲-vhttps://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?

答案1

因此,正如 @mdaniel 所建议的那样,从别名改为 root,再加上修复前端应用程序中的路径,就可以解决整个问题。如果您偶然发现此线程,这将有所帮助:https://stackoverflow.com/questions/53207059/react-nginx-routing-to-subdirectory

相关内容