django - 在 Django settings.py for Django-debug-toolbar 的 docker 中获取 NGINX ip 地址
问题描述
我有一个 dockerized DRF 项目,其中安装了 NGINX。一切正常,除了一件事:
Django debug toolbar
需要INTERNAL_IPS
在 settings.py 中指定参数。
对于码头工人,我使用这个:
hostname, _, ips = socket.gethostbyname_ex(socket.gethostname())
INTERNAL_IPS = [ip[:-1] + "1" for ip in ips]
它也可以正常工作,但不适用于 NGINX,因为 NGINX 使用它自己的 ip 动态(可能?)在内部定义或由 docker 或其他任何东西分配。
我可以从服务器日志中获取此 IP:
172.19.0.8 - - [09/Oct/2020:17:10:40 +0000] "GET /admin/ HTTP/1.0" 200 6166 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 OPR/71.0.3770.228"
并将其添加到setting.py:
hostname, _, ips = socket.gethostbyname_ex(socket.gethostname())
INTERNAL_IPS = [ip[:-1] + "1" for ip in ips]
INTERNAL_IPS.append('172.18.0.8')
但我希望这个 ip 在不同的机器等上可能会有所不同,所以它不够可靠。
所以这个问题是 - 是否有可能以某种方式在 settings.py 中动态获取 NGINX docker ip 或以某种方式修复 docker-compose?
码头工人撰写:
version: '3.8'
volumes:
postgres_data:
redis_data:
static_volume:
media_volume:
services:
web:
build: .
#command: python /code/manage.py runserver 0.0.0.0:8000
command: gunicorn series.wsgi:application --config ./gunicorn.conf.py
env_file:
- ./series/.env
volumes:
- .:/code
- static_volume:/home/app/web/staticfiles
- media_volume:/home/app/web/mediafiles
# ports:
# - 8000:8000
expose:
- 8000
depends_on:
- db
- redis
- celery
- celery-beat
links:
- db
nginx:
build: ./nginx
volumes:
- static_volume:/home/app/web/staticfiles
- media_volume:/home/app/web/mediafiles
ports:
# - 1337:80
- 8000:80
depends_on:
- web
db:
build:
context: .
dockerfile: postgres.dockerfile
restart: always
env_file:
- ./series/.env
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
volumes:
- postgres_data:/var/lib/postgresql/data/
ports:
- target: 5432
published: 5433
protocol: tcp
mode: host
redis:
image: redis:alpine
command: >
redis-server
--appendonly yes
--appendfsync no
--auto-aof-rewrite-percentage 100
--auto-aof-rewrite-min-size 64mb
ports:
- target: 6379
published: 6380
protocol: tcp
mode: host
volumes:
- redis_data:/data
restart: always
environment:
- REDIS_REPLICATION_MODE=master
celery:
build: .
command: celery worker -A series --loglevel=INFO --concurrency=4 -E
restart: always
environment:
- C_FORCE_ROOT=1
volumes:
- .:/code
depends_on:
- db
- redis
hostname: celery-main
celery-beat:
build: .
command: celery -A series beat --loglevel=INFO --pidfile=
restart: always
volumes:
- .:/code
depends_on:
- db
- redis
hostname: celery-beat
flower:
# http://localhost:8888/
image: mher/flower
environment:
- CELERY_BROKER_URL=redis://redis:6379/1
- FLOWER_PORT=8888
depends_on:
- celery
- celery-beat
- redis
restart: always
ports:
- target: 8888
published: 8888
protocol: tcp
mode: host
NGINX的dockerfile:
FROM nginx:1.19.0-alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d
谢谢
解决方案
推荐阅读
- ruby - 红宝石数相等?
- angular - 在 FormArray 中无法捕获 ngSubmit
- mysql - DBDParams 密码转义
- reactjs - 反应树实体路由
- eclipse-plugin - 如何从 p2-Repository 解析 Eclipse 插件并将它们用作 Maven 依赖项
- ruby-on-rails - CSV parsing returns "Unquoted fields do not allow \r or \n" but can't find error in source file?
- python - 为数据缓冲区创建类似文件的对象
- r - 使用 r 将字符串中的匹配单词替换为单词
- django - 即使在视图设置为@xframe_options_exempt 后,Django iframe 加载也被 xframe 选项拒绝
- jenkins - 我如何判断 Jenkins checkout 是否拉了任何东西?