Nginx - 将其余别名域重定向到 OpenShift 中的主域

Nginx - 将其余别名域重定向到 OpenShift 中的主域

出于 SEO 目的,我尝试将 OpenShift 中某个应用程序的几个别名域永久重定向到主域。该应用程序正在运行Openshift Nginx 墨盒

我目前有:

server {
    listen  <%= ENV['OPENSHIFT_NGINX_IP'] %>:<%= ENV['OPENSHIFT_NGINX_PORT'] %>;    

    root    <%= ENV['OPENSHIFT_REPO_DIR'] %>/public;

    location / {
        index  index.html index.htm;
    }

    server_name appname-domain.rhcloud.com;
}

server {
    listen  <%= ENV['OPENSHIFT_NGINX_IP'] %>:<%= ENV['OPENSHIFT_NGINX_PORT'] %>;    

    root    <%= ENV['OPENSHIFT_REPO_DIR'] %>/public;

    location / {
        index  index.html index.htm;
    }

    server_name main-alias.domain.com;
}

server {
    listen  <%= ENV['OPENSHIFT_NGINX_IP'] %>:<%= ENV['OPENSHIFT_NGINX_PORT'] %>;    

    server_name _;

    return 301 $scheme://main-alias.domain.com$request_uri;
}

返回的 HTTP 标头为:

GET / HTTP/1.1
User-Agent: curl/7.35.0
Host: another-alias.domain.com
Accept: */*
HTTP/1.1 301 Moved Permanently
Date: Thu, 17 Nov 2016 00:43:18 GMT
* Server nginx is not blacklisted
Server: nginx
Content-Type: text/html
Content-Length: 178
Location: http://another-alias.domain.com/

如您所见,Location 标头没有指向正确的 URL。

答案1

我可以通过删除 request_uri 变量来解决这个问题。但如果必须重定向参数,那么这不是一个可接受的答案。

server {
    listen  <%= ENV['OPENSHIFT_NGINX_IP'] %>:<%= ENV['OPENSHIFT_NGINX_PORT'] %> default_server;    

    server_name _;

    return 301 http://main-alias.domain.com;
}

server {
    listen  <%= ENV['OPENSHIFT_NGINX_IP'] %>:<%= ENV['OPENSHIFT_NGINX_PORT'] %>;    

    root    <%= ENV['OPENSHIFT_REPO_DIR'] %>/public;

    location / {
        index  index.html index.htm;
    }

    server_name main-alias.domain.com;
}

相关内容