nginx的https搭建

1、防火墙配置

开放HTTP和HTTPS服务(永久生效)

firewall-cmd --permanent --add-service=http

firewall-cmd --permanent --add-service=https

重新加载规则

firewall-cmd --reload

验证开放状态

firewall-cmd --list-services

2、配置账户验证

#安装nginx

yum install nginx -y

安装认证工具

yum install httpd-tools -y

创建认证文件

htpasswd -c /etc/nginx/.htpasswd admin

设置文件权限(仅Nginx可读)

chmod 600 /etc/nginx/.htpasswd

chown nginx:nginx /etc/nginx/.htpasswd

3、启用https

生成自签名证书

mkdir -p /etc/ssl/private

openssl req -x509 -nodes -days 365 -newkey rsa:2048 \

-keyout /etc/ssl/private/nginx.key \

-out /etc/ssl/certs/nginx.crt \

-subj "/C=CN/ST=Beijing/L=Beijing/O=Test/CN=your_domain.com"

4、nginx配置文件

server {

listen 80;

server_name your_domain.com;

return 301 https://server_namerequest_uri; # HTTP跳转HTTPS

}

server {

listen 443 ssl;

server_name your_domain.com;

SSL配置

ssl_certificate /etc/ssl/certs/nginx.crt;

ssl_certificate_key /etc/ssl/private/nginx.key;

ssl_protocols TLSv1.2 TLSv1.3; # 禁用旧协议

ssl_ciphers HIGH:!aNULL:!MD5;

账户验证

auth_basic "Restricted Area";

auth_basic_user_file /etc/nginx/.htpasswd;

root /usr/share/nginx/html;

index index.html;

}

5、验证与重启

检查配置语法

nginx -t

重启Nginx

systemctl reload nginx

相关推荐
艾莉丝努力练剑17 分钟前
【QT】Qt常用控件与布局管理深度解析:从原理到实践的架构思考
linux·运维·服务器·开发语言·网络·qt·架构
以太浮标18 分钟前
华为eNSP模拟器综合实验之- WLAN瘦AP配置实战案例详解
运维·网络·网络协议·华为·智能路由器·信息与通信
个性小王18 分钟前
华为-AC+FIT AP组网(web方式)
运维·网络·华为
码云数智-园园27 分钟前
Python 列表与元组:从核心区别到实战选型
运维·服务器·windows
zhangzeyuaaa28 分钟前
Logstash 单次运行模式下的数据丢失陷阱:Output 缓冲区丢弃的根因与根治方案
运维·logstash
格林威28 分钟前
Linux系统工业相机:Linux udev 规则绑定相机设备
linux·运维·开发语言·人工智能·数码相机·计算机视觉·工业相机
IMPYLH30 分钟前
Linux 的 mv 命令
linux·运维·服务器·bash
weixin_4235339932 分钟前
虚拟机-ubuntu突然连不上网,网络图标消失
linux·运维·ubuntu
zzzsde37 分钟前
【Linux】进程间通信(3)system V信号量
linux·运维·服务器
峥无39 分钟前
Linux 调试效率革命:CGDB
linux·运维·服务器