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

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

nginx is not serving go static files

问题

我的应用程序结构如下:

.
├── src
│   └── 一些 Go 文件
├── 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 / {
            # 检查静态文件,如果找不到则代理到应用程序
            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 找不到静态文件。这里是一些示例日志:

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 /usr/src/app/static/styles/bootstrap.min.css
# 输出:文件内容...

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

英文:

My app structure is like this:

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

And here is my 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"]  

And here is my 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:

And here is the 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;
        }
    }
}

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

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!!!

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

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

答案1

得分: 2

我已经使用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;
}
英文:

I have fixed that using volumes:

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

and in nginx.conf:

location /static {
    alias /var/www;
}

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:

确定