因此我使用 docker-compose 运行一个 nginx 容器:
services:
nginx:
build:
context: .
dockerfile: Dockerfile
restart: always
container_name: webserver_nginx
hostname: nginx
stop_grace_period: 1m30s
healthcheck:
test: ["CMD-SHELL", "curl -sf http://127.0.0.1/ -o /dev/null || exit 1"]
interval: 1m30s
timeout: 10s
retries: 3
start_period: 40s
ports:
- 443:443
- 80:80
volumes:
# Service Restriction with password
- ./.credentials/.htpasswd:/var/.credentials/.htpasswd
# Nginx Configurations
- ./nginx.conf:/etc/nginx/nginx.conf
- ./proxy_params:/etc/nginx/proxy_params
- ./conf:/etc/nginx/conf.d
#Nginx serve static files
- /var/backups/off:/var/web_data/off
# Nginx Logging
- /var/log/nginx:/var/log/nginx
networks:
- webserver_network
networks:
webserver_network:
external: true
Dockerfile:
FROM nginx
CMD ["nginx", "-g", "daemon off;"]
我的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;
}
并配置我的容器:
server {
listen 80;
listen [::]:80;
server_name name.com;
location /admin {
proxy_pass http://off:4000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 100;
proxy_http_version 1.1;
}
location /static {
alias /var/web_data/off/collectstatic;
}
location /media {
alias /var/web_data/off/upload;
}
location / {
return 404;
}
}
因此,每当我尝试请求时,http://name.com/admin
我都会从 access.log 中获取这种日志:
127.0.0.1 - - [16/May/2022:15:53:42 +0000] "GET / HTTP/1.1" 404 153 "-" "curl/7.74.0" "-"
127.0.0.1 - - [16/May/2022:15:55:12 +0000] "GET / HTTP/1.1" 404 153 "-" "curl/7.74.0" "-"
127.0.0.1 - - [16/May/2022:15:56:42 +0000] "GET / HTTP/1.1" 404 153 "-" "curl/7.74.0" "-"
并且我在浏览器上从 nginx 获得 502 网关错误,即使我尝试访问主站点,http://name.com/
我仍然收到 502 错误,我的容器组合如下:
version: '3.8'
services:
offch:
build:
context: .
dockerfile: deploy/beta/Dockerfile
args:
WORKING_DIR: /app
container_name: off
hostname: django
restart: 'always'
command: gunicorn -c deploy/beta/configs/gunicorn/conf.py --chdir api api.wsgi:application
volumes:
# Source Code of project
- .:/app
# Collect Static files
- /var/backups/offch/collectstatic:/collectstatic
# Media Files (Uploads)
- /var/backups/offch/upload:/app/media/upload
- /var/log/upstart:/var/log/upstart
stop_grace_period: 1m30s
healthcheck:
test: ["CMD", "curl", "-f", "http://127.0.0.1:4000"]
interval: 1m30s
timeout: 10s
retries: 3
start_period: 40s
ports:
- "4000:4000"
networks:
- webserver_network
networks:
webserver_network:
external: true
顺便说一句,我可以通过 IP 和端口访问该网站,只有在 nginx 上才有这个问题。