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

2024-08-26 20:28

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

nginx访问控制

用于location段

Allow:设定允许哪台或哪些主机访问,多个参数间用空格隔开

Deny:设定禁止那台或哪些主机访问,多个参数间用空格隔开

//deny
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conflocation /status {echo "lisy";deny 192.168.35.143;}
[root@nginx ~]# nginx -s reload
//验证
[root@test ~]# curl http://192.168.35.142/status
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.24.0</center>
</body>
</html>//开启stub_status模块,stub_status模块主要作用于查看nginx的一些状态信息
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conflocation /status {echo "lisy";stub_status on;}[root@nginx ~]# nginx -s reload
//查看状态信息
[root@test ~]# curl http://192.168.35.142/status
Active connections: 1 
server accepts handled requests19 19 19 
Reading: 0 Writing: 1 Waiting: 0
//Active connections:当前nginx正在处理的活动连接数
//Server accepts handled requests:nginx总共处理了63个连接,成功创建63次握手,总共处理了62个请求
//Reading:nginx读取到客户端的Header信息数
//Writing:nginx返回给客户端的eader信息数
//Waiting:开启keep-alive的情况下,这个值等于active-(reading+writing),意思就是nginx已经处理完成,正在等候下一次请求指令的驻留连接。所以,在访问效率高、请求很快就被处理完毕的情况下,waiting数比较多是正常的。如果reading+writing数较多,则说明并发访问量非常大,正在处理过程中。//allow和deny同时存在时
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conflocation /status {echo "lisy";allow 192.168.35.143;deny all;}
[root@nginx ~]# nginx -s reload
//验证
[root@test ~]# curl http://192.168.35.142/status
lisy
[root@test2 ~]# curl http://192.168.35.142/status
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.24.0</center>
</body>
</html>

用户认证

//安装httpd-tools软件包
[root@nginx ~]# yum -y install httpd-tools//创建用户密钥文件
[root@nginx ~]# htpasswd -c -m /usr/local/nginx/conf/.user_auth_file lsy123
New password: 
Re-type new password: 
Adding password for user lsy123//配置nginx(注意auth_basic_user_file必须用绝对路径)
[root@nginx conf]# vim nginx.conf
[root@nginx conf]# nginx -s reload

验证 

 

 https配置

//环境准备
//nginx/example.com 192.168.35.142
//test.example.com 192/168.35.143//tesr主机
//在CA服务器中生成一对密钥
[root@test ~]# mkdir  -p  /etc/pki/CA/private
[root@test ~]# cd /etc/pki/CA/
[root@test CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048)[root@test CA]# ls
private
[root@test CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 1024
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
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) []:lsy
Email Address []:lsy@example.com//nginx主机
//在nginix中生成证书签署请求,发送给CA
[root@nginx conf]# (umask 077;openssl genrsa -out httpd.key 2048)
[root@nginx conf]# openssl req -new -key httpd.key -days 1024 -out httpd.csr
Ignoring -days without -x509; not generating a certificate
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
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) []:lsy
Email Address []:lsy@example.comPlease enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []://将证书发送给test主机,在test主机中查看
[root@nginx conf]# scp httpd.csr root@192.168.35.143:/root/
[root@test ~]# ls
anaconda-ks.cfg  httpd.csr//test主机签署证书
[root@test ~]# mkdir /etc/pki/CA/newcerts
[root@test ~]# touch /etc/pki/CA/index.txt
[root@test ~]# echo "01" > /etc/pki/CA/serial
[root@test ~]# openssl ca -in httpd.csr -out httpd.crt -days 1024
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:Serial Number: 1 (0x1)ValidityNot Before: Aug 26 11:27:32 2024 GMTNot After : Jun 16 11:27:32 2027 GMTSubject:countryName               = CNstateOrProvinceName       = HBorganizationName          = huayuorganizationalUnitName    = linuxcommonName                = lsyemailAddress              = lsy@example.comX509v3 extensions:X509v3 Basic Constraints: CA:FALSEX509v3 Subject Key Identifier: 2D:35:3F:B7:26:D7:F1:DE:2C:8D:DC:E7:DC:5C:0E:EB:C3:C7:70:E4X509v3 Authority Key Identifier: E6:16:C5:70:7C:2D:BC:B8:A2:60:18:C9:5A:4C:32:1D:5E:F6:94:FF
Certificate is to be certified until Jun 16 11:27:32 2027 GMT (1024 days)
Sign the certificate? [y/n]:y1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
[root@test ~]# ls
anaconda-ks.cfg  httpd.crt  httpd.csr//将签署的证书httpd.crt和服务器的证书cacert.pem发送给nginx
[root@ca ~]# scp httpd.crt root@192.168.35.142:/usr/local/nginx/conf/ 
[root@ca ~]# scp /etc/pki/CA/cacert.pem root@192.168.35.142:/usr/local/nginx/conf///nginx主机配置https
[root@nginx conf]# vim 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;#charset koi8-r;#access_log  logs/host.access.log  main;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 "lsy" > index.html
[root@nginx html]# nginx -s reload

