惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
WordPress大学
WordPress大学
The Cloudflare Blog
云风的 BLOG
云风的 BLOG
博客园 - Franky
D
Docker
小众软件
小众软件
阮一峰的网络日志
阮一峰的网络日志
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
U
Unit 42
M
MIT News - Artificial intelligence
B
Blog
GbyAI
GbyAI
C
Check Point Blog
P
Proofpoint News Feed
博客园 - 司徒正美
Hugging Face - Blog
Hugging Face - Blog
雷峰网
雷峰网
IT之家
IT之家
Google DeepMind News
Google DeepMind News
V
V2EX
Stack Overflow Blog
Stack Overflow Blog

Liu Zijian's Blog | 一个技术博客

使用Certbot自动续签HTTPS证书 使用Filebeat采集Nginx日志到ES Python的协程 Python中的异常 Python中的类和对象 Python的函数 Python的数据结构,推导式、迭代器和生成器 Spring AI集成多模态模型 LangChain4j多模态 LangChain Tools工具使用 Python中的模块和包 Python全局环境和虚拟环境(venv) LangChain Prompt提示词工程 LangChain4j Tools工具使用 基于Dify搭建AI智能体应用 LangChain4j RAG检索增强生成 Spring AI实现MCP Server Spring AI集成MCP Client LangChain4j Prompt提示词工程 Spring AI使用知识库增强对话功能 Spring AI实现一个智能客服 Spring AI实现一个简单的对话机器人 实现MinIO数据的每日备份 自己实现一个DNS服务 简单理解AI智能体 大模型和大模型应用 LangChain开篇 LangChain4j开篇 一个解析Excel2007的POI工具类 DataPermissionInterceptor源码解读
Nginx设置HTTPS监听
Liu Zijian · 2024-10-13 · via Liu Zijian's Blog | 一个技术博客

1. 获取 SSL 证书

首先,你需要获取一个 SSL 证书,可以从以下渠道获得:

  • 自签名证书(测试用)
  • 付费证书(如购买的证书)

这里,博主使用这个网站生成 https://ssl.host.mw/certificate/apply

2. 安装证书

安装证书文件和私钥到服务器,通常是 .crt.key 文件。自己确定存放的目录。

3. 配置 Nginx

编辑 Nginx 配置文件,按照自己实际情况来,通常在 /etc/nginx/nginx.conf 中。

(1)配置 HTTP 重定向到 HTTPS

将所有 HTTP 请求重定向到 HTTPS,你可以在 Nginx 配置文件中添加以下代码:

server {
    listen 80;
    server_name www.liuzijian.com;

    # 重定向所有请求到 HTTPS
    return 301 https://$host$request_uri;
}

(2)配置 HTTPS 服务器

在同一个配置文件中,添加 HTTPS 服务器的配置:

server {
    listen 443 ssl;
    server_name www.liuzijian.com;

    # SSL 证书路径,按照实际情况填写
    ssl_certificate /xxx/your_domain.crt;
    ssl_certificate_key /xxx/your_domain.key;

    # 推荐的 SSL 配置
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;

    
    location / {
        # 配置你的网站根目录
        root /var/www/your_domain;
        index index.html index.htm;
    }

    # 代理到其他端口
    #location / {

    #    proxy_pass http://127.0.0.1:8360;
    #}
}

4. 检查并重启 Nginx

确保配置文件没有语法错误:

sudo nginx -t

如果没有错误,重启 Nginx:

sudo systemctl restart nginx

5. 测试

通过访问 http://www.liuzijian.comhttps://www.liuzijian.com 测试是否实现了 HTTP 到 HTTPS 的重定向。