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 天前
解决nginx错误:http://localhost:7000正常,http://localhost:7000/map 报错404
nginx·大剑师
xing-xing2 天前
Docker容器中Nginx站点根目录网页配置访问
nginx·docker
XUEYUAN52122 天前
ASN 自治系统号风控:平台如何通过 IP 所属自治域批量识别代理流量
python·网络协议·http·网络安全·socks5
2501_916007472 天前
苹果商店iOS应用上架费用全面解析:开发者计划与审核费用计算
android·ios·小程序·https·uni-app·iphone·webview
Lsetea2 天前
证书没到期却报certificate has expired:OpenSSL定位中间证书与系统时间
运维·https·ssl证书·openssl·证书链
PC2005-cloud2 天前
Nginx 防盗链配置实战:用 referer 模块保护网站静态资源
nginx
2501_916008892 天前
如何实现iOS App代码混淆:SwiftShield使用指南
android·ios·小程序·https·uni-app·iphone·webview
SDWAN_Cheap2 天前
HTTPS到底加密了什么?
https·数据加密
captain3762 天前
HTTP(2)
java·网络·http·java-ee
PHP实战开发录2 天前
Nginx上传大文件为什么报413
nginx·php·开发