加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

配置 – 在nginx和HTTP-Authentication下的服务静态文件

发布时间:2020-12-13 21:10:45 所属栏目:Nginx 来源:网络整理
导读:我在服务器上以测试模式部署了一个应用程序.通过HTTP身份验证将访问权限限制为选定的一组用户.这很好.问题是,如果我通过不同的location指令提供静态文件,nginx会为这些文件提供“Not Authorized”.我尝试了auth_basic,但没有骰子. 这是vhost conf: # Virtua

我在服务器上以测试模式部署了一个应用程序.通过HTTP身份验证将访问权限限制为选定的一组用户.这很好.问题是,如果我通过不同的’location’指令提供静态文件,nginx会为这些文件提供“Not Authorized”.我尝试了auth_basic,但没有骰子.

这是vhost 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;
    }
  }

}

有什么建议 ?

编辑:

我尝试将图像放在另一个子域下并为它配置一个单独的“服务器”块 – 没有任何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

在您不想进行身份验证的区域中.文档说这可以“覆盖从低级指令继承的操作”,但在这种情况下你的父指令(服务器块)不包含任何auth指令.

这可能是一个错误,当你试图禁用继承的身份验证而没有启用它时你打开它(可能它只是翻了一下?),但我建议从静态位置删除该语句.

就我所知,你在位置上使用^?并没有真正做任何事情,因为你在服务器块中没有任何正则表达式匹配.如果您在此处查看分辨率顺序说明:

http://wiki.nginx.org/NginxHttpCoreModule#location

你会看到使用^?可以防止检查正则表达式指定的位置.进一步在该文档页面上,您将看到对于文字匹配,nginx选择“最佳”匹配,其中“最佳”通常是与最长子字符串匹配的文字匹配.我不确定的是内部是什么

location /foo

比“更好”

location ^~ /foo

即使两者都是文字匹配,后者只是附加了一个提示.但是,由于您在当前设置中不需要^?位,请尝试删除它们,看看它是否清除了.当然,如果你给我们一个编辑的配置澄清,你的块中有?或?*匹配,这对你没有帮助.

如果这些都不起作用,那么你可以尝试移动

auth_basic

auth_basic_user_file

声明到服务器块.然后把你的

auth_basic off

声明到静态位置,它们将禁用您打开的内容.

==更新==

这个简单的例子适用于0.7.61以下:

server {

    listen 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;

    location /images {
        root /srv/www/sites/test.domain.com;
    }

    location / {
        root /srv/www/sites/test.domain.com;
        index index.html;

        auth_basic test;
        auth_basic_user_file /etc/nginx/auth/test.htpasswd;

        if ( -f $request_filename ) {
            expires 30d;
            break;
        }
    }

}

在站点目录中,我只有index.html和/ images中的图形文件.如果我在一个全新的浏览器会话中转到/images/filename.jpg,它会毫无错误地出现.如果然后转到站点的根目录,我会收到一个auth提示符.如果我然后返回到图像,我的日志显示经过身份验证的用户名(第一个请求显示“ – ”)

数据包跟踪显示,使用/images/filename.jpg的GET浏览器提供了身份验证信息(没有401质询).我假设nginx记录了一个提供的用户名,无论是否特别需要获取该文件(当然,由于挑战是针对/,浏览器必须提供用户输入的/images/filename.jpg凭据).

显然我的例子不包括代理,但基本功能就在那里.

我最初测试它的一个错误(你也做过)是在root指令中包含location块的子目录.注意/ images和/的根是如何相同的 – 在尝试检索文件时,nginx会在子目录中添加.

如果我为/ images块包含/ images创建了一个root参数,我会得到一个404试图从一个新的浏览器会话到达jpg(没有提示auth).我想知道你的代理设置是否导致请求被/ block捕获而不是(如我的例子中)掉落在配置的底部?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读