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

相关推荐
郭老二2 分钟前
【docker】docker compose使用,已nginx为例
nginx·docker·容器
zzu123zsw37 分钟前
从 proxy_pass 末尾斜杠到 WebAssembly 预压缩:深挖 Nginx 生产环境四大高频踩坑点
运维·nginx·wasm·webassembly·devops·运维排错
郝亚军2 小时前
RK3562 中有 mosquitto、nginx 了,还需要 http,ftp,tftp 等协议吗?
运维·nginx·http
是个西兰花5 小时前
http讲解
网络·网络协议·http
Lsetea6 小时前
OpenSSL报certificate revoked:error 23与CRL吊销层级排查
linux·https·ssl证书·openssl·crl
HelloWorld工程师6 小时前
不改动任何历史配置,也能让 SSL 证书自动续期
linux·服务器·网络协议·https·ssl
Lsetea7 小时前
curl走HTTPS代理报SSL证书错误:proxy-cacert与cacert双层信任排查
linux·https·ssl证书·curl·代理服务器
菜鸟阿达9 小时前
我把 API 调试工具做成了浏览器版“Postman“,开源了:HTTP/SSE/WebSocket 全能测
http·开源·postman
2501_9378609410 小时前
HTTP/HTTPS 学习笔记(中):报头、Cookie、状态码与请求构造
学习·http·https