我正在配置 Docker Nginx,但一直遇到以下错误:
nginx-1 | 2024/12/26 11:57:50 [emerg] 1#1: "worker_processes" directive is not allowed here in /etc/nginx/conf.d/nginx.conf:1
nginx-1 | nginx: [emerg] "worker_processes" directive is not allowed here in /etc/nginx/conf.d/nginx.conf:1
我在做什么: 我正在尝试使用 Docker 设置 Nginx,我的设置包括三个文件:
docker-compose.yml:
nginx:
build: ./nginx
volumes:
- static_volumes:/usr/src/app/_static
- media_volumes:/usr/src/app/_media
ports:
- 80:80
depends_on:
- web
web:
build: .
command: gunicorn daniel_blog.wsgi:application --bind 0.0.0.0:8000
volumes:
- static_volumes:/usr/src/app/_static
- media_volumes:/usr/src/app/_media
- ./:/usr/src/app/_media
expose:
- 8000
env_file:
- ./.env.prod
depends_on:
- db
db:
image: postgres:17.0-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
env_file:
- ./.env.prod.db
volumes:
postgres_data:
static_volumes:
media_volumes:
nginx/nginx.conf:
server {
listen 80;
server_name localhost;
# Default location
location / {
proxy_pass http://daniel_blog;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
# Static files
location /static/ {
alias /usr/src/app/_static/;
}
# Media files
location /media/ {
alias /usr/src/app/_media/;
}
}
nginx/Dockerfile:
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d
问题: 我添加了
worker_processes auto
;在我的nginx.conf
的顶部,但我不断收到错误:
nginx-1 | nginx: [emerg] "worker_processes" directive is not allowed here in /etc/nginx/conf.d/nginx.conf:1
即使我删除
worker_processes auto
;,同样的错误仍然存在。
我很困惑,因为我认为 worker_processes
应该在 nginx.conf
中工作。
从错误日志来看,似乎是文件
/etc/nginx/conf.d/nginx.conf
导致了问题。
我相信这是因为我的 Dockerfile 中的 COPY 指令而被引用的。
我的理解(和问题): 我读到,worker_processes 应该只存在于主 Nginx 配置文件中,如
/etc/nginx/nginx.conf
,而不是 /etc/nginx/conf.d/*.conf
。
这是正确的吗?如果是这样,我应该如何调整我的 nginx.conf
和 Dockerfile 来解决这个问题?
由于我正在学习书籍教程,因此我不完全理解为什么会出现此错误。你能帮我理解为什么
/etc/nginx/conf.d/nginx.conf
中不允许worker_processes吗?
任何帮助或指导将不胜感激!
将worker_processes移至主配置文件
不要将worker_processes放入/etc/nginx/conf.d/nginx.conf中,而是将其放入主nginx.conf文件(/etc/nginx/nginx.conf)中,如下所示
server {
listen 80;
server_name localhost;
# Default location
location / {
proxy_pass http://daniel_blog;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
# Static files
location /static/ {
alias /usr/src/app/_static/;
}
# Media files
location /media/ {
alias /usr/src/app/_media/;
}
}
更新主 nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
修改 Dockerfile
FROM nginx:latest
COPY nginx/nginx.conf /etc/nginx/nginx.conf
COPY nginx/nginx.conf /etc/nginx/conf.d/nginx.conf
重建您的 Docker 容器
docker-compose down
docker-compose up --build