How can I add basic auth to an nginx configuration with rewrite

Viewed 4782

I have the following nginx configuration I would like to add basic authentication to this configuration to this.

upstream phpfcgi {
        server 127.0.0.1:7777;
# server unix:/var/run/php5-fpm.sock; #for PHP-FPM running on UNIX socket
}
server{
  listen 80;
  server_name qu.abc.com;
  root /home/qu/website/current/web;
  location / {
# try to serve file directly, fallback to rewrite
          try_files $uri @rewriteapp;

  }

  location @rewriteapp {
  # rewrite all to app.php
          rewrite ^(.*)$ /app.php/$1 last;
  }

  location ~ ^/(app|app_dev|config)\.php(/|$) {
          fastcgi_pass phpfcgi;
          fastcgi_split_path_info ^(.+\.php)(/.*)$;
          include fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          fastcgi_param HTTPS off;
  }

  error_log /var/log/nginx/project_error.log;
  access_log /var/log/nginx/project_access.log;
}

I have edit the location block to this

location / {
# try to serve file directly, fallback to rewrite
          auth_basic "Restricted";
          auth_basic_user_file /home/qu/website/current/web/.htpassword;
          try_files $uri @rewriteapp;

  }

But this one doesn't work with rewrite. Could somebody help on this.

1 Answers
Related