Nginx 强制跳转到HTTPS

莫南 | |被浏览65次| -1
|2018-05-28 22:45:35

Nginx 转 HTTPS

满意回答

2018-05-28 22:45:36

方法一:


rewrite ^(.*)$ https://$host$1 permanent;


方法二:


适用于 80端口和443 配置在同一个 server{}内

if ($server_port = 80) {
rewrite ^(.*)$ https://$host$1 permanent;
}

其他情况, 站点绑定很多域名,只让某个域名跳转:

set $redirect_https 1;
if ($server_port = 80) {
set $redirect_https "${redirect_https}2";
}
if ($http_host = 'www.xorg.cn') {
set $redirect_https "${redirect_https}3";
}
if ($http_host = 'xorg.cn') {
set $redirect_https "${redirect_https}3";
}
if ($redirect_https = "123") {
rewrite ^(.*)$ https://$host$1 permanent;
}


配置示例:


server {
listen       80;
server_name  www.xorg.cn;
rewrite ^ https://$http_host$request_uri? permanent;
}
server {
listen 443;
ssl on;
ssl_certificate /etc/ssl/cacert.pem;
ssl_certificate_key /etc/ssl/privkey.pem;
server_name www.xorg.cn;  
server_tokens off;
location / {
fastcgi_param   HTTPS               on;
fastcgi_param   HTTP_SCHEME         https;
}
}

一生二

 |采纳率:64%

您需要登录后才可以回复

全部回答(1)

  • 2018-05-28 22:45:36

    方法一:


    rewrite ^(.*)$ https://$host$1 permanent;


    方法二:


    适用于 80端口和443 配置在同一个 server{}内

    if ($server_port = 80) {
    rewrite ^(.*)$ https://$host$1 permanent;
    }

    其他情况, 站点绑定很多域名,只让某个域名跳转:

    set $redirect_https 1;
    if ($server_port = 80) {
    set $redirect_https "${redirect_https}2";
    }
    if ($http_host = 'www.xorg.cn') {
    set $redirect_https "${redirect_https}3";
    }
    if ($http_host = 'xorg.cn') {
    set $redirect_https "${redirect_https}3";
    }
    if ($redirect_https = "123") {
    rewrite ^(.*)$ https://$host$1 permanent;
    }


    配置示例:


    server {
    listen       80;
    server_name  www.xorg.cn;
    rewrite ^ https://$http_host$request_uri? permanent;
    }
    server {
    listen 443;
    ssl on;
    ssl_certificate /etc/ssl/cacert.pem;
    ssl_certificate_key /etc/ssl/privkey.pem;
    server_name www.xorg.cn;  
    server_tokens off;
    location / {
    fastcgi_param   HTTPS               on;
    fastcgi_param   HTTP_SCHEME         https;
    }
    }

一个提问,一份期待;一条回答,一份回应。知识问与答,连接你和我!

-- 来自问答管理员 admin

返回顶部