Nginx auth_basic 不适用于特定 url
Nginx auth_basic not working for a specific url
我想用密码保护我拥有的 URL 之一,我正在尝试使用:
location /about/payment {
auth_basic "secured site";
auth_basic_user_file /var/www/my.passwd;
}
问题是系统要求我提供用户名和密码。一旦输入正确的用户名和密码,我就会收到此日志的 404 错误:
*55268 open() "/var/www/mysite.com/deployment/web/about/payment" failed (2: No such file or directory), client: 172.16.0.53, server: ~^(?<branch>\w+)\.mysite\.dev$, request: "GET /about/payment HTTP/1.1", host: "deployment.mysite.dev"
编辑:
完整的 nginx 配置文件在这里
server {
listen 80;
access_log ...;
error_log ...;
server_name ~^(?<branch>\w+)\.mysite\.dev$ ~^(?<branch>\w+)\.mysite\.com$;
root /var/www/git/branches/mysite.com/$branch/web;
location /about/payment {
auth_basic "secured site";
auth_basic_user_file /var/www/mysite.passwd;
}
# strip app_eudev.php/ prefix if it is present
rewrite ^/app_eudev\.php/?(.*)$ / permanent;
# remove trailing slash
rewrite ^/(.*)/$ / permanent;
# sitemap rewrite
rewrite ^/sitemap_(.*)$ /sitemap/ last;
location / {
try_files $uri @symfonyapp;
}
location @symfonyapp {
rewrite ^(.*)$ /app_eudev.php/ last;
}
location /var/www/dms/ {
internal;
alias /var/www/dms/;
}
location @htmlimages {
root /var/www/dms/;
}
location ~ /html/.*\.(png|gif|jpg|pdf)$ {
root ...;
try_files $uri @htmlimages;
}
location /files {
root ...;
}
location /assets {
root ...;
}
location /img {
root ...
}
location ~ \.php(/|$) {
fastcgi_pass 127.0.0.1:9001;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
}
当 nginx 找到匹配的 location
将处理请求时,它会忽略可能匹配请求的任何其他 location
。
在您的情况下,在您添加身份验证之前,对 /about/payment
的请求由 location /
继续,最终将请求传递给 PHP。但是一旦你添加 location /about/payment
请求到 URL 将由这个没有特殊指令的位置处理,所以 nginx 将尝试提供静态文件。
您应该添加将请求传递给 PHP 的指令,在本例中它非常简单:
location /about/payment {
auth_basic "secured site";
auth_basic_user_file /var/www/my.passwd;
root ...;
try_files $uri @symfonyapp;
}
例如对于 Wordpress 网站,我想锁定 URL
location ~* /block-url/ {
auth_basic "Internal Staging Site";
auth_basic_user_file /etc/nginx/.htpasswd;
try_files $uri $uri/ /index.php?$args; # Most important!
}
我想用密码保护我拥有的 URL 之一,我正在尝试使用:
location /about/payment {
auth_basic "secured site";
auth_basic_user_file /var/www/my.passwd;
}
问题是系统要求我提供用户名和密码。一旦输入正确的用户名和密码,我就会收到此日志的 404 错误:
*55268 open() "/var/www/mysite.com/deployment/web/about/payment" failed (2: No such file or directory), client: 172.16.0.53, server: ~^(?<branch>\w+)\.mysite\.dev$, request: "GET /about/payment HTTP/1.1", host: "deployment.mysite.dev"
编辑:
完整的 nginx 配置文件在这里
server {
listen 80;
access_log ...;
error_log ...;
server_name ~^(?<branch>\w+)\.mysite\.dev$ ~^(?<branch>\w+)\.mysite\.com$;
root /var/www/git/branches/mysite.com/$branch/web;
location /about/payment {
auth_basic "secured site";
auth_basic_user_file /var/www/mysite.passwd;
}
# strip app_eudev.php/ prefix if it is present
rewrite ^/app_eudev\.php/?(.*)$ / permanent;
# remove trailing slash
rewrite ^/(.*)/$ / permanent;
# sitemap rewrite
rewrite ^/sitemap_(.*)$ /sitemap/ last;
location / {
try_files $uri @symfonyapp;
}
location @symfonyapp {
rewrite ^(.*)$ /app_eudev.php/ last;
}
location /var/www/dms/ {
internal;
alias /var/www/dms/;
}
location @htmlimages {
root /var/www/dms/;
}
location ~ /html/.*\.(png|gif|jpg|pdf)$ {
root ...;
try_files $uri @htmlimages;
}
location /files {
root ...;
}
location /assets {
root ...;
}
location /img {
root ...
}
location ~ \.php(/|$) {
fastcgi_pass 127.0.0.1:9001;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
}
当 nginx 找到匹配的 location
将处理请求时,它会忽略可能匹配请求的任何其他 location
。
在您的情况下,在您添加身份验证之前,对 /about/payment
的请求由 location /
继续,最终将请求传递给 PHP。但是一旦你添加 location /about/payment
请求到 URL 将由这个没有特殊指令的位置处理,所以 nginx 将尝试提供静态文件。
您应该添加将请求传递给 PHP 的指令,在本例中它非常简单:
location /about/payment {
auth_basic "secured site";
auth_basic_user_file /var/www/my.passwd;
root ...;
try_files $uri @symfonyapp;
}
例如对于 Wordpress 网站,我想锁定 URL
location ~* /block-url/ {
auth_basic "Internal Staging Site";
auth_basic_user_file /etc/nginx/.htpasswd;
try_files $uri $uri/ /index.php?$args; # Most important!
}