我有一个使用多个容器的Web应用程序(基于Django):
根据我阅读的一些文档,我应该使用NGINX之类的东西来提供我的静态内容。但是我不知道该怎么做。我应该在Web应用程序容器上还是作为单独的NGINX容器安装NGINX。我如何通过Traefik的请求?据我所知,您无法使用Traefik服务器静态内容。
这是我的docker-compose.yml的样子:
4 warnings generated.
ERROR: /Users/isaacsultan/Code/tensorflow/tensorflow/BUILD:576:1: Executing genrule //tensorflow:tensorflow_python_api_gen failed (Exit 1)
/anaconda/lib/python2.7/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters
Traceback (most recent call last):
File "/private/var/tmp/_bazel_isaacsultan/0e2667ab20883652d759a6a805575b2d/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/create_tensorflow.python_api.runfiles/org_tensorflow/tensorflow/python/tools/api/generator/create_python_api.py", line 27, in <module>
from tensorflow.python.tools.api.generator import doc_srcs
File "/private/var/tmp/_bazel_isaacsultan/0e2667ab20883652d759a6a805575b2d/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/create_tensorflow.python_api.runfiles/org_tensorflow/tensorflow/python/__init__.py", line 81, in <module>
from tensorflow.python import keras
File "/private/var/tmp/_bazel_isaacsultan/0e2667ab20883652d759a6a805575b2d/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/create_tensorflow.python_api.runfiles/org_tensorflow/tensorflow/python/keras/__init__.py", line 25, in <module>
from tensorflow.python.keras import applications
File "/private/var/tmp/_bazel_isaacsultan/0e2667ab20883652d759a6a805575b2d/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/create_tensorflow.python_api.runfiles/org_tensorflow/tensorflow/python/keras/applications/__init__.py", line 21, in <module>
import keras_applications
ImportError: No module named keras_applications
INFO: Elapsed time: 57510.356s, Critical Path: 492.10s
INFO: 6867 processes: 6867 local.
FAILED: Build did NOT complete successfully
答案 0 :(得分:3)
如果其他人需要此答案,则答案在于创建单独的NGINX服务,然后将前端规则定向到静态位置(xyz.com/static),例如参见下文(docker-compose.yml的一部分):
nginx:
image: nginx:alpine
container_name: nginx_static_files
restart: always
volumes:
- ./default.conf:/etc/nginx/conf.d/default.conf
- ./saleor/static/:/static
labels:
- "traefik.enable=true"
- "traefik.backend=nginx"
- "traefik.frontend.rule=Host:xyz.co;PathPrefix:/static"
- "traefik.port=80"
您还需要确保正确配置了Nginx配置文件(default.conf):
server {
listen 80;
server_name _;
client_max_body_size 200M;
set $cache_uri $request_uri;
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; access_log off; }
ignore_invalid_headers on;
add_header Access-Control-Allow_Origin *;
location /static {
autoindex on;
alias /static;
}
location /media {
autoindex on;
alias /media;
}
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
}
所有荣誉归功于Traefik闲暇频道上的Pedro Rigotti,帮助我达成了解决方案。
答案 1 :(得分:0)
我对Traefik和Docker不太了解。
但是我可以告诉您如何安装nginx并使用它来提供静态文件(始终建议您这样做,以免通过提供静态文件而使django服务器停止运行)
安装nginx并按照提及的步骤来设置nginx。
sudo apt-get install nginx
该网站可用的文件应如下所示:
server {
listen 80;
listen [::]:80;
server_name xyz.com;
client_max_body_size 20M;
# xyz.com/media/any_static_asset_file.jpg
# when anyone hits the above url then the request comes to this part.
location /media/ {
# do make sure that the autoindex is off so that your assets are only accessed when you have proper path
autoindex off;
# this is the folder where your asset files are present.
alias /var/www/services/media/;
}
# whenever any request comes to xyz.com then this part would handle the request
location / {
proxy_pass http://unix:/var/www/services/xyz/django_server.sock;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}