设为首页 收藏本站
查看: 854|回复: 0

[经验分享] Nginx 的两种认证方式

[复制链接]

尚未签到

发表于 2017-12-22 11:00:20 | 显示全部楼层 |阅读模式
  简介:
  今天来研究一下 Nginx 的两种认证方式。
  1、auth_basic 本机认证
  2、ngx_http_auth_request_module 第三方认证
  一、安装 Nginx
  

shell > sh auto.sh install nginx  

  
install_nginx(){
yum -y install gcc gcc-c++ wget make pcre-devel zlib-devel openssl-devel  

id www-data > /dev/null 2>&1 || useradd -r -s /sbin/nologin www-data  

  cd
/usr/local/src; wget -qc http://nginx.org/download/nginx-1.10.2.tar.gz || exit 9  

  tar zxf nginx-1.10.2.tar.gz; cd nginx-1.10.2
  ./configure --prefix=/usr/local/nginx-1.10.2 \
  --with-http_dav_module \
  --with-http_ssl_module \
  --with-http_realip_module \
  --with-http_gzip_static_module \
  --with-http_stub_status_module \
  --with-http_degradation_module \
  --with-http_auth_request_module && make && make install
  mkdir /usr/local/nginx-1.10.2/conf/vhost; mkdir -p /data/logs/nginx
  mkdir -p /data/git-webroot/{api-htdocs,web-htdocs} && chown -R www-data.www-data /data/git-webroot
  echo "/usr/local/nginx-1.10.2/sbin/nginx" >> /etc/rc.local
  
}
  

  二、auth_basic 本机认证
  

shell > yum -y install httpd-tools  # 安装 htpasswd 工具  

  
shell
> cd /usr/local/nginx-1.10.2/conf  

  
shell
> htpasswd -c pass.db wang  # 创建认证用户 wang 并输入密码,添加用户时输入 htpasswd pass.db username  

  
shell
> vim /usr/local/nginx-1.10.2/conf/vhost/local.conf  

  
server {
  listen      
80;  server_name  local.server.com;
  auth_basic
"User Authentication";  auth_basic_user_file
/usr/local/nginx-1.10.2/conf/pass.db;  location
/ {  root   
/data/www;  index  index.html;
  }
  
}
  

  # 这样就实现了本机认证,需要维护 pass.db 文件
  三、ngx_http_auth_request_module 第三方认证
  # 编译 Nginx 时需要添加该模块 --with-http_auth_request_module
  # 该模块可以将客户端输入的用户名、密码 username:password 通过 Base64 编码后写入 Request Headers 中
  # 例如:wang:wang -> Authorization:Basic d2FuZzp3YW5n=
  # 然后通过第三方程序解码后跟数据库中用户名、密码进行比较,Nginx 服务器通过 header 的返回状态判断是否认证通过。
  

shell > vim /usr/local/nginx-1.10.2/conf/vhost/local.conf  # 我们先来编辑本机配置文件,也就是用户直接访问的域名  

  
server {
  listen
80;  server_name local.server.com;
  

  auth_request
/auth;  

  location
/ {  root   html;
  index  index.html;
  }
  

  location
/auth {  proxy_pass http:
//auth.server.com/HttpBasicAuthenticate.php;  
        proxy_pass_request_body off;
  proxy_set_header Content-Length "";
  proxy_set_header X-Original-URI $request_uri;
  }
  
}
  

  # auth_request /auth; # 启用认证
  # proxy_pass http://auth.server.com/HttpBasicAuthenticate.php;  # 认证服务器地址
  # 参考地址:http://nginx.org/en/docs/http/ngx_http_auth_request_module.html
  

shell > vim /usr/local/nginx-1.10.2/conf/vhost/auth.conf  # 这是第三方认证服务器,认证逻辑使用的 PHP 代码  

  
server {
  listen      
80;  server_name  auth.server.com;
  

  location
~ \.php$ {  fastcgi_pass   
127.0.0.1:9000;  fastcgi_index  index.php;
  fastcgi_param  SCRIPT_FILENAME  
/usr/local/nginx-1.10.2/html$fastcgi_script_name;  include        fastcgi_params;
  }
  
}
  

  
shell
> vim /usr/local/nginx-1.10.2/html/HttpBasicAuthenticate.php  

  

<?php  

  

if(isset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])){  $username
= $_SERVER['PHP_AUTH_USER'];  $password
= $_SERVER['PHP_AUTH_PW'];  

if ($username == 'wang' && $password == '123456'){  return
true;  }
  
}
  

  
header(
'WWW-Authenticate: Basic realm="Git Server"');  
header(
'HTTP/1.0 401 Unauthorized');  

  

?>  

  # 用户访问 local.server.com 弹出框中输入的用户名、密码保存在 $_SERVER 变量中
  # 中间 if 段,只做演示用,工作中应该是拿用户输入的用户名、密码跟数据库中的数据做比较
  # 用户访问 local.server.com 就会去 auth.servere.com 做用户认证,认证通过后继续访问 local.server.com
  # 目前 Nginx 的第三方认证,工作中自己搭建的 git + gitweb 在使用中,配置文件如下:( 认证逻辑大家使用自己喜欢的语言编写即可 )
  

shell > vim /usr/local/nginx-1.10.2/conf/vhost/git.server.com  

  
server {
  listen      
80;  server_name git.server.com;
  root        
/usr/local/share/gitweb;  

  client_max_body_size 50m;
  

  #auth_basic
"Git User Authentication";  #auth_basic_user_file
/usr/local/nginx-1.10.2/conf/pass.db;  

  auth_request
/auth;  

  location
~ ^.*\.git/objects/([0-9a-f]+/[0-9a-f]+|pack/pack-[0-9a-f]+.(pack|idx))$ {  root
/data/git;  }
  

  location
~ /.*\.git/(HEAD|info/refs|objects/info/.*|git-(upload|receive)-pack)$ {  root         
/data/git;  fastcgi_pass  unix:
/var/run/fcgiwrap.socket;  fastcgi_connect_timeout 24h;
  fastcgi_read_timeout 24h;
  fastcgi_send_timeout 24h;
  fastcgi_param SCRIPT_FILENAME     
/usr/local/libexec/git-core/git-http-backend;  fastcgi_param PATH_INFO           $uri;
  fastcgi_param GIT_HTTP_EXPORT_ALL
"";  fastcgi_param GIT_PROJECT_ROOT   
/data/git;  fastcgi_param REMOTE_USER $remote_user;
  include fastcgi_params;
  }
  

  try_files $uri @gitweb;
  

  location @gitweb {
  fastcgi_pass  unix:
/var/run/fcgiwrap.socket;  fastcgi_param GITWEB_CONFIG   
/etc/gitweb.conf;  fastcgi_param SCRIPT_FILENAME  
/usr/local/share/gitweb/gitweb.cgi;  fastcgi_param PATH_INFO        $uri;
  include fastcgi_params;
  }
  

  location
/auth {  proxy_pass http:
//auth.server.com/HttpBasicAuthenticate.php;  
        proxy_pass_request_body off;
  proxy_set_header Content-Length "";
  proxy_set_header X-Original-URI $request_uri;
  }
  
}
  

  # End

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-426784-1-1.html 上篇帖子: Nginx 日志分析及性能排查 下篇帖子: 跨过Nginx上基于uWSGI部署Django项目的坑
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表