I have a problem with nginx when migrating from apache.
all uris are rewrited to /index.php, and ^/admin uri should be protected by basic auth.
when with apache, I configured it as below:
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php [L]
<Location ~ "^/admin">
AuthType Basic
AuthName "Restricted"
AuthUserFile /etc/apache2/httpd.passwd
Require valid-user
</Location>
this is nginx configuration but it does not work:
if (!-f $request_filename) {
rewrite ^ /index.php last;
}
location ~ \.php$ {
include fastcgi.conf;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
location ~* /admin/ {
auth_basic "admin login";
auth_basic_user_file /etc/nginx/httpd.passwd;
}
how can i do it in nginx?
Translating apache configurations are rarely a 1:1 conversion into nginx directives. The rewrite phase is executed before the access phase, and server-level rewrite directives are executed before location selection even happens, so the if/rewrite is run before the admin location is even considered. I would suggest something like this: