您的当前位置:首页正文

[转载]nginx配置http强制跳转https

来源:华拓网

rewrite 方法

server {  
    listen  80;
    server_name docs.lvrui.io
      
    rewrite ^(.*)$  https://$host$1 permanent;  
}

server {
    listen 443 ssl;
    server_name docs.lvrui.io;
    index index.html index.htm;
    access_log  /var/log/nginx/docs.log  main;
    ssl on;
    ssl_certificate 
    ssl_certificate_key  
    error_page 404 /404.html;
    location / {
        root 
    }
}

497 状态码

error code 497: normal request was sent to HTTPS

server {
    listen 80;
    server_name docs.lvrui.io
    
    error_page 497  https://$host$uri?$args;
}

server {
    listen 443 ssl;
    server_name docs.lvrui.io;
    index index.html index.htm;
    access_log  /var/log/nginx/docs.log  main;
    ssl on;
    ssl_certificate 
    ssl_certificate_key  
    error_page 404 /404.html;
    location / {
        root 
    }
}

index.html 刷新网页

$ curl  -vv 
* Rebuilt URL to: 
*   Trying 220.181.57.217...
* TCP_NODELAY set
* Connected to  (220.181.57.217) port 80 (#0)
> GET / HTTP/1.1
> Host: 
> User-Agent: curl/7.51.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Sat, 01 Apr 2017 06:32:35 GMT
< Server: Apache
< Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT
< ETag: "51-47cf7e6ee8400"
< Accept-Ranges: bytes
< Content-Length: 81
< Cache-Control: max-age=86400
< Expires: Sun, 02 Apr 2017 06:32:35 GMT
< Connection: Keep-Alive
< Content-Type: text/html
< 
<html>
<meta http-equiv="refresh" 
</html>
* Curl_http_done: called premature == 0
* Connection #0 to host  left intact

# index.html
<html>  
    <meta http-equiv="refresh" content="0;url=https://docs.lvrui.io/">
</html>

server {
    listen 80;
    server_name docs.lvrui.io;
    
    location / {
        # 将 index.html 文件放到下面的目录下
        root 
    }
}

server {
    listen 443 ssl;
    server_name docs.lvrui.io;
    index index.html index.htm;
    access_log  /var/log/nginx/docs.log  main;
    ssl on;
    ssl_certificate 
    ssl_certificate_key  
    error_page 404 /404.html;
    location / {
        root 
    }
}