简介:

今天来研究一下 Nginx 的两种认证方式。

1、auth_basic 本机认证
2、ngx_http_auth_request_module 第三方认证

一、安装 Nginx

  1. shell > sh auto.sh install nginx
  2.  
  3. install_nginx(){
  4. yum -y install gcc gcc-c++ wget make pcre-devel zlib-devel openssl-devel
  5.  
  6. id www-data > /dev/null >& || useradd -r -s /sbin/nologin www-data
  7.  
  8. cd /usr/local/src; wget -qc http://nginx.org/download/nginx-1.10.2.tar.gz || exit 9
  9.  
  10. tar zxf nginx-1.10..tar.gz; cd nginx-1.10.
  11. ./configure --prefix=/usr/local/nginx-1.10. \
  12. --with-http_dav_module \
  13. --with-http_ssl_module \
  14. --with-http_realip_module \
  15. --with-http_gzip_static_module \
  16. --with-http_stub_status_module \
  17. --with-http_degradation_module \
  18. --with-http_auth_request_module && make && make install
  19. mkdir /usr/local/nginx-1.10./conf/vhost; mkdir -p /data/logs/nginx
  20. mkdir -p /data/git-webroot/{api-htdocs,web-htdocs} && chown -R www-data.www-data /data/git-webroot
  21. echo "/usr/local/nginx-1.10.2/sbin/nginx" >> /etc/rc.local
  22. }

二、auth_basic 本机认证

  1. shell > yum -y install httpd-tools # 安装 htpasswd 工具
  2.  
  3. shell > cd /usr/local/nginx-1.10./conf
  4.  
  5. shell > htpasswd -c pass.db wang # 创建认证用户 wang 并输入密码,添加用户时输入 htpasswd pass.db username
  6.  
  7. shell > vim /usr/local/nginx-1.10./conf/vhost/local.conf
  8.  
  9. server {
  10. listen ;
  11. server_name local.server.com;
  12.  
  13. auth_basic "User Authentication";
  14. auth_basic_user_file /usr/local/nginx-1.10./conf/pass.db;
  15.  
  16. location / {
  17. root /data/www;
  18. index index.html;
  19. }
  20. }

# 这样就实现了本机认证,需要维护 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 的返回状态判断是否认证通过。

  1. shell > vim /usr/local/nginx-1.10./conf/vhost/local.conf # 我们先来编辑本机配置文件,也就是用户直接访问的域名
  2.  
  3. server {
  4. listen ;
  5. server_name local.server.com;
  6.  
  7. auth_request /auth;
  8.  
  9. location / {
  10. root html;
  11. index index.html;
  12. }
  13.  
  14. location /auth {
  15. proxy_pass http://auth.server.com/HttpBasicAuthenticate.php;
  16. proxy_pass_request_body off;
  17. proxy_set_header Content-Length "";
  18. proxy_set_header X-Original-URI $request_uri;
  19. }
  20. }

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

  1. shell > vim /usr/local/nginx-1.10./conf/vhost/auth.conf # 这是第三方认证服务器,认证逻辑使用的 PHP 代码
  2.  
  3. server {
  4. listen ;
  5. server_name auth.server.com;
  6.  
  7. location ~ \.php$ {
  8. fastcgi_pass 127.0.0.1:;
  9. fastcgi_index index.php;
  10. fastcgi_param SCRIPT_FILENAME /usr/local/nginx-1.10./html$fastcgi_script_name;
  11. include fastcgi_params;
  12. }
  13. }
  14.  
  15. shell > vim /usr/local/nginx-1.10./html/HttpBasicAuthenticate.php
  16.  
  17. <?php
  18.  
  19. if(isset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])){
  20. $username = $_SERVER['PHP_AUTH_USER'];
  21. $password = $_SERVER['PHP_AUTH_PW'];
  22.  
  23. if ($username == 'wang' && $password == ''){
  24. return true;
  25. }
  26. }
  27.  
  28. header('WWW-Authenticate: Basic realm="Git Server"');
  29. header('HTTP/1.0 401 Unauthorized');
  30.  
  31. ?>

# 用户访问 local.server.com 弹出框中输入的用户名、密码保存在 $_SERVER 变量中
# 中间 if 段,只做演示用,工作中应该是拿用户输入的用户名、密码跟数据库中的数据做比较
# 用户访问 local.server.com 就会去 auth.servere.com 做用户认证,认证通过后继续访问 local.server.com

# 目前 Nginx 的第三方认证,工作中自己搭建的 git + gitweb 在使用中,配置文件如下:( 认证逻辑大家使用自己喜欢的语言编写即可 )

  1. shell > vim /usr/local/nginx-1.10./conf/vhost/git.server.com
  2.  
  3. server {
  4. listen ;
  5. server_name git.server.com;
  6. root /usr/local/share/gitweb;
  7.  
  8. client_max_body_size 50m;
  9.  
  10. #auth_basic "Git User Authentication";
  11. #auth_basic_user_file /usr/local/nginx-1.10./conf/pass.db;
  12.  
  13. auth_request /auth;
  14.  
  15. location ~ ^.*\.git/objects/([-9a-f]+/[-9a-f]+|pack/pack-[-9a-f]+.(pack|idx))$ {
  16. root /data/git;
  17. }
  18.  
  19. location ~ /.*\.git/(HEAD|info/refs|objects/info/.*|git-(upload|receive)-pack)$ {
  20. root /data/git;
  21. fastcgi_pass unix:/var/run/fcgiwrap.socket;
  22. fastcgi_connect_timeout 24h;
  23. fastcgi_read_timeout 24h;
  24. fastcgi_send_timeout 24h;
  25. fastcgi_param SCRIPT_FILENAME /usr/local/libexec/git-core/git-http-backend;
  26. fastcgi_param PATH_INFO $uri;
  27. fastcgi_param GIT_HTTP_EXPORT_ALL "";
  28. fastcgi_param GIT_PROJECT_ROOT /data/git;
  29. fastcgi_param REMOTE_USER $remote_user;
  30. include fastcgi_params;
  31. }
  32.  
  33. try_files $uri @gitweb;
  34.  
  35. location @gitweb {
  36. fastcgi_pass unix:/var/run/fcgiwrap.socket;
  37. fastcgi_param GITWEB_CONFIG /etc/gitweb.conf;
  38. fastcgi_param SCRIPT_FILENAME /usr/local/share/gitweb/gitweb.cgi;
  39. fastcgi_param PATH_INFO $uri;
  40. include fastcgi_params;
  41. }
  42.  
  43. location /auth {
  44. proxy_pass http://auth.server.com/HttpBasicAuthenticate.php;
  45. proxy_pass_request_body off;
  46. proxy_set_header Content-Length "";
  47. proxy_set_header X-Original-URI $request_uri;
  48. }
  49. }

