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

相关推荐
jason.zeng@15022077 小时前
jenkins踩坑指南
运维·jenkins
hweiyu007 小时前
Linux 命令:patch
linux·运维·服务器
IDC02_FEIYA7 小时前
Windows资源管理器未响应怎么处理?
运维·服务器·windows
遇见火星7 小时前
Linux 服务可用性监控实战:端口、进程、接口怎么监控?
android·linux·运维
tod1137 小时前
IP分片和组装的具体过程
运维·服务器·网络
哆啦code梦8 小时前
Kong vs Nginx:微服务网关选型指南
nginx·kong·微服务网关
IT研究所8 小时前
信创浪潮下 ITSM 的价值重构与实践赋能
大数据·运维·人工智能·安全·低代码·重构·自动化
代码AI弗森8 小时前
WSL2:在 Windows 上获得一整套可托付的 Linux 工程环境
linux·运维·服务器
玉梅小洋8 小时前
Docker 镜像在节点间的高效拷贝的方案
运维·docker·容器
niceffking8 小时前
Linux信号相关函数
linux·运维·服务器·linux信号