验证

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



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

相关文章

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

Windows的CMD窗口如何查看并杀死nginx进程

《Windows的CMD窗口如何查看并杀死nginx进程》:本文主要介绍Windows的CMD窗口如何查看并杀死nginx进程问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录Windows的CMD窗口查看并杀死nginx进程开启nginx查看nginx进程停止nginx服务

Nginx Location映射规则总结归纳与最佳实践

《NginxLocation映射规则总结归纳与最佳实践》Nginx的location指令是配置请求路由的核心机制,其匹配规则直接影响请求的处理流程,下面给大家介绍NginxLocation映射规则... 目录一、Location匹配规则与优先级1. 匹配模式2. 优先级顺序3. 匹配示例二、Proxy_pa

Windows 系统下 Nginx 的配置步骤详解

《Windows系统下Nginx的配置步骤详解》Nginx是一款功能强大的软件,在互联网领域有广泛应用,简单来说,它就像一个聪明的交通指挥员,能让网站运行得更高效、更稳定,:本文主要介绍W... 目录一、为什么要用 Nginx二、Windows 系统下 Nginx 的配置步骤1. 下载 Nginx2. 解压

在Spring Boot中实现HTTPS加密通信及常见问题排查

《在SpringBoot中实现HTTPS加密通信及常见问题排查》HTTPS是HTTP的安全版本,通过SSL/TLS协议为通讯提供加密、身份验证和数据完整性保护,下面通过本文给大家介绍在SpringB... 目录一、HTTPS核心原理1.加密流程概述2.加密技术组合二、证书体系详解1、证书类型对比2. 证书获

Mysql中的用户管理实践

《Mysql中的用户管理实践》:本文主要介绍Mysql中的用户管理实践,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录13. 用户管理13.1 用户 13.1.1 用户信息 13.1.2 创建用户 13.1.3 删除用户 13.1.4 修改用户

Nginx 413修改上传文件大小限制的方法详解

《Nginx413修改上传文件大小限制的方法详解》在使用Nginx作为Web服务器时,有时会遇到客户端尝试上传大文件时返回​​413RequestEntityTooLarge​​... 目录1. 理解 ​​413 Request Entity Too Large​​ 错误2. 修改 Nginx 配置2.1

Web技术与Nginx网站环境部署教程

《Web技术与Nginx网站环境部署教程》:本文主要介绍Web技术与Nginx网站环境部署教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Web基础1.域名系统DNS2.Hosts文件3.DNS4.域名注册二.网页与html1.网页概述2.HTML概述3.

NGINX 配置内网访问的实现步骤

《NGINX配置内网访问的实现步骤》本文主要介绍了NGINX配置内网访问的实现步骤,Nginx的geo模块限制域名访问权限,仅允许内网/办公室IP访问,具有一定的参考价值,感兴趣的可以了解一下... 目录需求1. geo 模块配置2. 访问控制判断3. 错误页面配置4. 一个完整的配置参考文档需求我们有一