Nginx配置HTTP强制跳转到HTTPS的解决办法

 更新时间:2023年08月09日 11:23:57   作者:abiao1981  
这篇文章主要给大家介绍了关于Nginx配置HTTP强制跳转到HTTPS的解决办法,当Nginx配置https后通常需要将用户http请求强制跳转到https,需要的朋友可以参考下

前言

https 访问我们的测试域名 https://www.xxx.com 站点,但是当我们直接在浏览器地址栏中直接输入 www.xxx.com 的时候却发现进入的是 http 协议的网站,这与我们的初衷不一致。

由于浏览器默认访问域名使用的是80端口,而当我们使用SSL证书后,网站的端口就变成了443,所以当我们直接在浏览器中输入网址 www.xxx.com 的时候进入的是 80 端口的 HTTP 站点而不是 443 端口的 HTTPS 站点。

解决方法

这里提供两种 http 跳转到 https 的方法:

1. 使用nginx的 rewrite 将请求过来的 http URL直接重写成 https

server {
listen 80;
#填写绑定证书的域名
server_name www.xxx.com;
#强制将http的URL重写成https
rewrite ^(.*) https://$server_name$1 permanent;
}

2. 使用301重定向的方式将 http 的请求重定向到 https 上

server {
listen 80;
#填写绑定证书的域名
server_name www.xxx.com;
#把http的域名请求转成https
return 301 https://$host$request_uri;
}

3.完整的代码

# https配置
server {
listen 443 ssl;
server_name www.xxx.com xxx.com;
#ssl on; #开启ssl支持
ssl_certificate /data/ssl/www.xxx.com.pem; #指定服务器证书路径
ssl_certificate_key /data/ssl/www.xingguangshe.com.key; #指定私钥证书路径
ssl_session_timeout 5m;
#ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
#ssl_prefer_server_ciphers on;
root /myweb/new/xxx.com;
location / {
index index.php index.html index.htm;
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
break;
}
}
location ~ \.php/?.*$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.(?:php|phar))(/.*)$;
fastcgi_intercept_errors on;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
#fastcgi_pass php-fpm; #uquq use this
}
location ~ /\.ht {
deny all;
}
}
#http配置
server {
listen 80;
#listen somename:8080;
server_name www.xxx.com xxx.com;
client_max_body_size 80m;
#error_page 404 /data/ymg280/404.html;
#error_page 500 502 503 504 /errors/default/50x.html;
rewrite ^(.*) https://$server_name$1 permanent;
if ($host != 'ww.xxx.com'){
#rewrite ^/(.*)$ http://www.xxx.com/$1 permanent;
}
root /myweb/new/xxx.com;
location / {
index index.php index.html index.htm;
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
break;
}
}
#pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php/?.*$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.(?:php|phar))(/.*)$;
fastcgi_intercept_errors on;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
#fastcgi_pass php-fpm;
}
#deny access to .htaccess files, if Apache's document root
location ~ /\.ht {
deny all;
}
}

总结

到此这篇关于Nginx配置HTTP强制跳转到HTTPS的文章就介绍到这了,更多相关Nginx强制跳转HTTPS内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • nginx服务器中access_log日志分析与配置详解

    nginx服务器中access_log日志分析与配置详解

    通过访问日志,可以知晓用户的地址,网站的哪些部分最受欢迎,用户的浏览时间,对大多数用户用的的浏览器做出针对性优化。下面这篇文章主要给大家介绍了关于nginx服务器中access_log日志分析与配置的相关资料,需要的朋友可以参考下。
    2017-12-12
  • 在Linux中查看Apache或Nginx服务状态的详细步骤

    在Linux中查看Apache或Nginx服务状态的详细步骤

    在Linux中,查看Apache或Nginx服务的状态通常涉及到使用系统管理工具或特定于这些Web服务器的命令,以下是如何查看Apache和Nginx服务状态的详细步骤,需要的朋友可以参考下
    2024-03-03
  • nginx运行报错:unknown directive “stream“的解决方案

    nginx运行报错:unknown directive “stream“的解决方案

    这篇文章主要给大家介绍了nginx 运行报错:unknown directive "stream"的原因,主要是因为没有安装stream模块导致的,我们只需要编译安装一下stream模块即可解决这个问题,文中有详细的解决方案,需要的朋友可以参考下
    2023-09-09
  • 分析nginx日志并屏蔽采集者ip(nginx屏蔽ip配置实例)

    分析nginx日志并屏蔽采集者ip(nginx屏蔽ip配置实例)

    这篇文章主要介绍了分析nginx日志并屏蔽采集者ip(nginx屏蔽ip配置实例),本文先是讲解了分析需要屏蔽日志的方法,然后讲解了Nginx中屏蔽IP的配置方法,需要的朋友可以参考下
    2015-02-02
  • nginx有哪些常规调优手段详解

    nginx有哪些常规调优手段详解

    性能调优就是用更少的资源提供更好的服务,成本利益最大化,下面这篇文章主要给大家介绍了关于nginx有哪些常规调优手段的相关资料,需要的朋友可以参考下
    2023-01-01
  • 使用nginx设置代理服务器

    使用nginx设置代理服务器

    今天小编就为大家分享一篇关于使用nginx设置代理服务器,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • 在nginx中部署https服务的详细步骤

    在nginx中部署https服务的详细步骤

    Web服务器一般指网站服务器,可以处理浏览器等Web客户端的请求并返回相应响应,也可以放置网站文件,让全世界浏览;可以放置数据文件,让全世界下载,本文主要介绍nginx中部署https服务的具体流程,也是搭建web的开端与主要步骤之一
    2023-10-10
  • CentOS 6.3安装配置Nginx方法

    CentOS 6.3安装配置Nginx方法

    这篇文章主要介绍了CentOS 6.3安装配置Nginx方法,需要的朋友可以参考下
    2014-11-11
  • 解决Nginx location中配置proxy_pass转发时斜线‘/‘导致404问题

    解决Nginx location中配置proxy_pass转发时斜线‘/‘导致404问题

    这篇文章主要介绍了解决Nginx location中配置proxy_pass转发时斜线‘/‘导致404问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-05-05
  • Nginx配置代理gRPC的方法

    Nginx配置代理gRPC的方法

    本篇文章主要介绍了Nginx配置代理gRPC的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03

最新评论