大网数据广告活动
服务器运维问答

Nginx - Allow access to folder directory only by referring URL

· 52CE 我爱测

front-end web developer here. Sorry in advance. My company wants to store new builds of our software on our web server, running Nginx, provided by WP Engine . The file path for these builds would be company.com/downloads/file.zip…

问题原文

原帖链接 · Jesse Rogers · CC BY-SA 3.0

front-end web developer here. Sorry in advance.

My company wants to store new builds of our software on our web server, running Nginx, provided by WP Engine.

The file path for these builds would be company.com/downloads/file.zip. We want to restrict access to files in this folder unless they come from a specific referring URL that sits behind a login gate for our customers.

  • If user clicks link to any file inside /downloads/ from allowed referrer, user gets file
  • If user clicks link from any other source, return 404 or redirect to homepage
  • If empty referrer or direct access attempt, return 404 or redirect to homepage

I've found a resource on preventing hotlinking for images (we'll be dealing with .zip files), which might work for me, but I need help with the syntax of this language. There's probably a bunch wrong with it.

location ~ /downloads/$ {
valid_referers none blocked ~.allowed_domain.com;
if ($invalid_referer) {
return 404;
}
}

WP Engine doesn't allow me to add Nginx code myself, so I'll have to send them the code I want them to implement. If anyone knows how to do this and can help me out, I'd really appreciate it!

已采纳解答

原帖链接 · Věroš K. · CC BY-SA 3.0

Other possibility is to use X-Accel header. I don't know your use case, so it may not be a good fit.

X-Accel is nginx specific header. You can issue that header in your PHP script - like header("X-Accel-Redirect: /private-downloads/magic.iso") When nginx recognize this header, it will server content of the file magic.iso located in /data/private-downloads.

So your task is to prepare download.php which check authorization and respond either with X-Accel header or redirect to login screen.

Definition of /private-downloads/ in nginx.conf

location /private-downloads/ {
    internal;    # this
    alias /data/private-downloads;
    # this should be located outside of HTTP server root
}

For more details, plase check x-accel questions on serverfault. 1

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-02-22)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。