nginx重写& auth_basic(modx cms)

时间:2014-08-14 19:11:48

标签: .htaccess mod-rewrite nginx modx-revolution

我想用passwd保护root,并为modx cms进行urlfriendly重写。 只有重写有效,但密码不会被请求。

我的ispconfig nginx指令看起来像这样

location / {
    index index.html index.php
    auth_basic "Protected Area";
    auth_basic_user_file /var/www/clients/client21/web22/web/htpasswd;
    client_max_body_size 0;
    if (!-e $request_filename) {
            rewrite ^/(.*)$ /index.php?q=$1 last;
    }
}

location ~ /\.ht {
    deny  all;
}

location ~ \.php$ {
    include /etc/nginx/fastcgi_params;
    fastcgi_pass  unix:/var/run/php5-fpm.sock;
    fastcgi_read_timeout 600; 
    fastcgi_buffers 16 16k;
    fastcgi_buffer_size 32k;
    client_max_body_size 0;
}

似乎auth_basic不会被重写规则执行或覆盖。有人有个主意吗?

很多人看到这个问题之后但是我没有在这里得到答案我发布问题的解决方案。问题是基本的auth需要在php指令中完成。

location / {
    index index.html index.php
    client_max_body_size 0;
    if (!-e $request_filename) {
            rewrite ^/(.*)$ /index.php?q=$1 last;
    }
}

location ~ /\.ht {
    deny  all;
}

location ~ \.php$ {
    auth_basic "Protected Area";
    auth_basic_user_file /var/www/clients/client21/web22/web/htpasswd;
    include /etc/nginx/fastcgi_params;
    fastcgi_pass  unix:/var/run/php5-fpm.sock;
    fastcgi_read_timeout 600;
    fastcgi_buffers 16 16k;
    fastcgi_buffer_size 32k;
    client_max_body_size 0;
}

1 个答案:

答案 0 :(得分:0)

尝试这套规则。没有PHP指令并利用FURLS。

location / { 
  auth_basic "Restricted"; 
  # Add path to .htpasswd file
  auth_basic_user_file /var/www/clients/client21/web22/web/.htpasswd;
  # if the protected location is modx, then ...
  try_files $uri $uri/ @modx-rewrite;
  # otherwise, if static files, you’d use
  # try_files $uri $uri/ =404;
  # This line should always be placed at the end
  try_files $uri $uri/ @modx-rewrite; 
}