nginx – 使用特定子域进行身份验证

前端之家收集整理的这篇文章主要介绍了nginx – 使用特定子域进行身份验证前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

基本上我正在尝试在访问子域的特定部分(dev.domain.com和pma.domain.com)时启用身份验证模块,他们都必须加载身份验证模块.我似乎无法弄清楚为什么我的Nginx配置文件无法正常工作.

在第二个服务器块中,您可以通过身份验证模块查看pma和dev,当我访问pma.domain.com或dev.domain.com时,我看不到我的浏览器显示的身份验证模块,也没有任何错误日志存储.

无论如何,我只需要一个修复程序来启用它们两个子域的身份验证,而不是整个重写我的Nginx配置文件.

  1. server {
  2. server_name domain.com;
  3. root /var/www/domain.com/www;
  4. index index.PHP index.htm index.html;
  5. error_page 404 /404.html;
  6. error_page 500 502 503 504 /50x.html;
  7. access_log /var/www/domain.com/logs/access.log;
  8. error_log /var/www/domain.com/logs/errors.log;
  9. error_page 404 /index.PHP;
  10. location ~ \.PHP$
  11. {
  12. fastcgi_pass 127.0.0.1:9000;
  13. fastcgi_index index.PHP;
  14. fastcgi_param SCRIPT_FILENAME /var/www/domain.com/www$fastcgi_script_name;
  15. include fastcgi_params;
  16. }
  17. }
  18. server {
  19. server_name ~^(.+)\.domain\.com$;
  20. set $file_path $1;
  21. root /var/www/domain.com/www/$file_path;
  22. index index.html index.PHP;
  23. access_log /var/www/domain.com/logs/access.log;
  24. error_log /var/www/domain.com/logs/errors.log;
  25. location /
  26. {
  27. try_files $uri /$uri /index.PHP?$args;
  28. }
  29. location ~ pma
  30. {
  31. auth_basic "Website development";
  32. auth_basic_user_file /var/www/domain.com/www/dev/authfile;
  33. }
  34. location ~ dev
  35. {
  36. auth_basic "Website development";
  37. auth_basic_user_file /var/www/domain.com/www/dev/authfile;
  38. }
  39. location ~ \.PHP$
  40. {
  41. fastcgi_pass 127.0.0.1:9000;
  42. fastcgi_index index.PHP;
  43. fastcgi_param SCRIPT_FILENAME /var/www/domain.com/www$fastcgi_script_name;
  44. include fastcgi_params;
  45. }
  46. }

任何人?

最佳答案
但是,您需要将这两个域拆分为单独的服务器块,并为其他服务器块保留通配符替换,或尝试使用下面的解决方法.

1.
“location”指令仅适用于URI,不适用于主机头

2.
如果你尝试做类似的事情

  1. if ($host ~ "(dev|pma).example.com" ) {
  2. auth_basic "Website development";
  3. auth_basic_user_file /var/www/domain.com/www/dev/authfile;
  4. }

然后你会得到一个

error Nginx: [emerg] “auth_basic” directive is not allowed here
in…..

因为auth_basic指令是无条件的

解决方法(未经过充分测试):

  1. if ($host ~ "(dev|pma).example.com" ) {
  2. return 555;
  3. }
  4. error_page 555 = @auth;
  5. location @auth {
  6. auth_basic "Website development";
  7. auth_basic_user_file /var/www/domain.com/www/dev/authfile;
  8. try_files $uri /$uri /index.PHP?$args;
  9. }

猜你在找的Nginx相关文章