使用Nginx的X-Accel-Redirect头来提供受保护的文件

我试图在我的nginx.conf中使用这个指令来提供受保护的文件:

location /secure/ { internal; alias /home/ldr/webapps/nginx/app/secure/; } 

我通过path的forms:“/ myfile.doc”

该文件的path是:/home/ldr/webapps/nginx/app/secure/myfile.doc

当我访问“http://myserver/secure/myfile.doc”(在http后面插入空格以停止ServerFault将其转换为链接)

我已经尝试了跟踪/closures位置指令,这没有什么区别。

两个问题:

  1. 我如何解决它!
  2. 我怎样才能debugging这样的问题呢? 我怎样才能让Nginx报告它正在寻找哪个path? error.log显示什么都没有,access.log只是告诉我哪个url被请求 – 这是我已经知道的位! 没有任何反馈的随机尝试是没有意思的。

这是我的整个nginx.conf:

  daemon off; worker_processes 2; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; server { listen 21534; server_name my.server.com; client_max_body_size 5m; location /media/ { alias /home/ldr/webapps/nginx/app/media/; } location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; fastcgi_pass unix:/home/ldr/webapps/nginx/app/myproject/django.sock; fastcgi_pass_header Authorization; fastcgi_hide_header X-Accel-Redirect; fastcgi_hide_header X-Sendfile; fastcgi_intercept_errors off; include fastcgi_params; } location /secure { internal; alias /home/ldr/webapps/nginx/app/secure/; } } } 

编辑:

我在这里尝试一些build议

所以我试过了:

 location /secure/ { internal; alias /home/ldr/webapps/nginx/app/; } 

无论是否有位置上的斜线。

我也试过在“location /”指令之前移动这个块。

我链接到的页面在'location'之后有^〜:

 location ^~ /secure/ { ...etc... 

不知道这意味着什么,但它也不起作用!

好像你误会了x-accel-redirect的意思。 这个function的要点是允许你的后端处理authentication,日志logging等等,然后把文件的提交交给Nginx。

这意味着你不直接访问这个URI,但是你的后端发出一个x-accel-redirect头文件,而Nginx会把这个文件作为后端处理,这样就可以把它释放出来做其他的事情。

如果这实际上是你说的话, I'm passing in paths in the form: "/myfile.doc"那么你的URI不匹配的位置。 你实际上给Nginx一个新的请求,所以它会做一个正常的位置匹配。 因此,如果你想要它匹配location /secure/你需要传递一个以/secure/开头的URI,例如/secure/myfile.doc将提供/home/ldr/webapps/nginx/app/secure/myfile.doc

尝试使用根而不是别名。

 location /secure/ { internal; root /home/ldr/webapps/nginx/app/; }