Nginx密码保护整个端口号8081

我正在尝试密码保护我的网站上的整个端口 – https://domain.com:8081和http://domain.com:8081

我试图编辑/etc/nginx/sites-enabled/domain.com.vhost在服务器块中添加以下内容无效(我从这个链接获得 ,除了链接是关于密码保护目录,而不是端口):

location ^~ :8081 { auth_basic "Restricted Area"; auth_basic_user_file conf/htpasswd; } 

我也试过“位置:8081”,但是这也没有奏效。

我怎样才能密码保护端口8081(或任何其他港口我渴望这个问题)?

如果它有什么区别,我使用Nginx 1.4.6的Ubuntu 14.04.1 LTS。

谢谢。

[编辑]

在实施Nathan的解决scheme时,当进入https://domain.com:8081/phpmyadmin/(SSL )时,它会提示input用户名和密码,但会给我一个“500内部服务器错误”页面。 以下是Nginx错误日志中显示的内容:

 [crit] 3390#0: *154 open() "/etc/nginx/conf/htpasswd" failed (13: Permission denied), client: 152.35.52.108, server: domain.com, request: "GET /phpmyadmin/ HTTP/1.1", host: "domain.com:8081" 

当去http://domain.com:8081/phpmyadmin/ (非SSL),它给了我“400错误的请求普通的HTTP请求被发送到HTTPS端口”。 在错误日志上没有任何注册; 相反,Nginx的访问日志中会出现以下内容:

 "GET /phpmyadmin/ HTTP/1.1" 40.1" 400 279 "-" "[user agent]" 

我假设你有一个单独的server {为这个端口块…所以你只是保护在该服务器块内的/目录:

 location / { auth_basic "Restricted Area"; auth_basic_user_file conf/htpasswd; } 

所以:

 server { listen 8081; server_name whateveryouwant; root /path/to/root/folder; location / { auth_basic "Restricted Area"; auth_basic_user_file conf/htpasswd; } } 

我没有testing过这个,但是看起来应该如此。