首页 > 系统相关 >Nginx http重定向https

Nginx http重定向https

时间:2023-06-29 10:56:39浏览次数:37  
标签:http name ssl server Nginx https com listen

Nginx配置同一个域名http与https两种方式都可访问,证书是阿里云上免费申请的

 

server
{
listen 80;
listen 443 ssl;
ssl on;
server_name 域名;
index index.html index.htm index.php default.html default.htm default.php;
ssl_certificate /usr/local/nginx/cert/21402058063066221.pem; //下载申请后阿里ssh提供的pem
ssl_certificate_key /usr/local/nginx/cert/21402058063066221.key;//下载申请后阿里ssh提供的key
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;

root /home/wwwroot/网站目录;

include laravel.conf; //好吧,这里是laravel配置,不一定合适您哈,请或略
#error_page 404 /404.html;
include enable-php.conf;

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}

location ~ .*\.(js|css)?$
{
expires 12h;
}

access_log /home/wwwlogs/airclass.mime.org.cn.log;
}

 

关键在于上面的listen 80;
listen 443 ssl; 开启80端口

当然,这样玩就没有啥意义了,既然是https,就完全没必要http传输数据啦.我们必须把所有http请求转发到https,

把http重定向到https使用了nginx的重定向命令。那么应该如何写重定向?之前老版本的nginx可能使用了以下类似的格式。
也就是再添加一个虚拟机server,80端口一个

server {
listen 80;
server_name www.domain.com;
rewrite ^/(.*) https://$server_name$1 permanent; #跳转到Https
}

重写依旧不同版本可能如下

rewrite ^/(.*)$ https://domain.com/$1 permanent;

或者

rewrite ^ https://domain.com$request_uri? permanent;

现在nginx新版本已经换了种写法,上面这些已经不再推荐。现在网上可能还有很多文章写的是第一种。

下面是nginx http页面重定向到https页面最新支持的写法:

 

server {
listen    80;
server_name domain.com;
return    301 https://$server_name$request_uri;
}

server {
listen    443 ssl;
server_name domain.com;

}

 

但是我的nginx/1.10.0好像跑不起来,也许不支持这种写法吧...
下面是基于http转https的完整配置:

 

server
{
#listen 80;
listen 443;
ssl on;
server_name domain.com; //你的域名
index index.html index.htm index.php default.html default.htm default.php;
ssl_certificate /usr/local/nginx/cert/user.medsci-tech.com/214020580630662.pem;
ssl_certificate_key /usr/local/nginx/cert/user.medsci-tech.com/214020580630662.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;

root /home/wwwroot/web/public;//项目根目录

include laravel.conf;
#error_page 404 /404.html;
include enable-php.conf;

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}

location ~ .*\.(js|css)?$
{
expires 12h;
}

}
server {
listen 80;
server_name domain.com;
rewrite ^/(.*) https://$server_name$request_uri? permanent;
}


标签:http,name,ssl,server,Nginx,https,com,listen
From: https://www.cnblogs.com/Fooo/p/17513429.html

相关文章

  • nginx完全卸载删除
    nginx卸载其实很简单,只需要两步即可完成!第一步:输入以下指令全局查找nginx相关的文件:sudofind/-namenginx*第二步:删除查找出来的所有nginx相关文件sudorm-rffile此处跟查找出来的nginx文件说明:全局查找往往会查出很多相关文件,但是前缀基本都是相同,后面不同的部分......
  • Linux安装nginx详细步骤
    Linux版本:CentOS7.664位Nginx版本:1.13.7下载地址:http://nginx.org/download/nginx-1.13.7.tar.gz在安装nginx前首先要确认系统中安装了gcc、pcre-devel、zlib-devel、openssl-devel。由于我这是刚在阿里云新买的服务器,所以什么都没有安装,下面开始安装:1.安装依赖包1.//......
  • Nginx 配置HTTP跳转到HTTPS
    https 访问我们的测试域名 https://www.xxx.com 站点,但是当我们直接在浏览器地址栏中直接输入 www.xxx.com 的时候却发现进入的是 http 协议的网站,这与我们的初衷不一致。 由于浏览器默认访问域名使用的是80端口,而当我们使用SSL证书后,网站的端口就变成了443,所以当我们直......
  • F5配置http请求自动跳转https到后端服务器
    F5配置http请求自动跳转https到后端服务器关联irules_sys_https_redirect实现跳转#HTTP_to_HTTPS_iRule自定义whenHTTP_REQUEST{HTTP::redirect"https://[HTTP::host][HTTP::uri]"}#FactoryF5httpsredirectiRule官方定义whenHTTP_REQUEST{HTTP::redirecthttps://[g......
  • Taurus .Net Core 微服务开源框架:Admin 插件【4-1】 - 配置管理-Kestrel【含https启用
    前言:继上篇:Taurus.NetCore微服务开源框架:Admin插件【3】-指标统计管理本篇继续介绍下一个内容:1、系统配置节点:App- Config 界面界面图如下:双击节点即可进入修改模式,如: 修改说明: 不勾选持久化,该配置当前生效,下次重次失效,修改后配置值旁边带有【temp】说明文......
  • Kong入门学习实践(6)HTTPS与TCP流代理
    最近在学习Kong网关,因此根据老习惯,我会将我的学习过程记录下来,一来体系化整理,二来作为笔记供将来翻看。由于我司会直接使用Kong企业版,学习过程中我会使用Kong开源版。本篇,我们学习快速配置HTTPS跳转与TCP流代理。HTTPS跳转配置HTTP协议虽然应用广泛,简单易用,但存在着巨大的安......
  • Servlet p3 HttpServletRequest对象
    HttpServletRequest对象基本介绍​ HttpServletRequest对象:HttpServletRequest对象专门用于封装HTTP请求消息,简称request对象。主要作用是用来接收客户端发送过来的请求信息,该对象是由Tomcat封装好传递过来。​ HttpServletRequest接口是ServletRequest的子接口。HTTP请......
  • nginx的作用
    Nginx(发音为“engine-x”)是一个高性能的开源Web服务器和反向代理服务器。它被广泛用于构建和部署现代化的Web应用程序。Web服务器静态文件服务:Nginx可以高效地提供静态文件服务,如HTML、CSS、JavaScript、图像等。它能够直接从磁盘中读取静态文件并发送给客户端,避免了将请求......
  • nginx设置为开机自启
    1NGINX安装1.1使用SCP工具上传nginx-1.16.1.tar.gz安装包到/usr/local/目录1.2安装 (该步骤在有网络的条件下可以执行,离线安装可以配置yum源后安装或下载安装包上传安装)1.3安装依赖[root@hadoop4~]#yum -y installgccgcc-c++pcrepcre-develzlibzlib-developensslo......
  • nethttp和gin 路由
    net/http路由注册functest1(){http.HandleFunc("/",func(whttp.ResponseWriter,r*http.Request){fmt.Fprintf(w,"Helloworld!")})err:=http.ListenAndServe(":9001",nil)iferr!=nil{log.F......