Nginx重定向跨浏览器不一致

时间:2017-04-09 20:38:25

标签: redirect nginx browser

我有一个顶级域名。我们称之为example.com。我还有三个子域:www.example.comapp.example.comstatic.app.example.com

我希望example.com重定向到www.example.com。 我还希望在保留域和路径的同时将所有http流量重定向到https。

这些是我的nginx配置文件(抱歉,它有点长):

###############  app.example.com ###################

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

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    include snippets/ssl-example.com.conf;
    include snippets/ssl-params.conf;

    server_name app.example.com;

    location / {
        proxy_pass http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    location /assets {
        alias /var/www/exampleapp/dist/public;
    }

    location /.well-known {
        alias /var/www/example/.well-known;
    }
}

###############  static.app.example.com ###################

server {
    listen 80;
    listen [::]:80;
    server_name static.app.example.com;
    return 301 https://static.app.example.com$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    include snippets/ssl-example.com.conf;
    include snippets/ssl-params.conf;

    server_name static.app.example.com;

    location / {
        alias /var/www/exampleapp/public/;
    }

    location /.well-known {
        alias /var/www/example/.well-known;
    }
}

###############  www.example.com & example.com ###################

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

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    include snippets/ssl-example.com.conf;
    include snippets/ssl-params.conf;

    server_name example.com;
    return 301 $scheme://www.example.com$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    include snippets/ssl-example.com.conf;
    include snippets/ssl-params.conf;

    server_name www.example.com;

    location / {
        proxy_pass http://localhost:5500;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    location /assets {
        alias /var/www/example/dist/public;
    }

    location /.well-known {
        alias /var/www/example/.well-known;
    }
}

现在这里是奇怪的部分。这在桌面chrome和firefox上运行得非常好。但在移动镀铬和边缘,它做了一些奇怪的事情。它从example.com重定向到app.example.com。我无法弄清楚它是什么。我已经尝试清除浏览器缓存并使用nginx设置但没有任何帮助。

2 个答案:

答案 0 :(得分:0)

因此,浏览器不一致只是缓存。即使我清除了浏览器缓存,它仍然缓存301重定向。显然,破坏重定向缓存的最佳方法是将随机查询字符串附加到网址。

就错误的重定向而言,一位朋友指出,如果您没有为裸域指定处理程序,那么将使用第一个子域,这就是http请求所发生的事情。当我听到这个时,https很好。我刚刚在端口80上为裸域添加了另一个服务器块。这解决了这个问题。

答案 1 :(得分:0)

您在端口80上缺少example.com的服务器。因此,使用http连接到example.com的任何浏览器都将使用默认服务器(恰好是{{1}服务器块)。

您应该将上一个app.example.com http块更改为:

server