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

相关推荐
程序员果子20 小时前
零拷贝:程序性能加速的终极奥秘
linux·运维·nginx·macos·缓存·centos
天生励志12320 小时前
Nginx安装部署
运维·nginx
濊繵1 天前
Linux网络--HTTP cookie 与 session
网络·网络协议·http
IT利刃出鞘1 天前
Nginx--变量的使用
运维·nginx
云和数据.ChenGuang1 天前
运维工程师软件之httpd`(Apache HTTP Server)
运维·http·apache
方块A1 天前
轻量级的 HTTP 跳转服务
网络·网络协议·http
同学807961 天前
新版本Chrome谷歌浏览器访问本地网络请求跨域无法正常请求
前端·http
代码不停1 天前
HTTP / HTTPS详细介绍
网络协议·http·https
G_Cloudpipe1 天前
Nginx 服务器部署
运维·服务器·nginx
董世昌411 天前
HTTP 核心:GET 与 POST 深度解析(区别、原理与实战场景)
网络·网络协议·http