接上文
阿里云支持https请求配置 http://new.xiaosongit.com/article/504.html Nginx支持Https协议 http://new.xiaosongit.com/article/503.html |
其实我们配置好 https以后,直接输入域名并不能直接跳转到 https上去,除非把https:// 这个也输入上,才可以正常跳转。
如何直接输入域名强制跳转到 https?
第一种方法:利用497错误端口跳转
当网站只允许https访问时,当用http访问时nginx会报出497错误码
配置中 需要监听两个端口
指定 497 跳转地址
server {
listen 80; #处理http请求过来
listen 443; #处理https
ssl on;
#证书
ssl_certificate /data/www/smallnews/ssl/cert-1540869161424.crt;
#私钥
ssl_certificate_key /data/www/smallnews/ssl/cert-1540869161424.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
fastcgi_param HTTPS on;
fastcgi_param HTTP_SCHRMR https;
access_log /data/www/smallnews/httpsaccess.log;
server_name sp.xi******; #域名
#当网站只允许https访问时,当用http访问时nginx会报出497错误码
error_page 497 https://$host$uri?$args;
}以上配置完成后,重新启动nginx
第二种方法:网站添加了https证书后,当http方式访问网站时就会报404错误,所以需要做http到https的强制跳转设置.
这种做法,必须是需要两个conf配置文件,一个配置 80 端口的 一个配置 443 的
当端口不一样的时候,同一个域名 nignx是不会报错的。
配置1:
server {
listen 80; //这里监听的是80 切记
server_name sp.xi******;
index index.html index.php index.htm;
access_log /data/www/smallnews/access.log main;
error_log /data/www/smallnews/error.log;
rewrite ^(.*)$ https://$host$1 permanent; //这是ngixn早前的写法,现在还可以使用。
location ~ / {
root /data/www/smallnews;
index index.html index.php index.htm;
}
}
#看看https 443 端口如何配置server {
listen 443; #处理https
ssl on;
#证书
ssl_certificate /data/www/smallnews/ssl/cert-1540869161424.crt;
#私钥
ssl_certificate_key /data/www/smallnews/ssl/cert-1540869161424.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
fastcgi_param HTTPS on;
fastcgi_param HTTP_SCHRMR https;
access_log /data/www/smallnews/httpsaccess.log;
server_name sp.xi******; #域名 同 80端口域名保持一致
}有了以上的两个步骤,重新启动ngixn 服务器 。
-------------------------------------------------------
上面的跳转配置rewrite ^(.*)$ https://$host$1 permanent;
也可以改为下面
rewrite ^/(.*)$ http://dev.wangshibo.com/$1 permanent;
或者
rewrite ^ http://dev.wangshibo.com$request_uri? permanent;
-------------------------------------------------------
配置2
server {
listen 80;
server_name sp.xi******;
index index.html index.php index.htm;
return 301 https://$server_name$request_uri; //这是nginx最新支持的写法
location ~ / {
root /data/www/smallnews;
index index.html index.php index.htm;
}
}配置443
server {
listen 443; #处理https
ssl on;
#证书
ssl_certificate /data/www/smallnews/ssl/cert-1540869161424.crt;
#私钥
ssl_certificate_key /data/www/smallnews/ssl/cert-1540869161424.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
fastcgi_param HTTPS on;
fastcgi_param HTTP_SCHRMR https;
access_log /data/www/smallnews/httpsaccess.log;
server_name sp.xi******; #域名 同 80端口域名保持一致
}其实和第一种方法就差
return 301 https://$server_name$request_uri; //这是nginx最新支持的写法
配置3:这种方式适用于多域名的时候,即访问wangshibo.com的http也会强制跳转到https://dev.wangshibo.com上面
server {
listen 80;
server_name dev.wangshibo.com wangshibo.com *.wangshibo.com;
index index.html index.php index.htm;
access_log /usr/local/nginx/logs/8080-access.log main;
error_log /usr/local/nginx/logs/8080-error.log;
if ($host ~* "^wangshibo.com$") {
rewrite ^/(.*)$ https://dev.wangshibo.com/ permanent;
}
location ~ / {
root /var/www/html/8080;
index index.html index.php index.htm;
}
}
配置4:下面是最简单的一种配置
server {
listen 80;
server_name dev.wangshibo.com;
index index.html index.php index.htm;
access_log /usr/local/nginx/logs/8080-access.log main;
error_log /usr/local/nginx/logs/8080-error.log;
if ($host = "dev.wangshibo.com") {
rewrite ^/(.*)$ http://dev.wangshibo.com permanent;
}
location ~ / {
root /var/www/html/8080;
index index.html index.php index.htm;
}
}--------------------------------------------
以上是nginx配置说明
--------------------------------------------
--------------------------------------------
Apache配置说明
--------------------------------------------
Apache服务器:
如果需要整站跳转,则在网站的配置文件的<Directory>标签内,键入以下内容:
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [L,R]如果对某个目录做https强制跳转,则复制以下代码:
RewriteEngine on
RewriteBase /yourfolder
RewriteCond %{SERVER_PORT} !^443$
#RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [L,R]
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]如果只需要对某个网页进行https跳转,可以使用redirect 301来做跳转!redirect 301 /你的网页 https://你的主机+网页
--------------------------------------------
单独页面PHP页面跳转:
--------------------------------------------
添加在网站php页面内
if ($_SERVER["HTTPS"] <> "on")
{
$xredir="https://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
header("Location: ".$xredir);
}