【问题标题】:Symfony2 and NGINX - auth_basic allways asks for the passwordSymfony2 和 NGINX - auth_basic 总是要求输入密码
【发布时间】:2014-07-21 09:40:43
【问题描述】:

我有一个使用 Symfony2 框架编写并在 Nginx 服务器上运行的项目。 目标是使用 auth_basic 保护它。

我在 nginx 配置文件中做了什么:

location ~ \.php(/|$) {
auth_basic 'RESTRICTED ACCESS';
auth_basic_user_file /var/www/my.passwd;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;

}

但是,当我尝试访问该页面并填写用户名和密码时,它会一次又一次地问我同样的问题。

我在页面上有一些重定向:

server {
listen 80;
server_name example.com;
     rewrite ^ http://www.example.com$uri permanent;
}

server {
  listen 80;

  listen 443 default_server ssl;
  ssl_certificate ssl2013/myssl.crt;
  ssl_certificate_key ssl2013/myssl.key;
  keepalive_timeout 70;

  set $asset_dir /var/www/example.com/web/bundles/mdpimain;

  server_name www.example.com;
  root /var/www/example.com/web;

  # strip app.php/ prefix if it is present
  rewrite ^/app\.php/?(.*)$ /$1 permanent;

  # rewrite home
  rewrite ^/home/? / permanent;

  # remove trailing slash
  rewrite ^/(.*)/$ /$1 permanent;

  # remove index.php
  rewrite ^[/](.*)/index\.php$ /$1 permanent;

  # sitemap redirection
  rewrite ^/sitemap_(.*)$ /sitemap/$1 last;

  location / {
    index app.php;
    if (-f $request_filename) {
      break;
    }
    rewrite ^(.*)$ /app.php/$1 last;
  }

EDIT1.

另外一个细节:我使用的密码和用户都是ok的,因为nginx error.log中没有日志,所以出现了重定向问题。

【问题讨论】:

    标签: nginx http-authentication


    【解决方案1】:

    尝试检查$remote_user,如果为空,则返回403。

    编辑这对我有用。

    server {
        listen       80;
        server_name  www.example.com;
    
        auth_basic 'RESTRICTED ACCESS';
        auth_basic_user_file /var/web/my.passwd;
    
        set $ok "no";
        if ($remote_user ~ ^$) { break; }
        if ($remote_user != '') { set $ok "yes"; }
    
        if ($ok != "yes") {
            return 403;
        }
    
        # Path for static files
        root /var/web/public_html;
    
        location / {
            # try to serve file directly, fallback to app.php
            try_files $uri /app_dev.php$is_args$args;
        }
    
        location ~ ^/(app|app_dev|config)\.php(/|$) {
            fastcgi_pass        127.0.0.1:9000;
            fastcgi_split_path_info ^(.+\.php)(/.*)$;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param HTTPS off;
        }
    }
    

    【讨论】:

    • 什么也没发生,仍然是“受限访问”对话框并询问我用户名和密码
    • 你重新加载或重启了吗?也许尝试将 auth 部分移出 php 块?
    • 把它移到外面,同样的问题。
    • 而且,每次我必须填写 auth_basic 信息时,都会创建一个新的访问日志
    • 我刚试过,它对我有用。修改它以适合您的配置。
    猜你喜欢
    • 2014-05-30
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    • 2014-02-01
    • 2011-05-14
    • 2017-12-11
    相关资源
    最近更新 更多