Nginx HTTPS www 到非 www 重定向问题

Jac*_*and 2 nginx https

我在使用 nginx 配置从 HTTPS www 重定向到 HTTPS non-www 时遇到了一些问题。我正在按照(删除“www”并使用 nginx 重定向到“https”)提供的说明进行操作。

现在有以下工作:

但这不会:

我最终得到了“欢迎使用 nginx!” 页。我没有任何其他 nginx 配置文件/sites-enabled。任何想法为什么会这样做?

我的配置文件在下面。

upstream redemfit {
    server unix:/srv/redemfit/run/gunicorn.sock fail_timeout=0;
}

server {
    listen         80;
    server_name    www.redemfit.com redemfit.com;
    rewrite        ^ https://redemfit.com$request_uri? permanent;
}

server {
    listen 443;
    ssl on;
    ssl_certificate /etc/ssl/private/redemfit-bundle.crt;
    ssl_certificate_key /etc/ssl/private/redemfit.key;
    ssl_protocols SSLv3 TLSv1;
    ssl_ciphers ALL:!aNULL:!ADH:!eNULL:!LOW:!EXP:RC4+RSA:+HIGH:+MEDIUM;
    server_name www.redemfit.com
    rewrite ^ https://redemfit.com$request_uri? permanent;
}

server {
    listen   443;

    ssl on;
    ssl_certificate /etc/ssl/private/redemfit-bundle.crt;
    ssl_certificate_key /etc/ssl/private/redemfit.key;
    ssl_protocols SSLv3 TLSv1;
    ssl_ciphers ALL:!aNULL:!ADH:!eNULL:!LOW:!EXP:RC4+RSA:+HIGH:+MEDIUM;

    server_name redemfit.com;

    client_max_body_size 4G;

    access_log /srv/redemfit/logs/nginx-access.log;
    error_log /srv/redemfit/logs/nginx-error.log;

    gzip on;
    gzip_disable "msie6";

    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    location /static {
        auth_basic off;
        root /srv/redemfit/static_collected;
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        if (!-f $request_filename) {
            proxy_pass http://redemfit;
            break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Glu*_*eon 5

尝试server {}完全删除www.redemfit.com 的HTTPS块并将以下代码添加到主 HTTPS 块中:

if ($host = 'www.redemfit.com' ) {
          rewrite  ^/(.*)$  https://redemfit.com/$1  permanent;
}
Run Code Online (Sandbox Code Playgroud)

我希望在写这篇文章时你打错了:

server { listen 443; ssl on; ssl_certificate /etc/ssl/private/redemfit-bundle.crt; ssl_certificate_key /etc/ssl/private/redemfit.key; ssl_protocols SSLv3 TLSv1; ssl_ciphers ALL:!aNULL:!ADH:!eNULL:!LOW:!EXP:RC4+RSA:+HIGH:+MEDIUM; server_name www.redemfit.com # <- Missing semicolon rewrite ^ https://redemfit.com$request_uri? permanent; }