gpt4 book ai didi

Nginx:X-Accel-Redirect 内部不起作用

转载 作者:行者123 更新时间:2023-12-01 19:55:59 26 4
gpt4 key购买 nike

这是我在 nginx 上的“内部”设置:

location /issues/ {
root /home/some_user/some_project/;
internal;
}

当我注释掉“内部”部分时,Nginx 会在“问题”文件夹中提供文件,并且其中的文件很好。但即使启用它,我仍然无法通过 Python 或 PHP 代码提供任何文件:

# Python, outputs an empty zip        
import os
file_name = '12.pdf.zip'
response = HttpResponse()
response['Content-Disposition'] = 'attachment; filename=%s' % t_str(file_name)
response['Content-Length'] = os.path.getsize(file_path)
response['Content-Type'] = "application/zip"
response['X-Accel-Redirect'] = '12.pdf.zip'
return response

# PHP, does nothing
header("X-Accel-Redirect: 12.pdf.zip");

这些是站点启用/默认和 nginx.conf。 8000 用于 Django,81 用于 PHP

server {

listen 80; ## listen for ipv4

server_name localhost;

access_log /var/log/nginx/localhost.access.log;

location / {
# proxy / requests to apache running django on port 8081
proxy_pass http://127.0.0.1:8000/;
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;
proxy_max_temp_file_size 0;

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 /issues/ {
root /home/some_user/some_project/;
internal;
}
}

worker_processes 1;

error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
# multi_accept on;
}

http {
# include /etc/nginx/mime.types;

access_log /var/log/nginx/access.log;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;

gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";

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;
proxy_max_temp_file_size 0;

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;

include /etc/nginx/sites-enabled/*;

}

我尝试将 Nginx 配置更改为别名,同样的事情。并尝试了所有可能的文件路径。谁能看出出了什么问题吗?或者给我一些指点?谢谢。

最佳答案

我不知道为什么我之前无法让它工作。也许 Nginx Wiki 文档是错误的?我是这样解决的:

在 nginx/sites-available/default 中:

   location /issues {
root /home/ubuntu;
internal;
}

在views.py中:

file_name = 'some.zip'
url = '/issues/' + file_name
response = HttpResponse()
response['Content-Type'] = ""
response['X-Accel-Redirect'] = url
return response

Zip 存档的完整路径是:/home/ubuntu/issues/some.zip

关于Nginx:X-Accel-Redirect 内部不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5547197/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com