nGinx 将所有 URL 重定向到 https://

nGinx 将所有 URL 重定向到 https://

我正在尝试让以下方法工作,这样无论你在 URL 栏中输入什么,你总是会转到“ https://www.”。到目前为止,www. 不会转到https://www.,也https://不会转到https://www.

下面是我的代码。需要做什么?

server { 
        listen  80;
        server_name  example.com;
        rewrite ^(.*) https://www.example.com$1 permanent;
}

    server {
        listen       80;
        server_name  www.example.com;
        #rewrite ^(.*) https://www.example.com$1 permanent;
        root /home/knownsrv/public_html;

        listen       443 ssl;
        #server_name  example.com www.example.com;
        #root /home/knownsrv/public_html;
        ssl_certificate      /root/knownsrv.crt;
        ssl_certificate_key  /root/knownsrv.key;
        #ssl_client_certificate /root/chain.cer;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /home/knownsrv/public_html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
location ~ \.php$ {
            root           /home/knownsrv/public_html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
}
}

答案1

你把这件事想得太复杂了....

要将带有或不带有 www 的 HTTP 重定向到带有 www 的 HTTPS:

server {
        listen 80;
        server_name www.example.com example.com;
        return 301 https://www.example.com$request_uri;
}

这样就只剩下 HTTPS 不带 www 的情况了,可以这样解决:

server {
    listen 443 ssl;
    server_name example.com;
    return 301 https://www.example.com$request_uri;
    ssl_certificate /root/knownsrv.crt;
    ssl_certificate_key /root/knownsrv.key;
}

然后只需为 添加 ssl vhost 即可www.example.com。其他所有内容(即缺少 www 和/或非 ssl)都将重定向到它。无需if(这会严重降低 nginx 性能)。

答案2

在服务器块中添加:

if ($host !~* ^www\.) {
        rewrite ^(.*)$ https://www.website.com$1 permanent;
}

因此你的配置看起来应该是这样的:

server {
    listen  80;
    server_name website.com www.website.com;
    rewrite ^(.*) https://www.website.com$1 permanent;
}

server {
    listen 443 ssl;
    server_name website.com www.website.com;
    root /home/knownsrv/public_html;

    if ($host !~* ^www\.) {
        rewrite ^(.*)$ https://www.website.com$1 permanent;
    }

    ssl_certificate /root/knownsrv.crt;
    ssl_certificate_key /root/knownsrv.key;

    location / {
        root /home/knownsrv/public_html;
        index index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

    location ~ \.php$ {
        root            /home/knownsrv/public_html;
        fastcgi_pass    127.0.0.1:9000;
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include         fastcgi_params;
    }
}

相关内容