我正在尝试禁用我网站上某些端点的基本身份验证,因为某些第三方服务会向该站点发送请求。
我有两个 EC2 实例和一个 Application Load Balancer (ALB) 在它们后面。这是我的 nginx 配置文件。如您所见,我在服务器块中启用了身份验证并为某些端点禁用了它。但是,当我尝试访问某些端点时,例如 https://website.domain/api/providers/bgaming/rollback,我仍然被提示输入用户名和密码,而我不应该这样做。
upstream fpm_backend {
server unix:/run/php/php8.1-fpm.sock;
keepalive 256;
}
server {
listen 80 default_server;
server_name _;
root /home/xxxx/xxxx/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
error_page 404 /index.php;
auth_basic "Restricted access";
auth_basic_user_file /etc/nginx/.htpasswd;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location /api/provider/aleaplay/callback/transactions {
auth_basic off;
}
location /api/provider/aleaplay/callback/players/ {
auth_basic off;
}
location /callback {
auth_basic off;
}
location /api/providers/bgaming/play {
auth_basic off;
}
location /api/providers/bgaming/rollback {
auth_basic off;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
location /web.config {
return 404;
}
location ~ \.php$ {
fastcgi_pass fpm_backend;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
client_max_body_size 20M;
}
查看您的配置,一切最终都由根
index.php
文件处理。 /index.php
URI 由 location ~ \.php$
块处理并需要身份验证。
要禁用
/api/providers/bgaming/rollback
的身份验证,您需要使用 auth_basic off
and 在同一位置块中处理整个请求。
例如:
location ^~ /api/providers/bgaming/rollback {
auth_basic off;
try_files /index.php =404;
fastcgi_pass fpm_backend;
include fastcgi_params;
}
^~
运算符使该位置 优先(在这种特殊情况下可能不需要)。 try_files
语句将请求更改为 index.php
但由于它 not last 参数,请求继续在同一位置块中处理。最后两个语句将请求作为 PHP 文件处理。