hi,大家好,我是终南有客,一个热爱自由的程序员……
想要获取更多信息,请关注(终南有客)公众号,了解最新消息
新站SEO的朋友注意了,经本人测试,使用404强制跳转会导致页面无收录,因为爬虫不会跳转,比如访问https://justmyfreedom.com/地址会直接报404,而不会去管后面跳转的地址,直接显示无页面.
一.需求简介
基于nginx搭建了一个https访问的虚拟主机,监听的域名是justmyfreedom.com,但是很多用户不清楚https和http的区别,会很容易敲成https://justmyfreedom.com,这时会报出404错误,所以我需要做基于justmyfreedom.com域名的http向https的强制跳转
二.http跳转到https配置方式
进过网上查询,总结了一下三种方式,可以根据需求进行设置.
第一种:nginx的rewrite方法
这应该是大家最容易想到的方法.
配置
在NGINX的配置文件nginx.conf中配置,监听80端口,将http请求重写到https上即可.
server { listen 80; server_name justmyfreedom.com; rewrite ^(.*)$ https://$host$1 permanent;}
搭建云服务器完成后,就可以将http://justmyfreedom.com/的请求全部重写到https://justmyfreedom.com/上了
第二种:nginx的497状态码
error code 497
497 - normal request was sent to HTTPS
解释:当此虚拟站点只允许https访问时,当用http访问时nginx会报出497错误码,利用error_page命令将497状态码的链接重定向到https://justmyfreedom.com/这个域名上
配置
注意,前两中方法都是从网上获取,未试验过,代码可能有误,本人使用第三种,具体可参考第三种配置方式进行配置.
server { listen 443; #ssl端口 listen 80; #用户习惯用http访问,加上80,后面通过497状态码让它自动跳到443端口 server_name justmyfreedom.com; #为一个server{......}开启ssl支持 ssl on; #指定PEM格式的证书文件 ssl_certificate cert/a.pem; #指定PEM格式的私钥文件 ssl_certificate_key cert/a.key; #让http请求重定向到https请求 error_page 497 https://$host$uri?$args;}
第三种:index.html刷新网页
上述两种方法均会耗费服务器的资源,我们用其他公司公司是如何实现http跳转到https的跳转.测试在浏览器中输入将baidu.com,发现很巧妙的利用meta的刷新作用,将baidu.com跳转到www.baidu.com.因此我们可以基于http://justmyfreedom.com的虚拟主机路径下也写一个index.html,内容就是http向https的跳转
index.html
<html>
<meta http-equiv="refresh" content="0;url=https://justmyfreedom.com/">
</html>
nginx虚拟主机配置
具体nginx.conf文件配置,可参考:
#user nobody;
worker_processes 1;#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connections 1024;
}http {include mime.types;default_type application/octet-stream;#log_format main '$remote_addr - $remote_user [$time_local] "$request" '# '$status $body_bytes_sent "$http_referer" '# '"$http_user_agent" "$http_x_forwarded_for"';#access_log logs/access.log main;sendfile on;#tcp_nopush on;keepalive_timeout 65;server {listen 80;server_name justmyfreedom.com;# 开启gzipgzip on;# 低于1KB的资源不压缩gzip_min_length 1024;# 压缩级别 1-9 建议4左右,越高越消耗CPUgzip_comp_level 3;# 压缩那些资源,多个空格隔开,不建议压缩图片gzip_types text/plain application/javascript application/x-java scripttext/java scripttext/xml text/css;# 配置禁用gzip条件,支持正则,此表示IE6及下不启用,低版本不支持gzip_disable "MSIE [1-6]\.";# 是否添加“Vary: Accept-Encoding”响应头gzip_vary on;#charset koi8-r;#access_log logs/host.access.log main;charset utf-8;location / {root home/web/myweb/WLBlogDjango;index index.html index.htm;}# 不要忘记配置跳转error_page 404 https://justmyfreedom.com/;# redirect server error pages to the static page /50x.html#error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}# HTTPS server#server {listen 443 ssl;server_name justmyfreedom.com;# 开启gzipgzip on;# 低于1KB的资源不压缩gzip_min_length 1024;# 压缩级别 1-9 建议4左右,越高越消耗CPUgzip_comp_level 3;# 压缩那些资源,多个空格隔开,不建议压缩图片gzip_types text/plain application/javascript application/x-java scripttext/java scripttext/xml text/css;# 配置禁用gzip条件,支持正则,此表示IE6及下不启用,低版本不支持gzip_disable "MSIE [1-6]\.";# 是否添加“Vary: Accept-Encoding”响应头gzip_vary on;ssl on; ssl_certificate cert/a.pem;ssl_certificate_key cert/a.key;#ssl_session_cache shared:SSL:1m;ssl_session_timeout 5m;# ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4:!DH:!DHE;ssl_protocols TLSv1 TLSv1.1 TLSv1.2;ssl_prefer_server_ciphers on;location / {#root html;#index index.html index.htm;include uwsgi_params;uwsgi_pass 127.0.0.1:8899;uwsgi_param UWSGI_SCRIPT WLBlogDjango.wsgi;uwsgi_param UWSGI_CHDIR /home/web/myweb/WLBlogDjango; #项目路径}location /static/ {alias /home/web/myweb/WLBlogDjango/static/; #静态资源路径}}}
注:
上述三种方法均可以实现基于nginx强制将http请求跳转到https请求,大家可以评价一下优劣或者根据实际需求进行选择。参考时一定要结合自己的实际情况,我很多配置基于项目安全考虑做了删减.
想要了解更多信息,请关注(终南有客)公众号