我的 Docker 项目中的 Nginx 出现了问题。我知道这里有很多类似的帖子,但没有一个能帮助我。
我正在使用 Nginx 和 Docker,我的网站可以正常运行,但我想添加一些扩展和安全功能,并改进编写的代码以提高性能。为此,我想添加一个 http 块并定义一个事件块。
当我尝试添加这些内容时,出现此错误:
[emerg] 7#7: "events" directive is not allowed here in /etc/nginx/conf.d/app.conf:1
或者当我删除事件部分时:
[emerg] 7#7: "http" directive is not allowed here in /etc/nginx/conf.d/app.conf:13
由于某些原因,我的 /etc/nginx/ 目录中没有 sites-enabled 和 sites-available 文件夹。我不知道这是否重要。
但是,我的 app.conf 文件如下所示(可以正常工作):
#events {
# worker_processes auto;
# worker_connections 1024;
# worker_rlimit_nofile 2048;
# multi_accept on;
# mutex_accept on;
# mutex_accept_delay 500ms;
# use epoll;
# epoll_events 512;
#}
#http {
#access_log logs/access.log combined;
#error_log logs/warn.log warn;
upstream app {
least_conn;
server app_2:8001;
server app_2:443;
server app:8000;
server app:443;
}
server {
listen 80;
server_name find-your-spirits.de;
location / {
return 301 https://$host$request_uri;
}
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
}
server {
listen 443 ssl;
server_name find-your-spirits.de;
location / {
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/.htpasswd;
proxy_pass http://app;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /staticfiles/ {
alias /app/staticfiles/;
add_header Access-Control-Allow-Origin *;
}
ssl_certificate /etc/letsencrypt/live/find-your-spirits.de/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/find-your-spirits.de/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
#}
如果我删除#,我就会收到我提到的错误。
这是我的 Dockerfile:
FROM nginx:1.19.0
RUN rm /etc/nginx/conf.d/default.conf
COPY app.conf /etc/nginx/conf.d
这是我的 docker-compose 文件:
version: '3'
services:
app:
image: django
build:
context: ./app
dockerfile: Dockerfile
env_file:
- ./.test.env
volumes:
- ./app/:/app/
- ./app/staticfiles/:/app/staticfiles
- ./data/.htpasswd:/etc/nginx/.htpasswd
command: gunicorn --bind 0.0.0.0:8000 --chdir /app/ Webserver.wsgi
app_2:
image: django
build:
context: ./app
dockerfile: Dockerfile
env_file:
- ./.test.env
volumes:
- ./app/:/app/
- ./app/staticfiles/:/app/staticfiles
- ./data/.htpasswd:/etc/nginx/.htpasswd
command: gunicorn --bind 0.0.0.0:8001 --chdir /app/ Webserver.wsgi
nginx:
image: nginx:1.19.0
build:
context: ./data/nginx
dockerfile: Dockerfile
ports:
- "80:80"
- "443:443"
volumes:
- ./data/nginx:/etc/nginx/conf.d
- ./app/staticfiles/:/app/staticfiles
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
- ./data/.htpasswd:/etc/nginx/.htpasswd
command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
certbot:
image: certbot/certbot
volumes:
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
db:
image: postgres
volumes:
- postgres_data:/var/lib/postgresql/data/
env_file:
- ./.test.env
environment:
- POSTGRES_DB_PORT=${SQL_PORT}
- POSTGRES_DB_HOST=${SQL_HOST}
- POSTGRES_PASSWORD=${SQL_PASSWORD}
- POSTGRES_USER=${SQL_USER}
- POSTGRES_DB=${SQL_DATABASE}
ports:
- 5432:5432
volumes:
postgres_data
如果您需要更多信息或其他文件的内容,请发表评论,我会随后添加它们。
另外,我对 Nginx 还很陌生,所以如果您发现某些写得不好的地方,请随时纠正或改进。