nginx访问控制,用户认证,https

2024-08-27 04:52

本文主要是介绍nginx访问控制,用户认证,https,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

nginx访问控制

用于location段Allow:设定允许哪台或哪些主机访问,多个参数间用空格隔开Deny:设定禁止那台或哪些主机访问,多个参数间用空格隔开比如:allow 192.168.100.20  192.168.100.30;deny all;拒绝某台主机访问nginx状态页面location /status {echo "hello";deny 192.168.100.20;}

 此时使用192.168.100.20来访问

此时是拒绝访问

使用192.168.100.30来访问

开启stub_status模块

stub_status模块主要作用于查看nginx的一些状态信息location /status {echo "hello";stub_status on;}

访问

 

Active connections:当前nginx正在处理的活动连接数

Server accepts handled requests:nginx总共处理了63个连接,成功创建63次握手,总共处理了62个请求

Reading:nginx读取到客户端的Header信息数

Writing:nginx返回给客户端的Header信息数

Waiting:开启keep-alive的情况下,这个值等于active-(reading+writing),意思就是nginx已经处理完成,正在等候下一次请求指令的驻留连接。所以,在访问效率高、请求很快就被处理完毕的情况下,waiting数比较多是正常的。如果reading+writing数较多,则说明并发访问量非常大,正在处理过程中。

 当allow和deny同时存在时

  location /status {stub_status on;allow 192.168.100.20;deny all;

使用192.168.100.20来访问

使用192.168.100.30来访问

拒绝访问

 默认访问权限是allow all,所有用户均可访问

1、只允许指定得ip访问,禁止其他ip访问allow 192.168.100.11;allow 192.168.100.12;deny all;2、只禁止指定的ip访问,允许其他ip访问deny 192.168.100.11;deny 192.168.100.12;allow all;

 用户认证

auth_basic “欢迎信息”;
auth_basic_user_file  “/path/to/user_auth_file”;
//user_auth_file内容格式
username:password//这里的密码为加密后的密码串,建议用htpasswd来创建文件
htpasswd -c -m /path/to/.user_auth_file USERNAME//授权用户
安装httpd-tools软件包
[root@nginx ~]# yum -y install httpd-tools//创建用户密钥文件
[root@nginx ~]# cd /usr/local/nginx/conf/
[root@nginx conf]# htpasswd -c -m .user_auth_file chen
New password: 
Re-type new password: 
Adding password for user chen
[root@nginx conf]# cat .user_auth_file 
chen:$apr1$whXqcpS.$EORacQbsq0P6JblZ0ayM5///配置nginx(注意auth_basic_user_file必须用绝对路径)
[root@nginx conf]# vim nginx.conflocation /status {stub_status on;auth_basic "welcome to hyedu";auth_basic_user_file "/usr/local/nginx/conf/.user_auth_file";}//ngint -t 测试配置文件并重载配置文件
[root@nginx conf]# 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@nginx conf]# nginx -s reload

访问

 

https配置 

Nginx:192.168.100.10

CA:192.168.100.30

 

//在CA服务器中生成一对密钥
[root@ca ~]# mkdir  -p  /etc/pki/CA/private
[root@ca ~]# cd /etc/pki/CA/
[root@ca CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048)
Generating RSA private key, 2048 bit long modulus (2 primes)
.+++++
....................+++++
e is 65537 (0x010001)[root@ca CA]# openssl rsa -in private/cakey.pem -pubout
writing RSA key
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvaWtkdtUn3T+pXIvD1Rf
LUGP8NdmlVqwamSU7fxqRA5BiWi7gKsNpnSBHlXGJ3PeFBRbNfff/IOpZLnMWDB4
OKDp63pB4OcB3GKWNoJsDYEg5m4HYdhHjJRywTkfmuUNoIok8fBg6gsYYHov9EVK
tmV9FTZBRIPSq7hiVm8dYPDFsuAhvi5CUxGO/VEXRsiJvePSQ1IAaMYUv/mDDMKC
GXX/qvyWPRMA6KdFmr6hO32jbY3fzllzfQpN3tjNrXbQPRa1o6GFQ9nQC8kHzo5L
qtRdeJ0ZMqQyU76f6kJQwcBPS2t/ByTGxq8DRAiVATNK2xO3LuNvfCv+CYRYuVwV
bwIDAQAB
-----END PUBLIC KEY-----[root@ca CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 1024
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HB
Locality Name (eg, city) [Default City]:WH
Organization Name (eg, company) [Default Company Ltd]:huayu
Organizational Unit Name (eg, section) []:linux
Common Name (eg, your name or your server's hostname) []:chen
Email Address []:cy@example.com//在nginix中生成证书签署请求,发送给CA[root@nginx ~]# cd /usr/local/nginx/conf/
[root@nginx conf]# (umask 077;openssl genrsa -out httpd.key 2048)
Generating RSA private key, 2048 bit long modulus (2 primes)
................................................................................+++++
............................................................................................................+++++
e is 65537 (0x010001)[root@nginx conf]#  openssl req -new -key httpd.key -days 1024 -out httpd.csr
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HB
Locality Name (eg, city) [Default City]:WH
Organization Name (eg, company) [Default Company Ltd]:huayu
Organizational Unit Name (eg, section) []:linux
Common Name (eg, your name or your server's hostname) []:chen
Email Address []:cy@example.com[root@nginx conf]#  ls
httpd.csr  httpd.key[root@nginx conf]#  scp httpd.csr root@192.168.100.30:/root///在CA主机中查看
[root@ca ~]# ls
anaconda-ks.cfg  Documents  httpd.csr //CA签署证书并发送给NGINX
[root@ca ~]# mkdir /etc/pki/CA/newcerts
[root@ca ~]# touch /etc/pki/CA/index.txt
[root@ca ~]#  echo "01" > /etc/pki/CA/serial
[root@ca ~]# openssl ca -in httpd.csr -out httpd.crt -days 1024
[root@ca ~]# ls
anaconda-ks.cfg  Documents  httpd.crt  initial-setup-ks.cfg  Pictures  Templates
Desktop          Downloads  httpd.csr//将CA签署的证书httpd.crt和服务器的证书cacert.pem发送给nginx
[root@ca ~]# scp httpd.crt root@192.168.100.10:/usr/local/nginx/conf/ 
[root@ca ~]# scp /etc/pki/CA/cacert.pem root@192.168.100.10:/usr/local/nginx/conf///nginx配置https
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.confserver {listen       443 ssl;server_name  localhost;ssl_certificate httpd.crt;ssl_certificate_key httpd.key;ssl_session_cache    shared:SSL:1m;ssl_session_timeout  5m;ssl_ciphers  HIGH:!aNULL:!MD5;ssl_prefer_server_ciphers  on;location / {root   html;index  index.html index.htm;}//nginx -t 测试配置文件
[root@nginx conf]# 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@nginx conf]# cd /usr/local/nginx/html/
[root@nginx html]# echo "hello" > index.html
[root@nginx html]# nginx -s reload

访问

这篇关于nginx访问控制,用户认证,https的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1110689

相关文章

Nginx分布式部署流程分析

《Nginx分布式部署流程分析》文章介绍Nginx在分布式部署中的反向代理和负载均衡作用,用于分发请求、减轻服务器压力及解决session共享问题,涵盖配置方法、策略及Java项目应用,并提及分布式事... 目录分布式部署NginxJava中的代理代理分为正向代理和反向代理正向代理反向代理Nginx应用场景

Nginx搭建前端本地预览环境的完整步骤教学

《Nginx搭建前端本地预览环境的完整步骤教学》这篇文章主要为大家详细介绍了Nginx搭建前端本地预览环境的完整步骤教学,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录项目目录结构核心配置文件:nginx.conf脚本化操作:nginx.shnpm 脚本集成总结:对前端的意义很多

springboot依靠security实现digest认证的实践

《springboot依靠security实现digest认证的实践》HTTP摘要认证通过加密参数(如nonce、response)验证身份,避免明文传输,但存在密码存储风险,相比基本认证更安全,却因... 目录概述参数Demopom.XML依赖Digest1Application.JavaMyPasswo

Spring Boot分层架构详解之从Controller到Service再到Mapper的完整流程(用户管理系统为例)

《SpringBoot分层架构详解之从Controller到Service再到Mapper的完整流程(用户管理系统为例)》本文将以一个实际案例(用户管理系统)为例,详细解析SpringBoot中Co... 目录引言:为什么学习Spring Boot分层架构?第一部分:Spring Boot的整体架构1.1

Python爬虫HTTPS使用requests,httpx,aiohttp实战中的证书异步等问题

《Python爬虫HTTPS使用requests,httpx,aiohttp实战中的证书异步等问题》在爬虫工程里,“HTTPS”是绕不开的话题,HTTPS为传输加密提供保护,同时也给爬虫带来证书校验、... 目录一、核心问题与优先级检查(先问三件事)二、基础示例:requests 与证书处理三、高并发选型:

k8s admin用户生成token方式

《k8sadmin用户生成token方式》用户使用Kubernetes1.28创建admin命名空间并部署,通过ClusterRoleBinding为jenkins用户授权集群级权限,生成并获取其t... 目录k8s admin用户生成token创建一个admin的命名空间查看k8s namespace 的

Nginx实现端口映射的示例代码

《Nginx实现端口映射的示例代码》本文主要介绍了Nginx实现端口映射的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1. 找到nginx的部署路径2. 备份原来的配置文件3. 编辑nginx.conf文件4. 在

Nginx屏蔽服务器名称与版本信息方式(源码级修改)

《Nginx屏蔽服务器名称与版本信息方式(源码级修改)》本文详解如何通过源码修改Nginx1.25.4,移除Server响应头中的服务类型和版本信息,以增强安全性,需重新配置、编译、安装,升级时需重复... 目录一、背景与目的二、适用版本三、操作步骤修改源码文件四、后续操作提示五、注意事项六、总结一、背景与

Nginx部署HTTP/3的实现步骤

《Nginx部署HTTP/3的实现步骤》本文介绍了在Nginx中部署HTTP/3的详细步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录前提条件第一步:安装必要的依赖库第二步:获取并构建 BoringSSL第三步:获取 Nginx

Web服务器-Nginx-高并发问题

《Web服务器-Nginx-高并发问题》Nginx通过事件驱动、I/O多路复用和异步非阻塞技术高效处理高并发,结合动静分离和限流策略,提升性能与稳定性... 目录前言一、架构1. 原生多进程架构2. 事件驱动模型3. IO多路复用4. 异步非阻塞 I/O5. Nginx高并发配置实战二、动静分离1. 职责2