django 在 apache 和 nginx 上提供媒体文件

发布于 2024-10-14 15:49:16 字数 964 浏览 3 评论 0原文

我即将进入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枯叶蝶 2024-10-21 15:49:16

在 Apache 中,在虚拟主机中设置一个别名来直接提供该文件:

Alias /url/to/open-flash-chart.swf /full/path/to/open-flash-chart.swf

然后,不要使用 {{ MEDIA_URL }} 来引用该文件,而是在绝对路径中编写代码:

<object data="/url/to/open-flash-chart.swf" />

Nginx 仍将代理该文件请求(因为这不是您的媒体路径),然后 Apache 会将文件传递回 nginx。

或者,不推荐,但如果必须直接从 Apache 到浏览器,您可以指定端口:

<object data="http://servername:8080/url/to/open-flash-chart.swf" />

In Apache, set up an alias in your virtual host to serve this file directly:

Alias /url/to/open-flash-chart.swf /full/path/to/open-flash-chart.swf

Then, instead of using {{ MEDIA_URL }} to reference the file, code in the absolute path:

<object data="/url/to/open-flash-chart.swf" />

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:

<object data="http://servername:8080/url/to/open-flash-chart.swf" />
巷雨优美回忆 2024-10-21 15:49:16

您需要更改 nginx 配置来处理

/path/to/open-flash-chart.swf 

apache,就像您对 / (root) 所做的那样

You need to change nginx config to handle

/path/to/open-flash-chart.swf 

with apache, same way you did it for / (root)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文