
我的 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 還很陌生,所以如果你看到寫得不好的東西,請隨時糾正或改進它。