Nginx如何重定向 https,成功http80自动跳转https443!

/usr/local/nginx/conf/nginx.conf

#这个位置进行配置

server {
  listen   80;
  server_name  derfoe.cn;
  return   301 https://$server_name$request_uri;
}

如果已经配置好了SSL 以上配置就可以成功

如果未配置SSL,要先配置SSL证书

以下的 只需要更换域名,还有一个网站路径defult

此域名以 derfoe.cn 域名为例!

######################## default ############################
#只需要将下面部分替换掉,从这开始   ,只需要改SSL证书文件路径 ##########################
 



#重定向句子

server {
  listen   80;
  server_name  derfoe.cn;
  return   301 https://$server_name$request_uri;
}



 
server {
    listen 443 ssl;
    server_name _;
    access_log /data/wwwlogs/access_nginx.log combined;
    # ssl证书地址
    ssl_certificate     /usr/local/nginx/cert/derfoe.cn.pem;  # pem文件的路径   
    ssl_certificate_key  /usr/local/nginx/cert/derfoe.cn.key; # key文件的路径
    
    # ssl验证相关配置
    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 /data/wwwroot/derfoe.cn;  #必须更换到https的网站路径


    index index.html index.htm index.php;
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    location /nginx_status {
      stub_status on;
      access_log off;
      allow 127.0.0.1;
      deny all;
    }
    location ~ [^/]\.php(/|$) {
      #fastcgi_pass remote_php_ip:9000;
      fastcgi_pass unix:/dev/shm/php-cgi.sock;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
      expires 30d;
      access_log off;
    }
    location ~ .*\.(js|css)?$ {
      expires 7d;
      access_log off;
    }
    location ~ ^/(\.user.ini|\.ht|\.git|\.svn|\.project|LICENSE|README.md) {
      deny all;
    }
  }




 ###################  结束  ##########################
########################## vhost #############################
  include vhost/*.conf;
}

别忘记要输入一串代码:

systemctl restart nginx #让代码生效

成功 访问域名 :derfoe.cn ,自动跳转 https:derfoe.cn

你可能感兴趣的:(nginx,https,服务器)