如何使用 Apache 和 Django(以及 Docker)配置我的 ProxyPass?
How do I configure my ProxyPass with Apache and Django (and Docker)?
我正在尝试构建一个本地 docker 容器以包含 Django 2/Python 3.7、Apache 2.4 和 MySql 5.7 图像。我在配置我的 Apache 代理以与我的 Django 实例正确交互时遇到问题。我的 apache/my-vhosts.conf 文件是这样的...
<VirtualHost *:80>
ServerName maps.example.com
ProxyPreserveHost On
ProxyPass / http://127.0.0.1/
ProxyPassReverse / http://127.0.0.1/
</VirtualHost>
我的 Apache 2.4 Dockerfile 看起来像
FROM httpd:2.4
COPY ./my-httpd.conf /usr/local/apache2/conf/httpd.conf
COPY ./my-vhosts.conf /usr/local/apache2/conf/extra/httpd-vhosts.conf
COPY ./maps /usr/local/apache2/htdocs/maps
我的整体 docker-compose.yml 文件看起来像 ...
version: '3'
services:
web:
restart: always
build: ./web
ports: # to access the container from outside
- "8000:8000"
environment:
DEBUG: 'true'
command: /usr/local/bin/gunicorn maps.wsgi:application -w 2 -b :8000
apache:
restart: always
build: ./apache/
ports:
- "80:80"
#volumes:
# - web-static:/www/static
links:
- web:web
mysql:
restart: always
image: mysql:5.7
environment:
MYSQL_DATABASE: 'maps_data'
# So you don't have to use root, but you can if you like
MYSQL_USER: 'chicommons'
# You can use whatever password you like
MYSQL_PASSWORD: 'password'
# Password for root access
MYSQL_ROOT_PASSWORD: 'password'
ports:
- "3406:3406"
volumes:
- my-db:/var/lib/mysql
volumes:
my-db:
遗憾的是,当我用 "docker-compose up," 启动所有内容时,我对“http://127.0.0.1/”的请求因 "The proxy server received an invalid response from an upstream server." 而死。在我的 docker-compose 输出中,我看到
apache_1 | [Sun Feb 09 21:07:37.521332 2020] [proxy:error] [pid 11:tid 140081943791360] [client 127.0.0.1:35934] AH00898: Error reading from remote server returned by /
apache_1 | 127.0.0.1 - - [09/Feb/2020:21:06:37 +0000] "GET / HTTP/1.1" 502 341
我认为问题是 apache/my-vhosts.conf 文件。
当您将 ProxyPass /
配置为 http://127.0.0.1/
时,这意味着您代理到 apache
服务的本地主机,而不是 web
服务或在主机上。
要代理传递到网络,请改用此 my-vhosts.conf 配置文件:
<VirtualHost *:80>
ServerName maps.example.com
ProxyPreserveHost On
ProxyPass / http://web:8000/
ProxyPassReverse / http://web:8000/
</VirtualHost>
我正在尝试构建一个本地 docker 容器以包含 Django 2/Python 3.7、Apache 2.4 和 MySql 5.7 图像。我在配置我的 Apache 代理以与我的 Django 实例正确交互时遇到问题。我的 apache/my-vhosts.conf 文件是这样的...
<VirtualHost *:80>
ServerName maps.example.com
ProxyPreserveHost On
ProxyPass / http://127.0.0.1/
ProxyPassReverse / http://127.0.0.1/
</VirtualHost>
我的 Apache 2.4 Dockerfile 看起来像
FROM httpd:2.4
COPY ./my-httpd.conf /usr/local/apache2/conf/httpd.conf
COPY ./my-vhosts.conf /usr/local/apache2/conf/extra/httpd-vhosts.conf
COPY ./maps /usr/local/apache2/htdocs/maps
我的整体 docker-compose.yml 文件看起来像 ...
version: '3'
services:
web:
restart: always
build: ./web
ports: # to access the container from outside
- "8000:8000"
environment:
DEBUG: 'true'
command: /usr/local/bin/gunicorn maps.wsgi:application -w 2 -b :8000
apache:
restart: always
build: ./apache/
ports:
- "80:80"
#volumes:
# - web-static:/www/static
links:
- web:web
mysql:
restart: always
image: mysql:5.7
environment:
MYSQL_DATABASE: 'maps_data'
# So you don't have to use root, but you can if you like
MYSQL_USER: 'chicommons'
# You can use whatever password you like
MYSQL_PASSWORD: 'password'
# Password for root access
MYSQL_ROOT_PASSWORD: 'password'
ports:
- "3406:3406"
volumes:
- my-db:/var/lib/mysql
volumes:
my-db:
遗憾的是,当我用 "docker-compose up," 启动所有内容时,我对“http://127.0.0.1/”的请求因 "The proxy server received an invalid response from an upstream server." 而死。在我的 docker-compose 输出中,我看到
apache_1 | [Sun Feb 09 21:07:37.521332 2020] [proxy:error] [pid 11:tid 140081943791360] [client 127.0.0.1:35934] AH00898: Error reading from remote server returned by /
apache_1 | 127.0.0.1 - - [09/Feb/2020:21:06:37 +0000] "GET / HTTP/1.1" 502 341
我认为问题是 apache/my-vhosts.conf 文件。
当您将 ProxyPass /
配置为 http://127.0.0.1/
时,这意味着您代理到 apache
服务的本地主机,而不是 web
服务或在主机上。
要代理传递到网络,请改用此 my-vhosts.conf 配置文件:
<VirtualHost *:80>
ServerName maps.example.com
ProxyPreserveHost On
ProxyPass / http://web:8000/
ProxyPassReverse / http://web:8000/
</VirtualHost>