将https://重定向到https:// www

时间:2016-05-26 12:24:30

标签: nginx https url-rewriting forge

我有一台带有DigitalOcean的ubuntu服务器,使用forge.laravel.com进行管理

我在nginx.conf文件中尝试了很多不同的方法,将所有可能的方案重定向到我的域https://www.domain.com

https://www.domain.com = https://www.domain.com -- GREAT
http://domain.com = https://www.domain.com -- GREAT
http://www.domain.com = https://www.domain.com -- GREAT
https://domain.com = https://domain.com -- This should redirect to the www.

这是我的nginx.conf的一部分

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

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

    server {
        server_name domain.com;
        return 301 $scheme://www.domain.com$request_uri; 
    }

    server {
        listen 443 ssl;
        server_name domain.com;
        root /home/forge/default/public;

.... other nginx.conf configuration

谁能告诉我我做错了什么?我尝试了很多种组合。

1 个答案:

答案 0 :(得分:1)

替换

server {
    server_name domain.com;
    return 301 $scheme://www.domain.com$request_uri; 
}

server {
    listen 443 ssl;
    server_name domain.com;
    root /home/forge/default/public;

通过

server {
    listen 443 ssl;
    server_name domain.com;
    return 301 https://www.domain.com$request_uri; 
    #INSERT HERE CERTIFICATES DIRECTIVES

}

server {
    listen 443 ssl;
    server_name www.domain.com;
    root /home/forge/default/public;