在 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 版本。

相关推荐
PanZonghui2 小时前
Centos项目部署之Nginx 的安装与卸载
linux·nginx
THMOM912 小时前
TinyWebserver学习(9)-HTTP
网络协议·学习·http
en-route5 小时前
HTTP 缓存
网络协议·http·缓存
隆里卡那唔14 小时前
在dify中通过http请求neo4j时为什么需要将localhost变为host.docker.internal
http·docker·neo4j
charlee4414 小时前
nginx部署发布Vite项目
nginx·性能优化·https·部署·vite
~山有木兮15 小时前
LiteHub中间件之限流实现
网络·http·中间件
游戏开发爱好者816 小时前
iOS App首次启动请求异常调试:一次冷启动链路抓包与初始化流程修复
websocket·网络协议·tcp/ip·http·网络安全·https·udp
2501_9151063216 小时前
频繁迭代下完成iOS App应用上架App Store:一次快速交付项目的完整回顾
websocket·网络协议·tcp/ip·http·网络安全·https·udp
CRMEB定制开发16 小时前
【实战】CRMEB Pro 企业版安装教程(附 Nginx 反向代理配置 + 常见问题解决)
nginx·商城系统·微信商城·crmeb
00后程序员张1 天前
免Mac上架实战:全平台iOS App上架流程的工具协作经验
websocket·网络协议·tcp/ip·http·网络安全·https·udp