nginx 不提供静态文件
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找不到静态文件。
以下是一些示例日志:
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;
}
我的应用结构是这样的:
.
├── 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找不到静态文件。
以下是一些示例日志:
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;
}