在相同的Nginx服务器块下提供不同的后端,无需重定向

时间:2014-04-09 19:34:28

标签: nginx url-rewriting

我有一个为生产网站https://example.com提供服务的rails应用。我还在https://blog.example.com的同一个网站上有一个WordPress博客。出于维护和营销原因,我们希望在主网站中不断更改网址,例如/about/faq等,以便使用WordPress管理,但在主网站下提供,以便 - < / p>

https://example.com/about将呈现https://blog.example.com/about但不会重定向,与https://example.com/faq相同,就好像它们已安装在该主域下一样。

https://example.com/anything-else将使用rails后端等

显然,这可以通过简单的重定向来实现,但我希望保留相同的网址 - 这可能吗?我尝试使用proxy_pass,但最终获得重定向到新网址:

server {
    # WordPress configuration
    listen 443 ssl;

    server_name blog.example.com;
    root /var/www/blog;

    #...
}

server {
    listen 443 ssl default;
    # .. misc SSL settings

    server_name example.com www.example.com;
    root /var/www/example;

    # .. misc settings

    location /about {
        # ... render blog.example.com/about
    }

    location /faq {
        # ... render blog.example.com/faq
    }

    location @unicorn {
        # ... unicorn configuration
    }
}

感谢。

1 个答案:

答案 0 :(得分:1)

关键是你会告诉nginx将某些请求代理到另一台服务器(在这种情况下是wordpress)

sever {
  #this is the rails server

  # These are all the locations combined into one condition
  # or you can split them to different locations if u want
  location /(faq|about|whatever) {
    # asking nginx to proxy these requests to this URL
    proxy_pass http://blog.example.com;
  }

  # The remaining cases that don't match the above criteria,
  # in your case this is where you want to pass to rails.
  location / {
    # remaining of rails settings
  }
}