简体   繁体   English

Nginx pod 不提供 css 文件

[英]Nginx pod not serving css files

I have a webapp running in kubernetes.我有一个在 kubernetes 中运行的 webapp。 I want to serve static files, css in my case, from nginx pod.我想从 nginx 吊舱中提供 static 文件、css 文件。 From the application I define css file location like this:从应用程序中,我定义了 css 文件位置,如下所示:

 <link rel="stylesheet" href="assets/css/stylesheet.css" type="text/css">

When building docker image I copy over css file to www/media/ and in nginx config I point to that:在构建 docker 图像时,我将 css 文件复制到www/media/和 nginx 配置中,我指出:

FROM nginx
RUN rm /etc/nginx/conf.d/default.conf
COPY config/default.conf /etc/nginx/conf.d/default.template
COPY assets/ /www/media
EXPOSE 80

Here's nginx config:这是 nginx 配置:

server {
    listen 0.0.0.0:80;
    server_name localhost;
    location / {
        proxy_pass http://${FLASK_APP}:8080/;
    }

    location ~ /assets {
        root /www/media;
    }
}

I have confirmed that the file can be found on nginx pod under /www/media/css/stylesheet.css , however I cannot reach it neither from the browser nor the application itself.我已经确认可以在 /www/media/css/stylesheet.css 下的/www/media/css/stylesheet.css pod 上找到该文件,但是我无法从浏览器或应用程序本身访问它。 The error I get is this: GET http://192.168.99.106:30604/assets/css/stylesheet.css net::ERR_ABORTED 404 (Not Found)我得到的错误是: GET http://192.168.99.106:30604/assets/css/stylesheet.css net::ERR_ABORTED 404 (Not Found)

/assets should point to www/media where the directory with stylesheet are kept, correct? /assets应该指向保存带有样式表的目录的www/media ,对吗? What am I misunderstanding?我有什么误解?

Not sure if this is the solution, but hopefully some things to try.不确定这是否是解决方案,但希望可以尝试一些事情。

  1. Change your docker file to将您的 docker 文件更改为

COPY assets /www/media

  1. In your comments you've said that you can see the files in /www/media.在您的评论中,您说过您可以在 /www/media 中看到这些文件。 But you're trying to access them in /assets.但是您正试图在 /assets 中访问它们。 Have you configured this in nginx correctly?您是否在 nginx 中正确配置了这个? Perhaps try this也许试试这个
location /assets/ {
    alias /www/media/;
  }
  1. Final thing I would mention is permissions.我要提到的最后一件事是权限。 What are the permissions of the files in the container?容器中文件的权限是什么? ls -la will tell you this. ls -la会告诉你这个。 They should be 755 I believe for Nignx.我相信 Nignx 应该是755

Hope this helps you.希望这对您有所帮助。

Ok, I figured it out.好的,我想通了。 Here's my Nginx config to serve the files:这是我的 Nginx 配置来提供文件:

server {
listen 0.0.0.0:80;
server_name localhost;

    location / {
      proxy_pass http://${FLASK_APP}:8080/; 
  }
    location ~ \.css {
      root /www/media;   
    }
}

I also changed my docker a bit:我还稍微改变了我的 docker:

FROM nginx
RUN rm /etc/nginx/conf.d/default.conf
COPY config/default.conf /etc/nginx/conf.d/default.template
COPY assets /www/media/assets
RUN chown -R nginx:nginx /www/
EXPOSE 80

With the above config in place css is being served without any issues.有了上述配置,css 的服务就没有任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM