在 Nginx 中将 HTTP 重定向到 HTTPS

要在 Nginx 中将 HTTP 重定向到 HTTPS,你可以使用以下配置:

server {

listen 80 default_server;

server_name your_domain.com www.your_domain.com;

return 301 https://server_namerequest_uri;

}

server {

listen 443 ssl;

server_name your_domain.com www.your_domain.com;

ssl_certificate /path/to/your/ssl/certificate;

ssl_certificate_key /path/to/your/ssl/certificate/key;

Additional SSL configuration goes here

location / {

Your website root goes here

root /path/to/your/website;

index index.html index.htm;

}

}

上面的配置做了以下事情:

  1. 第一个 server 块监听 80 端口(HTTP)。当用户访问 http://your_domain.comhttp://www.your_domain.com 时,Nginx 会返回 301 重定向,将请求重定向到 HTTPS。
  2. 第二个 server 块监听 443 端口(HTTPS)。它设置了 SSL 证书的路径,以及其他 SSL 相关的配置项。
  3. 在第二个 server 块中,location / 部分定义了网站的根目录。你需要将 /path/to/your/website 替换为你实际的网站根目录路径。

注意:

  • 你需要将 your_domain.com 替换为你自己的域名。
  • /path/to/your/ssl/certificate/path/to/your/ssl/certificate/key 替换为你的 SSL 证书和密钥的实际路径。
  • 根据你的具体需求,你可能需要调整其他 SSL 相关的配置项。

在应用新的配置后,重启 Nginx 服务器以使更改生效。之后,当用户访问 HTTP 版本的网站时,Nginx 将自动将他们重定向到 HTTPS 版本。

相关推荐
赖small强1 小时前
【ZeroRange WebRTC】Amazon Kinesis Video Streams WebRTC Data Plane REST API 深度解析
https·webrtc·data plane rest·sigv4 签名
m0_569531011 小时前
Nginx(4)--Nginx与tomcat反向代理和负载均衡
nginx·tomcat·负载均衡
Laravel技术社区2 小时前
海康视频 h5player 配置 proxy 代理websocket播放视频问题(websocket在业务系统https方式访问http的播放视频)
websocket·http·音视频
果壳~3 小时前
【Java】使用国密2,3,4.仿照https 统一请求响应加解密
java·https
R.lin4 小时前
浅谈Nginx
运维·nginx
Crazy________6 小时前
37负载均衡介绍和nginx模块编译安装
运维·nginx·负载均衡
百***34137 小时前
Nginx实现接口复制
运维·nginx·junit
赖small强9 小时前
【ZeroRange WebRTC】Amazon Kinesis Video Streams WebRTC Control Plane API 深度解析
https·webrtc·control plane
天黑请闭眼10 小时前
华为对象存储:nginx代理临时访问地址后访问报错:Authentication Failed
nginx·华为