大网数据广告活动
服务器运维问答

nginx location excluding a particular request

· 52CE 我爱测

On WordPress admin, I have basic http authentication for nginx by using this inside nginx config: location ^~ /wp-login.php { auth_basic "Restricted Area: WordPress"; auth_basic_user_file /etc/nginx/wp.htpasswd; include /etc/ngin…

问题原文

原帖链接 · Ashfame · CC BY-SA 3.0

On WordPress admin, I have basic http authentication for nginx by using this inside nginx config:

location ^~ /wp-login.php {
        auth_basic           "Restricted Area: WordPress";
        auth_basic_user_file /etc/nginx/wp.htpasswd;
        include /etc/nginx/fastcgi.conf;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
}

Now this works but I need to add an exclusion to it for wp-login.php?action=postpass

I learnt from this article how nginx chooses which location block will be used to serve a particular request. ^~ being used to override any rules that we might have, I think I need to specify in a manner by which there is a regex rule and an exact rule, which should do the desired job.

This is what makes most sense to me, but it doesn't work:

location = /wp-login.php?action=postpass {
        include /etc/nginx/fastcgi.conf;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
}

location ~* /wp-login.php {
        auth_basic           "Restricted Area: WordPress";
        auth_basic_user_file /etc/nginx/wp.htpasswd;
        include /etc/nginx/fastcgi.conf;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
}

Also, tried other random combinations but no luck at all. Important to mention that basic auth should be in use when the request url is either just wp-login.php or anything behind it in URL, except ?action=postpass

Please advice! Thanks in advance!

已采纳解答

原帖链接 · Mridul Aggarwal · CC BY-SA 3.0

something like this should work

set $shouldauth "Restricted Area: WordPress";
if ($arg_action ~* postpass) {
  set $shouldauth off;
}
location ~* /wp-login.php {
  auth_basic           $shouldauth;
  auth_basic_user_file /etc/nginx/wp.htpasswd;
  include /etc/nginx/fastcgi.conf;
  fastcgi_pass unix:/var/run/php5-fpm.sock;
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-09-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。