我有一个应用程序部署在服务器上的testing模式。 通过HTTPauthentication将访问权限限制在一组选定的用户中。 这工作正常。 问题是,如果我通过不同的“位置”指令提供静态文件,nginx给了我一个“没有授权”的文件。 我尝试auth_basicclosures,但没有骰子。
这是虚拟主机conf:
# Virtual Host upstream appname { server 127.0.0.1:4000; server 127.0.0.1:4001; server 127.0.0.1:4002; } server { listen 80; server_name appname.domain.com; # write app specific log # make sure you create this file in your log directory before running behind nginx access_log /home/apps/appname/shared/log/nginx.log main; # let nginx serve static files directly # images location ^~/images { auth_basic off; root /home/apps/appname/current/public/images; } location ^~/covers { auth_basic off; root /home/apps/covers; } # # javascript location ^~/javascripts { auth_basic off; root /home/apps/appname/current/public/javascripts; } # # css location ^~/stylesheets { auth_basic off; root /home/apps/appname/current/public/style; } # Push all other requests to Merb location / { # needed to forward user's IP address to merb proxy_set_header X-Real-IP $remote_addr; auth_basic "domains"; auth_basic_user_file htpasswd; if (!-f $request_filename) { proxy_pass http://appname; break; } } }
有什么build议么 ?
编辑:
我试图把图像放在另一个子域下,并为它configuration一个单独的“服务器”块 – 没有任何http_auth。 但它仍然给了我一个403的形象禁止! 这是我补充说的:
server { listen 80; server_name images.domain.com; access_log /home/apps/appname/shared/log/nginx_images.log main; error_log /home/apps/appname/shared/log/nginx_images_error.log; error_page 500 502 503 504 /500.html; location = /500.html { root /home/apps/www/http_error_codes; } location /images { root /home/apps/appname/current/public/images; } location /covers { root /home/apps/covers; } open_file_cache max=1000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; }
我也试图打开一个新的浏览器,直接从images.domain.com访问一个图像文件,但它仍然说403禁止!
我不确定你需要
auth_basic off
在您不想进行身份validation的区域。 文档说,这可以“覆盖可从低级指令inheritance的操作”,但是在这种情况下,您的父指令(服务器块)不包含任何auth指令。
这可能是一个错误,当你试图禁用inheritance的身份validation,而不是已经被启用,你打开它(也许它只是翻了一下?),但我build议是从您的静态位置删除该语句。
我可以告诉你在地点上使用^〜的方式并没有什么作用,因为在服务器块中没有任何正则expression式匹配。 如果您在这里查看分辨率的订单说明:
http://wiki.nginx.org/NginxHttpCoreModule#location
你会看到使用^〜来防止检查正则expression式指定的位置。 再往下看文档页面,你会发现对于字面匹配,nginxselect“最好”匹配,其中“最好”通常是与最长的子string匹配的字面匹配。 我不确定的是内部
location /foo
比“更好”
location ^~ /foo
即使两者都是文字匹配,后者只是附加提示。 但是因为你不需要你当前的设置中的^〜位,请尝试删除它们,看看它是否清除了事情。 当然,如果你已经给了我们一个澄清的编辑configuration,并且你在块中有〜或〜*匹配,这对你不会有什么帮助。
如果没有任何作品,那么你可以尝试移动
auth_basic
和
auth_basic_user_file
向服务器块报告。 然后把你的
auth_basic off
声明到静态位置,在那里他们将禁用你已经打开的东西。
==更新==
这个简单的例子适用于我在0.7.61下:
服务器{
听80;
server_name test.domain.com;
access_log /var/log/nginx/sites/test.domain.com/access.log;
error_log /var/log/nginx/sites/test.domain.com/error.log;
位置/图片{
根/srv/www/sites/test.domain.com;
}
位置 / {
根/srv/www/sites/test.domain.com;
index index.html;
auth_basictesting;
auth_basic_user_file /etc/nginx/auth/test.htpasswd;
if(-f $ request_filename){
到期30天;
打破;
}
}
}
在网站目录中,我拥有的是index.html和/ images中的graphics文件。 如果在全新的浏览器会话中转到/images/filename.jpg,则会出现错误。 如果再转到网站的根目录,我会得到一个auth提示符。 如果我然后返回到图像,我的日志显示authentication的用户名(第一个请求显示“ – ”)
数据包跟踪显示,身份validation信息是由浏览器提供的GET /images/filename.jpg(没有401的挑战)。 我假设nginxlogging一个提供的用户名,不pipe它是否被特别需要来获取文件(当然,因为挑战是反对/,浏览器必须提供用户input的凭据/images/filename.jpg)。
显然我的例子不包括代理,但基本function在那里。
我最初testing的一个错误(你也这样做)是在根指令中包含位置块的子目录。 注意/ images和/的根目录是相同的 – 当试图检索文件时,nginx将添加到子目录中。
如果我为/ images块include / images创build根参数,我会得到一个404尝试从新的浏览器会话中获得jpg(无需提示进行身份validation)。 我想知道你的代理设置是否导致请求被/阻塞而不是(如我的例子)通过configuration的底部?
尝试将你的文件chown给运行nginx的用户(www-data我想)