# End

Nginx 的两种认证方式的更多相关文章

  1. php 与 nginx 的两种处理方式

    1.IP:Port 监听方式 php-fpm docker pull PHP:2.4-alpine nginx.conf fastcgi_pass 127.0.0.1:9000; php-fpm 在容 ...

  2. git使用ssh密钥和https两种认证方式汇总(转)

    在版本库的SSH方式和HTTPS方式是不同的,具体来说就是url信息的不同,但是,实际的认证机制也是不同的.当建立了本机密钥之后,使用ssh方式实际上是不需要再次认证的,而https则每次需要输入密码 ...

  3. [Linux]PHP-FPM与NGINX的两种通讯方式

    一.通过监听TCP端口通讯 php-fpm.d/www.conf ; The address on which to accept FastCGI requests. ; Valid syntaxes ...

  4. 使用IdentityServer4,在一个ASPNetCore项目中,配置oidc和api的AccessToken两种认证授权

    1.配置两种认证方式 JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); services.AddAuthentication(op ...

  5. Web APi之认证(Authentication)两种实现方式【二】(十三)

    前言 上一节我们详细讲解了认证及其基本信息,这一节我们通过两种不同方式来实现认证,并且分析如何合理的利用这两种方式,文中涉及到的基础知识,请参看上一篇文中,就不再叙述废话. 序言 对于所谓的认证说到底 ...

  6. 转 Web APi之认证(Authentication)两种实现方式【二】(十三)

    前言 上一节我们详细讲解了认证及其基本信息,这一节我们通过两种不同方式来实现认证,并且分析如何合理的利用这两种方式,文中涉及到的基础知识,请参看上一篇文中,就不再废叙述废话. 序言 对于所谓的认证说到 ...

  7. Nginx 和 PHP 的两种部署方式比较

    2种部署方式简介 第一种 前置1台nginx服务器做HTTP反向代理和负载均衡 后面多态服务器部署Nginx Web服务和php-fpm提供的fast cgi服务 第二种 前置1台nginx服务器做W ...

  8. Java使用SFTP和FTP两种连接方式实现对服务器的上传下载 【我改】

    []如何区分是需要使用SFTP还是FTP? []我觉得: 1.看是否已知私钥. SFTP 和 FTP 最主要的区别就是 SFTP 有私钥,也就是在创建连接对象时,SFTP 除了用户名和密码外还需要知道 ...

  9. wdcp支持两种安装方式

    v3.2版本已发布,支持多PHP版本共存共用,支持SSL证书,更多可看论坛 v3版讨论区 更多安装说明请看 http://www.wdlinux.cn/bbs/thread-57643-1-1.htm ...

随机推荐

  1. [leetcode]33. Search in Rotated Sorted Array旋转过有序数组里找目标值

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  2. Iris数据集实战

    本次主要围绕Iris数据集进行一个简单的数据分析, 另外在数据的可视化部分进行了重点介绍. 环境 win8, python3.7, jupyter notebook 目录 1. 项目背景 2. 数据概 ...

  3. jQuery的介绍

    01-jQuery的介绍   1.为什么要使用jQuery 在用js写代码时,会遇到一些问题: window.onload 事件有事件覆盖的问题,因此只能写一个事件. 代码容错性差. 浏览器兼容性问题 ...

  4. jenkins shell部署

    jenkins执行shell脚本 jenkins执行shell 上一篇说的是jenkins+svn+maven把war包自动部署到Tomcat,这篇是从SVN上拉取代码maven生成jar文件,并且拷 ...

  5. ArrayList 初探

    1.ArrayList继承AbstractList,实现List.RandomAccess.Cloneable.Serializable接口 public class ArrayList<E&g ...

  6. linux学习第七天 (Linux就该这么学)

    今天讲了chmod (权限 设置)和 chown(属性 设置),特殊权限:SUID u+s 数字法是4  x=s  - = S,SGID g+s 数字法是2 x=s -=S,SBIT o+t  x=t ...

  7. Redhat/Centos6.x安装Chrome

    由于Chrome对rhel6.x不在支持发布版本,只能安装chromium版本! 01.下载地址 http://people.centos.org/hughesjr/chromium/6/x86_64 ...

  8. java多线程系列7 高级同步工具(1)信号量Semaphore

    Semaphore叫做信号量 可以控制某个资源可被同时访问的个数, acquire() 获取一个许可,得到许可才能执行后面的代码,如果没有就等待. release() 释放一个许可. 当信号量的只允许 ...

  9. spring与junit整合测试

    1.导包4+2+aop+test 2.配置注解 3.测试

  10. ABP框架系列之三十一:(Localization-本地化)

    Introduction Any application has at least one language for user interface. Many applications have mo ...