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

相关推荐
2401_8658548814 分钟前
服务器的windows和Linux系统有什么区别
linux·运维·服务器
IT199516 分钟前
MySQL运维笔记-一种数据定期备份的方法
运维·笔记·mysql
海域云-罗鹏17 分钟前
企业服务器防黑客攻击:WAF防火墙部署全攻略
运维·服务器
云飞云共享云桌面18 分钟前
SolidWorks服务器怎么实现研发软件多人共享、数据安全管理
java·linux·运维·服务器·数据库·自动化
ZeroNews内网穿透20 分钟前
EasyNode 结合 ZeroNews,实现远程管理服务器
运维·服务器·网络协议·安全·http
AOwhisky22 分钟前
用户、用户组管理
linux·运维·运维开发
2301_7679026436 分钟前
Ansible 自动化运维入门到实战
运维·自动化·ansible
翼龙云_cloud1 小时前
阿里云渠道商:在更换阿里云 GPU 公网 IP 时,如何确保数据的安全性?
运维·服务器·tcp/ip·阿里云·云计算
爱喝水的鱼丶1 小时前
SAP-ABAP:通过接口创建生产订单报“没有工艺路线选中”错误解决办法详解
运维·开发语言·sap·abap·bapi·生产订单
00后程序员张1 小时前
AppStoreInfo.plist 在苹果上架流程中的生成方式和作用
android·小程序·https·uni-app·iphone·webview