Apache只允许授权用户访问文件

我有一个现有的网站,需要链接到我们的networking服务器上的PDF文件。 问题是,只有通过login到站点进行身份validation的用户才能够查看该文件。 我已经尝试了所有我能想到的,我只是不知道如何configurationApache来做我想做的事情。 要么它允许每个人访问该文件,要么没有人访问。

如何设置configuration,使得只有来自foo.bar.com的请求被授权才能从blah.baz.com获取文件?

更新在我的我的网站的configuration文件我现在有

<Directory "/usr/local/web/static/foo"> Order allow,deny Deny from all Allow from foo.bar.com </Directory> 

当我在铬控制台检查失败的请求时,我可以看到它包含

 Host:blah.baz.com Referer:http://foo.bar.com/ 

你可以通过这种方式使用mod_write来实现:

 RewriteEngine On RewriteCond %{HTTP_REFERER} !pippo\.com [NC] RewriteCond %{REQUEST_URI} ^/foo RewriteRule .* - [F] 

尝试伪造引用者:

 curl --referer http://www.pluto.com/bot.html http://localhost/foo <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>403 Forbidden</title> </head><body> <h1>Forbidden</h1> <p>You don't have permission to access / on this server.</p> </body></html> 

文件应该通过你的应用程序来提供,其内容从磁盘读取,然后传送给用户。 但是,由于使用这种方法会产生开销,所以现在很多人推荐使用Apache的mod_xsendfile。

总结一下,你将需要修改你的应用程序来做你想做的事情,但是使用mod_xsendfile可能会减less需要在代码上完成的改variables。

您可以参考以下链接作为指导: https : //codeutopia.net/blog/2009/03/06/sending-files-better-apache-mod_xsendfile-and-php/

我最终使用“Referer”环境variables来检查请求是否来自正确的网站。 所以对于我上面给出的例子,configuration看起来像:

 ServerName blah.baz.com DocumentRoot /usr/local/web/static SetEnvIf Referer foo.bar.com localreferer <Directory "/usr/local/web/static/foo"> Order deny,allow Deny from all Allow from env=localreferer </Directory>