简体   繁体   English

Nginx从一个域重定向到另一个域?

[英]Nginx redirect from one domain to another?

I'm on the process of migrating the same app but to a different domain. 我正在将同一应用程序迁移到另一个域。

For the old domain, I've the routes as: 对于旧域,我将路由设置为:

http://app.example.com/app/users/sign_in?query=hello

I want it to be redirected to another domain omitting the app part as: 我希望将其重定向到另一个域,从而省略了app部分,因为:

http://app.newexample.com/users/sign_in?query=hello

I tried with: 我尝试了:

server {
  ...
  location /app {
    rewrite ^$scheme://app.sparkon.com/app(/.*)$ $1 last;
  }
  ...
}

I doesn't work. 我不工作 How to achieve this? 如何实现呢?

I had this issue about a year ago and spent a long time looking for solutions. 我大约一年前有这个问题,花了很长时间寻找解决方案。 I found and use this: 我发现并使用了这个:

server {
    listen 80;
    server_name example.com app.example.com;
    rewrite ^/app(.*)$ http://app.newexample.com$1 permanent;
}

server {
    listen 80;
    server_name app.newexample.com;
    # config for primary domain here
}

From this link . 这个链接 Credit to them. 归功于他们。

Don't put scheme in the rewrite pattern: 不要将方案放在重写模式中:

server {
    server_name app.example.com;
    location /app/ {
        rewrite ^/app/(.*)$ http://app.newexample.com/$1;
    }
}

Brg. BRG。

I prefer this method as it doesn't need to use rewrite, one of the things that i read are good to avoid too much, cause it needs more processing by the nginx engine. 我喜欢这种方法,因为它不需要使用重写,我读过的一件事很好避免了太多,因为它需要nginx引擎进行更多处理。

  location ~ /app/(.*) {
    return 301 $scheme://app.sparkon.com/$1;  
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM