【问题标题】:Always 'proxy_pass' and require 'auth_basic' on requests started with '/admin'. How to write the nginx config?对于以“/admin”开头的请求,始终使用“proxy_pass”并要求“auth_basic”。如何编写 nginx 配置?
【发布时间】:2011-06-13 03:30:18
【问题描述】:

这就是我所拥有的。问题是代理设置重复。有什么更好的方法?

proxy_redirect off;

location / {
  if (!-f $request_filename) {
    proxy_pass http://backend;
  }
}

location /admin {
  auth_basic 'Restricted';
  auth_basic_user_file passwd;

  if (!-f $request_filename) {
    proxy_pass http://backend;
  }
}

【问题讨论】:

    标签: nginx


    【解决方案1】:

    试试看:

    Location / {
       proxy_redirect off;
       if (!-f $request_filename) {
           proxy_pass http://backend;
       }
    
       location ~ ^/admin {
           auth_basic 'Restricted';
           auth_basic_user_file passwd;
       }
    
    
    }
    

    【讨论】:

    • 不起作用。 /admin 现在不会通过 proxy_pass。我也试过在if之前放置location ~ ^/admin,也不起作用。
    【解决方案2】:

    这可能会有所帮助。

    proxy_redirect off;
    
    location / {
      try_files $uri @proxy;
    }
    
    location @proxy {
      if ( $uri ~* "^/admin/? ) {
        auth_basic 'Restricted';
        auth_basic_user_file passwd;
      }
      proxy_pass http://backend;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-10-30
      • 2011-08-15
      • 2021-07-10
      • 1970-01-01
      • 1970-01-01
      • 2015-03-16
      • 2018-02-27
      • 2012-10-02
      • 2016-11-02
      相关资源
      最近更新 更多