nginx:[警告] 服务器名称包含可疑符号,且域名重定向较长

nginx:[警告] 服务器名称包含可疑符号,且域名重定向较长

在 Nginx 中重定向两个长域 URL 的最佳方法是什么,我想在两个域之间配置重定向,如下所示:

我确信之前已经有人问过这个问题,但我找不到有效的解决方案。

original link:

https://qwerty.test.com/education/7/abc-science

New link

https://www.test.com/education/7/abc-science

我尝试过这个:

server {
    listen 80;
    listen 443 ssl;
    server_name qwerty.test.com/education/7/abc-scienc;
    return 301 $scheme://www.test.com/education/7/abc-science$request_uri;
}

仍然收到一些错误

nginx: [warn] server name "education/7/abc-scienc
" has suspicious symbols in /etc/nginx/conf.d/redirect.conf:9

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

我是否需要使用这种方式将我的旧 URL 重定向到新的?

location = /content/unique-page-name {
  return 301 /new-name/unique-page-name;
}

有人能帮我解决这个问题吗?这里出了什么问题?

任何帮助都非常感谢!谢谢!

答案1

服务器名称应该仅仅是您的域名,而不是您的完整 URL。

这意味着你server_name应该是qwerty.test.com

然后您应该使用位置块进行正确的重定向,如下所示:

server {
    listen 80;
    listen 443 ssl;
    # Configure your SSL too!
    
    server_name query.test.com;

    location /education/7/abc-science {
        # Unique handler for the URL you want to redirect.
        return 301 $scheme://www.test.com/education/7/abc-science;
    }

    location / {
        # Ideally you need a site level redirect for *any* URI 
        # so that the request is sent to the different site 
        # on any other request URI.
        return 301 $scheme://www.test.com$request_uri;

        # Alternatively, do a different site configuration here for 
        # any other pages that don't redirect.
    }
}

相关内容