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

相关推荐
Lsetea12 分钟前
curl报58 unable to set private key file:客户端证书与私钥加载排查
运维·https·ssl证书·openssl·curl
天远Date Lab13 分钟前
零信任架构实战:基于天远学籍核验三要素构建自动化竞赛资格审查网关
运维·人工智能·架构·自动化
Lsetea1 小时前
OpenSSL报certificate is not yet valid:error 9与notBefore时间排查
linux·运维·https·ssl证书·openssl
魔法阵维护师1 小时前
Linux 命令行入门踩坑记录:从 VMware 装系统到文件管理
linux·运维·服务器
飞飞传输1 小时前
业务系统文件安全检测怎么做?生物医药企业选型与建设指南
大数据·运维·安全
2501_915909061 小时前
iOS应用从开发到上架App Store的完整发布流程与步骤指南
android·ios·小程序·https·uni-app·webview
姜鱼问生1 小时前
docker exec 排查容器问题:从日志到进程
运维·网络·数据库·docker·容器
qq_543447821 小时前
DNS解析怎么排查?故障处理与运维实用手册
运维·服务器·网络·php
DeviceHub1 小时前
AI服务器电源设计进阶:TLVR电感的应用解析与选型验证指南
运维·服务器·人工智能
Apipi*1 小时前
30天速通Linux 第七章进程间通信
linux·运维·服务器