django 在 apache 和 nginx 上提供媒体文件
我即将进入 django 项目的生产模式,但遇到了一个特殊的问题。我通过 apahce+mod_wsgi 运行 django 并通过 nginx 提供静态文件。
然而,我的情况要求我无法从 nginx 提供“所有”静态文件。只需要从 apache 提供“open-flash-chart.swf”。该项目使用 openpyc 并嵌入 open-flash-chart.swf ,它需要与 django 在同一服务器上运行,在我的例子中是 Apache。 我怎样才能做到这一点?我需要对 Apache 配置文件进行哪些更改?
server {
listen 80 default;
server_name localhost;
access_log /var/log/nginx/localhost.access.log;
location / {
proxy_pass http://localhost:8080;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location /media/ {
root /srv/www/enpass/;
expires max;
}
}
I'm going into production mode for my django project, but running into a peculiar problem. I'm running my django through apahce+mod_wsgi and serving static files through nginx.
However my situation demands that I cannot serve "all" static files from nginx. There is a need to serve only "open-flash-chart.swf" from apache. The project uses openpyc and embeds open-flash-chart.swf which needs to run on same server as django, which in my case is Apache.
How can I accomplish that? What changes to I need to make into Apache config files?
server {
listen 80 default;
server_name localhost;
access_log /var/log/nginx/localhost.access.log;
location / {
proxy_pass http://localhost:8080;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location /media/ {
root /srv/www/enpass/;
expires max;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Apache 中,在虚拟主机中设置一个别名来直接提供该文件:
然后,不要使用
{{ MEDIA_URL }}
来引用该文件,而是在绝对路径中编写代码:Nginx 仍将代理该文件请求(因为这不是您的媒体路径),然后 Apache 会将文件传递回 nginx。
或者,不推荐,但如果必须直接从 Apache 到浏览器,您可以指定端口:
In Apache, set up an alias in your virtual host to serve this file directly:
Then, instead of using
{{ MEDIA_URL }}
to reference the file, code in the absolute path:Nginx will still proxy the request (because it's not your media path), and then Apache will deliver the file back to nginx.
Alternatively, and not recommended, but if it must go straight from Apache to the browser, you could specify the port:
您需要更改 nginx 配置来处理
apache,就像您对 / (root) 所做的那样
You need to change nginx config to handle
with apache, same way you did it for / (root)