gpt4 book ai didi

angular - nginx 返回所有 Angular 文件(包括脚本)的 index.html 内容

转载 作者:行者123 更新时间:2023-12-02 16:09:45 25 4
gpt4 key购买 nike

我已经建立了一个 Angular (v9) 应用程序的解决方案,该应用程序正在构建为 Docker 镜像(使用 nginx 作为网络服务器)并部署到 Kubernetes。一切正常,除了对于每个请求,无论是根应用程序本身还是它的 javascript 文件,我都会收到 index.html 的内容。

我的 nginx 配置文件如下所示(大部分是默认的):

pid        /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
types {
module;
}

include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

keepalive_timeout 65;

#gzip on;

include /etc/nginx/conf.d/*.conf;

add_header X-Content-Type-Options nosniff;

server {
location / {
# First attempt to serve request as file, then
# as directory, then redirect to index(angular) if no file found.
try_files $uri $uri/ /index.html;
}
}
}

即使我注释掉 location/try_files 配置行,情况仍然相同。我找到了很多关于创建此类重写的指导,但我没有找到任何可以解释为什么在我没有配置它的情况下会发生这种重写的任何内容。

最佳答案

好吧,事实证明,它与 nginx 无关。入口元素配置不正确,将每个请求重写为“/”,因此每个请求都返回默认页面(=index.html)。

更详细地说,我在spec.rules中有以下配置(注意最后几行)

http:
paths:
- backend:
serviceName: my-app
servicePort: 80
path: /

必须更改为:

http:
paths:
- backend:
serviceName: my-app
servicePort: 80
path: /(.*)

关于angular - nginx 返回所有 Angular 文件(包括脚本)的 index.html 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60579877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com