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端口,会造成过多的重定向问题

相关推荐
Predestination王瀞潞15 小时前
5.4.2 通信->WWW万维网内容访问标准(W3C):WWW(World Wide Web) 核心技术规范
网络·网络协议·https·www
上去我就QWER16 小时前
详解HTTP协议中的multipart/form-data
网络·网络协议·http
困惑阿三18 小时前
客户消息及时反馈
nginx·node.js·飞书·企业微信
sugar__salt19 小时前
网络原理(五)——HTTP
网络·网络协议·http
liurunlin88820 小时前
httpslocalhostindex 配置的nginx,一刷新就报404了
运维·nginx
BullSmall20 小时前
Nginx负载均衡会话保持配置指南
运维·nginx·负载均衡
芒果披萨21 小时前
http请求
网络·网络协议·http
Predestination王瀞潞1 天前
5.3.1 通信->HTTP3超文本传输协议标准(IETF RFC 9114):HTTP(HypetText Transfer Protocol)
网络协议·tcp/ip·http
你才是臭弟弟1 天前
Nginx部署前后端
运维·nginx