nginx http 跳转到https

改 Nginx 配置文件

在您安装了 SSL 证书之后,您需要修改 Nginx 的配置文件以启用 HTTPS 和 HTTP 自动跳转 HTTPS。

打开 Nginx 配置文件(通常位于 /etc/nginx/nginx.conf),找到您的网站配置块。在该配置块中添加以下内容:

复制代码
server {
    listen 80;
    server_name example.com;
    return 301 https://$server_name$request_uri;
}
server {
    listen 443 ssl;
    server_name example.com;
    ssl_certificate /path/to/ssl/certificate;
    ssl_certificate_key /path/to/ssl/certificate_key;
    # other SSL configuration options
    # ...
    # other server configuration options
    # ...
}

该配置块包括两个部分:

  • 第一个部分监听 HTTP(端口 80),并将所有的 HTTP 请求重定向到 HTTPS。
  • 第二个部分监听 HTTPS(端口 443),并包括 SSL 证书和其他 SSL 配置。

注意不要在同一server 中同时监听443端口和80端口,会造成过多的重定向问题

相关推荐
Jinuss8 小时前
HTML页面http-equiv=“refresh“自动刷新原理详解
http·html
864记忆8 小时前
Qt创建连接注意事项
数据库·qt·nginx
Yan-英杰10 小时前
BoostKit OmniAdaptor 源码深度解析
网络·人工智能·网络协议·tcp/ip·http
Anarkh_Lee13 小时前
别再手写 conf 了!NgxFlow:基于 React Flow 的 Nginx 可视化与调试神器
前端·nginx·数据可视化
Run Out Of Brain13 小时前
解决nginx代理配置下wordpress的 /wp-admin/返回 302 重定向到登录页问题
运维·nginx
智慧的牛15 小时前
局域网内实现HTTPS
网络协议·https
一勺菠萝丶15 小时前
芋道项目部署:前端写死后端地址 vs Nginx 反向代理
前端·nginx·状态模式
青果网络_xz17 小时前
海外HTTP代理适合哪些业务?与Socks/隧道的区别
网络·网络协议·http
VekiSon18 小时前
综合项目实战——电子商城信息查询系统
linux·c语言·网络·http·html·tcp·sqlite3
ベadvance courageouslyミ18 小时前
简易在线商城制作
http·线程·在线商城·界面跳转