簡體   English   中英

nginx 不提供 go static 文件

[英]nginx is not serving go static files

我的應用程序結構是這樣的:

.
├── src
│   └── some go files
├── templates
├── static
    |── images
    |── js
    └── styles

這是我的Dockerfile

FROM golang:1.18

WORKDIR /usr/src/app

COPY go.mod .
COPY go.sum .

RUN go mod download

COPY . .

CMD ["go", "run", "src/cmd/main.go"]  

這是我的docker-compose.yml

version: "3.8"


services:
  pgsql:
    image: postgres
    ports:
      - "5432:5432"
    volumes:
      - todo_pg_db:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=todo
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres

  app:
    build: .
    ports:
      - "8080"
    restart: always
    depends_on:
      - pgsql

    
  nginx:
    image: nginx
    restart: always
    ports:
      - 801:801
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf


volumes:
  todo_pg_db:

這是nginx.conf

worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include mime.types;
    default_type application/octet-stream;
    access_log /var/log/nginx/access.log;
    sendfile on;

    server {
        listen 801;
        server_name 127.0.0.1;
        charset utf-8;
        keepalive_timeout 5;

        location / {
            # checks for static file, if not found proxy to app
            try_files $uri @backend;
        }

        location @backend {
            # client_max_body_size 10m;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_pass http://app:8080;
        }
    }
}

我的問題是 nginx 找不到 static 文件。
以下是一些示例日志:

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 然后我 cat 文件的內容,它工作正常!!!

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

我不知道這里出了什么問題。 我錯過了什么?

我已經修復了使用volumes的問題:

nginx:
  image: nginx
  restart: always
  ports:
    - 801:801
  volumes:
    - ./static:/var/www
    - ./nginx.conf:/etc/nginx/nginx.conf

nginx.conf中:

location /static {
    alias /var/www;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM