nginx没有提供go静态文件服务。

huangapple go评论118阅读模式
英文:

nginx is not serving go static files

问题

我的应用程序结构如下:

  1. .
  2. ├── src
  3. └── 一些 Go 文件
  4. ├── templates
  5. ├── static
  6. |── images
  7. |── js
  8. └── styles

这是我的 Dockerfile

  1. FROM golang:1.18
  2. WORKDIR /usr/src/app
  3. COPY go.mod .
  4. COPY go.sum .
  5. RUN go mod download
  6. COPY . .
  7. CMD ["go", "run", "src/cmd/main.go"]

这是我的 docker-compose.yml

  1. version: "3.8"
  2. services:
  3. pgsql:
  4. image: postgres
  5. ports:
  6. - "5432:5432"
  7. volumes:
  8. - todo_pg_db:/var/lib/postgresql/data
  9. environment:
  10. - POSTGRES_DB=todo
  11. - POSTGRES_USER=postgres
  12. - POSTGRES_PASSWORD=postgres
  13. app:
  14. build: .
  15. ports:
  16. - "8080"
  17. restart: always
  18. depends_on:
  19. - pgsql
  20. nginx:
  21. image: nginx
  22. restart: always
  23. ports:
  24. - 801:801
  25. volumes:
  26. - ./nginx.conf:/etc/nginx/nginx.conf
  27. volumes:
  28. todo_pg_db:

这是 nginx.conf 文件:

  1. worker_processes auto;
  2. error_log /var/log/nginx/error.log;
  3. pid /var/run/nginx.pid;
  4. events {
  5. worker_connections 1024;
  6. }
  7. http {
  8. include mime.types;
  9. default_type application/octet-stream;
  10. access_log /var/log/nginx/access.log;
  11. sendfile on;
  12. server {
  13. listen 801;
  14. server_name 127.0.0.1;
  15. charset utf-8;
  16. keepalive_timeout 5;
  17. location / {
  18. # 检查静态文件,如果找不到则代理到应用程序
  19. try_files $uri @backend;
  20. }
  21. location @backend {
  22. # client_max_body_size 10m;
  23. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  24. proxy_set_header X-Forwarded-Proto $scheme;
  25. proxy_set_header Host $http_host;
  26. proxy_redirect off;
  27. proxy_pass http://app:8080;
  28. }
  29. }
  30. }

我的问题是 nginx 找不到静态文件。这里是一些示例日志:

  1. open() "/usr/src/app/static/styles/bootstrap.min.css" failed (2: No such file or directory)

但是该目录确实存在。当我使用以下命令在 Docker 容器中执行时:sudo docker exec -it todo_app_1 bash,然后查看文件的内容,一切正常:

  1. cat /usr/src/app/static/styles/bootstrap.min.css
  2. # 输出:文件内容...

我不知道问题出在哪里。我漏掉了什么?

英文:

My app structure is like this:

  1. .
  2. ├── src
  3. └── some go files
  4. ├── templates
  5. ├── static
  6. |── images
  7. |── js
  8. └── styles

And here is my Dockerfile:

  1. FROM golang:1.18
  2. WORKDIR /usr/src/app
  3. COPY go.mod .
  4. COPY go.sum .
  5. RUN go mod download
  6. COPY . .
  7. CMD ["go", "run", "src/cmd/main.go"]

And here is my docker-compose.yml:

  1. version: "3.8"
  2. services:
  3. pgsql:
  4. image: postgres
  5. ports:
  6. - "5432:5432"
  7. volumes:
  8. - todo_pg_db:/var/lib/postgresql/data
  9. environment:
  10. - POSTGRES_DB=todo
  11. - POSTGRES_USER=postgres
  12. - POSTGRES_PASSWORD=postgres
  13. app:
  14. build: .
  15. ports:
  16. - "8080"
  17. restart: always
  18. depends_on:
  19. - pgsql
  20. nginx:
  21. image: nginx
  22. restart: always
  23. ports:
  24. - 801:801
  25. volumes:
  26. - ./nginx.conf:/etc/nginx/nginx.conf
  27. volumes:
  28. todo_pg_db:

And here is the nginx.conf:

  1. worker_processes auto;
  2. error_log /var/log/nginx/error.log;
  3. pid /var/run/nginx.pid;
  4. events {
  5. worker_connections 1024;
  6. }
  7. http {
  8. include mime.types;
  9. default_type application/octet-stream;
  10. access_log /var/log/nginx/access.log;
  11. sendfile on;
  12. server {
  13. listen 801;
  14. server_name 127.0.0.1;
  15. charset utf-8;
  16. keepalive_timeout 5;
  17. location / {
  18. # checks for static file, if not found proxy to app
  19. try_files $uri @backend;
  20. }
  21. location @backend {
  22. # client_max_body_size 10m;
  23. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  24. proxy_set_header X-Forwarded-Proto $scheme;
  25. proxy_set_header Host $http_host;
  26. proxy_redirect off;
  27. proxy_pass http://app:8080;
  28. }
  29. }
  30. }

My problem is that nginx can't find static files.
Here is some example logs:

  1. open() "/usr/src/app/static/styles/bootstrap.min.css" failed (2: No such file or directory)

But there is such directory.
when I exec to my docker container using this commad: sudo docker exec -it todo_app_1 bash.
Then I cat contents of the file, and it works fine!!!

  1. cat /usr/src/app/static/styles/bootstrap.min.css
  2. # output: file content...

I don't know what is wrong in here.
What am I missing?

答案1

得分: 2

我已经使用volumes修复了这个问题:

  1. nginx:
  2. image: nginx
  3. restart: always
  4. ports:
  5. - 801:801
  6. volumes:
  7. - ./static:/var/www
  8. - ./nginx.conf:/etc/nginx/nginx.conf

nginx.conf文件中的配置如下:

  1. location /static {
  2. alias /var/www;
  3. }
英文:

I have fixed that using volumes:

  1. nginx:
  2. image: nginx
  3. restart: always
  4. ports:
  5. - 801:801
  6. volumes:
  7. - ./static:/var/www
  8. - ./nginx.conf:/etc/nginx/nginx.conf

and in nginx.conf:

  1. location /static {
  2. alias /var/www;
  3. }

huangapple
  • 本文由 发表于 2022年4月1日 22:36:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/71708643.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定