热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

【CentOS7架构23】,设置nginx用户认证#180104

2019独角兽企业重金招聘Python工程师标准hellopasswdnginx用户认证viusrlocalnginxconfvhosttest.com.confserver

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

hellopasswd


nginx用户认证
  • vi /usr/local/nginx/conf/vhost/test.com.conf server { listen 80; serever_name test.com; index index.html index.htm index.php; root /data/wwwroot/test.com;

    location /{auth_basic "Auth";auth_basic_user_file /usr/local/nginx/conf/htpasswd;}

    }

  • yum install -y httpd

  • htpasswd -c /usr/local/nginx/conf/htpasswd aming

  • t && -s reload #测试配置并重新加载

[root@localhost default]# cd /usr/local/nginx/conf/vhost
[root@localhost vhost]# vi test.com.conf1 server2 {3 listen 80;4 server_name test.com;5 index index.html index.htm index.php;6 root /data/wwwroot/test.com;7 8 location /9 {10 auth_basic "Auth";11 auth_basic_user_file /usr/local/nginx/conf/htpasswd;12 }13 }

[root@localhost vhost]# /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd user
New password:
Re-type new password:
Adding password for user user
[root@localhost vhost]# cat /usr/local/nginx/conf/htpasswd
user:$apr1$NVdIvb1f$14A08nppYo/PwUYjQRD6O.

[root@localhost vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload

[root@localhost vhost]# curl -x 127.0.0.1:80 test.com



401 Authorization Required



nginx/1.4.7



[root@localhost vhost]# curl -x 127.0.0.1:80 -u user:123 test.com



404 Not Found



nginx/1.4.7


[root@localhost vhost]# ls /data/wwwroot/test.com
ls: cannot access /data/wwwroot/test.com: No such file or directory
[root@localhost vhost]# mkdir /data/wwwroot/test.com
[root@localhost vhost]# echo "test.com" > /data/wwwroot/test.com/index.html
[root@localhost vhost]# curl -x 127.0.0.1:80 -u user:123 test.com
test.com

若在某一目录下实现用户认证,如:test.com/admin/,则需将根目录/改为/admin/

[root@localhost vhost]# vi test.com.conf 1 server2 {3 listen 80;4 server_name test.com;5 index index.html index.htm index.php;6 root /data/wwwroot/test.com;7 8 location /admin/9 {10 auth_basic "Auth";11 auth_basic_user_file /usr/local/nginx/conf/htpasswd;12 }13 }[root@localhost vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost vhost]# curl -x 127.0.0.1:80 test.com
test.com
[root@localhost vhost]# mkdir /data/wwwroot/test.com/admin/
[root@localhost vhost]# echo "hello test.com admin dir" > /data/wwwroot/test.com/admin/index.html
[root@localhost vhost]# curl -x 127.0.0.1:80 -u user:123 test.com/admin/
hello test.com admin dir

以上为针对目录,若要针对当个文件,则需要匹配

[root@localhost vhost]# vi test.com.conf 1 server2 {3 listen 80;4 server_name test.com;5 index index.html index.htm index.php;6 root /data/wwwroot/test.com;7 8 location ~(.*)admin.php$9 {10 auth_basic "Auth";11 auth_basic_user_file /usr/local/nginx/conf/htpasswd;12 }13 }
[root@localhost vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload

[root@localhost vhost]# vi /data/wwwroot/test.com/admin.php1 [root@localhost vhost]# curl -x 127.0.0.1:80 test.com/admin/
hello test.com admin dir
[root@localhost vhost]# curl -x 127.0.0.1:80 test.com/admin.php



401 Authorization Required



nginx/1.4.7



[root@localhost vhost]# curl -x 127.0.0.1:80 -u user:123 test.com/admin.php

修改于 180104


转:https://my.oschina.net/hellopasswd/blog/1602459



推荐阅读
  • Windows下配置PHP5.6的方法及注意事项
    本文介绍了在Windows系统下配置PHP5.6的步骤及注意事项,包括下载PHP5.6、解压并配置IIS、添加模块映射、测试等。同时提供了一些常见问题的解决方法,如下载缺失的msvcr110.dll文件等。通过本文的指导,读者可以轻松地在Windows系统下配置PHP5.6,并解决一些常见的配置问题。 ... [详细]
  • 本文介绍了在Windows环境下如何配置php+apache环境,包括下载php7和apache2.4、安装vc2015运行时环境、启动php7和apache2.4等步骤。希望对需要搭建php7环境的读者有一定的参考价值。摘要长度为169字。 ... [详细]
  • 如何实现织梦DedeCms全站伪静态
    本文介绍了如何通过修改织梦DedeCms源代码来实现全站伪静态,以提高管理和SEO效果。全站伪静态可以避免重复URL的问题,同时通过使用mod_rewrite伪静态模块和.htaccess正则表达式,可以更好地适应搜索引擎的需求。文章还提到了一些相关的技术和工具,如Ubuntu、qt编程、tomcat端口、爬虫、php request根目录等。 ... [详细]
  • 这是原文链接:sendingformdata许多情况下,我们使用表单发送数据到服务器。服务器处理数据并返回响应给用户。这看起来很简单,但是 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • Voicewo在线语音识别转换jQuery插件的特点和示例
    本文介绍了一款名为Voicewo的在线语音识别转换jQuery插件,该插件具有快速、架构、风格、扩展和兼容等特点,适合在互联网应用中使用。同时还提供了一个快速示例供开发人员参考。 ... [详细]
  • 本文介绍了在Mac上搭建php环境后无法使用localhost连接mysql的问题,并通过将localhost替换为127.0.0.1或本机IP解决了该问题。文章解释了localhost和127.0.0.1的区别,指出了使用socket方式连接导致连接失败的原因。此外,还提供了相关链接供读者深入了解。 ... [详细]
  • 本文介绍了在mac环境下使用nginx配置nodejs代理服务器的步骤,包括安装nginx、创建目录和文件、配置代理的域名和日志记录等。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • GetWindowLong函数
    今天在看一个代码里头写了GetWindowLong(hwnd,0),我当时就有点费解,靠,上网搜索函数原型说明,死活找不到第 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文介绍了在Linux下安装Perl的步骤,并提供了一个简单的Perl程序示例。同时,还展示了运行该程序的结果。 ... [详细]
  • 成功安装Sabayon Linux在thinkpad X60上的经验分享
    本文分享了作者在国庆期间在thinkpad X60上成功安装Sabayon Linux的经验。通过修改CHOST和执行emerge命令,作者顺利完成了安装过程。Sabayon Linux是一个基于Gentoo Linux的发行版,可以将电脑快速转变为一个功能强大的系统。除了作为一个live DVD使用外,Sabayon Linux还可以被安装在硬盘上,方便用户使用。 ... [详细]
author-avatar
love小蕾XM_578
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有