简介:

今天来研究一下 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 >& || 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..tar.gz; cd nginx-1.10.
./configure --prefix=/usr/local/nginx-1.10. \
--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./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./conf

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

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

server {
listen ;
server_name local.server.com; auth_basic "User Authentication";
auth_basic_user_file /usr/local/nginx-1.10./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./conf/vhost/local.conf  # 我们先来编辑本机配置文件,也就是用户直接访问的域名

server {
listen ;
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./conf/vhost/auth.conf  # 这是第三方认证服务器,认证逻辑使用的 PHP 代码

server {
listen ;
server_name auth.server.com; location ~ \.php$ {
fastcgi_pass 127.0.0.1:;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx-1.10./html$fastcgi_script_name;
include fastcgi_params;
}
} shell > vim /usr/local/nginx-1.10./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 == ''){
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./conf/vhost/git.server.com

server {
listen ;
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./conf/pass.db; auth_request /auth; location ~ ^.*\.git/objects/([-9a-f]+/[-9a-f]+|pack/pack-[-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

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. Python中使用RabbitMQ

    一 RabbitMQ简介 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. MQ全称为Message Queue, 消息 ...

  2. CentOS 7 查询yum安装的软件及路径

    来源:CentOS 7 查询yum安装的软件及路径 先执行下面的命令,查看所有的已安装软件名称. rpm -qa 然后执行 rpm -ql 软件名称 就可以显示软件的安装路径.   [root@loc ...

  3. 描述符__get__,__set__,__delete__

    描述符__get__,__set__,__delete__ # 描述符:1用来代理另外一个类的属性 # __get__():调用一个属性时,触发 # __set__():为一个属性赋值时触发 # __ ...

  4. [费用流][BZOJ1070]修车

    修车 题目描述 同一时刻有位车主带着他们的爱车来到了汽车维修中心.维修中心共有M位技术人员,不同的技术人员对不同的车进行维修所用的时间是不同的.现在需要安排这M位技术人员所维修的车及顺序,使得顾客平均 ...

  5. CentOS6.5在虚拟机中安装

    只有一点,先建虚拟机,再选择iso镜像安装,注意,安装路径不能有中文空格之类的. CentOS6.5 64位下载链接 链接:https://pan.baidu.com/s/1d6zp5LtKtkL8I ...

  6. [Machine Learning] some concept about the CV

    Cross-validation VS SSE CV is not designed to improve the fit on the training data, but it won't nec ...

  7. HNOI 2018 简要题解

    寻宝游戏 毒瘤题. 估计考试只会前30pts30pts30pts暴力然后果断走人. 正解是考虑到一个数&1\&1&1和∣0|0∣0都没有变化,&0\&0& ...

  8. MFC程序执行后台操作时不允许操作界面的一种方法

    在使用MFC编写界面程序时,有时候会遇到像点击按钮后,后台进行大量操作后才显示处理结果这种情况,在后台处理过程中,界面不应该被允许做任何操作,这里介绍一种方法. 解决办法 点击按钮后,弹出一个模态对话 ...

  9. c++类对象的内存分布

    要想知道c++类对象的内存布局, 可以有多种方式,比如: 1)输出成员变量的偏移, 通过offsetof宏来得到 2)通过调试器查看, 比如常用的VS 1.没有数据成员的对象 class A{ }; ...

  10. 05 IO和管道

    目录   三种I/O设备 把I/O重定向至文件 使用管道   知识铺垫     1)查看fd-文件描述符 (L)   ll /proc/$$/fd   在Linux中,系统打开文件时会随机分配一个编号 ...