简体   繁体   English

虚拟主机还是干脆重写?

[英]Virtual host or simply rewrite?

I've a website that has a profile page. 我的网站上有个人资料页面。 Obviously all users have an own profile page available on this url: domain.dev/profile?user=%username%. 显然,所有用户都可以在以下URL上找到自己的个人资料页面:domain.dev/profile?user =%username%。

Now, I want to do that every user can see the profile on username.domain.dev. 现在,我想让每个用户都可以在username.domain.dev上看到配置文件。

I saw many post about that like How to let PHP to create subdomain automatically for each user? 我看到了很多有关此的文章,例如如何让PHP为每个用户自动创建子域? but it doesn't resolve my problem. 但这不能解决我的问题。

I've my website on ubuntu (nginx) and also on Windows IIS 10. How can I do that? 我在ubuntu(nginx)和Windows IIS 10上都有我的网站。我该怎么做? Do you have some other link/question that can I see? 您还有其他链接/问题可以看到吗? Or some suggestion? 还是一些建议?

In Nginx, you just need to set up something similar to this: 在Nginx中,您只需要设置以下内容:

server {
    listen       80;
    server_name  *.domain.dev;
    ...
}

Notice the: 注意:

server_name  *.domain.dev;

Within your application, you will need to split/handle the HOST header. 在您的应用程序中,您将需要拆分/处理HOST标头。

Another approach but this implies a redirect 301 is to do something like this: 但这是另一种方法,但这意味着重定向301要做这样的事情:

server {
   listen      80;
   server_name ~^(?<subdomain>\w+)\.your-domain\.tld$;
   return 301 https://domain.dev/profile?user=$subdomain;
}

In this case, notice the regex in the server_name: 在这种情况下,请注意server_name中的正则表达式

server_name ~^(?<subdomain>\w+)\.your-domain\.tld$;

This helps you to use the subdomain as $subdomain . 这可以帮助您将子域用作$subdomain

To avoid a redirect this may work: 为避免重定向,此方法可能有效:

server {
  listen      80;
  server_name ~^(?<subdomain>\w+)\.your-domain\.tld$;

  location / {
    resolver 8.8.8.8;
    proxy_pass http://httpbin.org/get?user=$subdomain;
    proxy_set_header Host httpbin.org; 
  } 
}

For testing purposes, I am using http://httpbin.org/ on the proxy_pass`, so that you could test with something like: 出于测试目的,我on the proxy_pass` on the使用http://httpbin.org/ ,以便您可以使用以下内容进行测试:

$ curl foo.your-domain.tld:8080
{
  "args": {
    "user": "foo"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Connection": "close", 
    "Host": "httpbin.org", 
    "User-Agent": "curl/7.54.0"
  }, 
  "origin": "91.65.17.142", 
  "url": "http://httpbin.org/get?user=foo"
}

Notice the response: 注意响应:

"url": "http://httpbin.org/get?user=foo"

Matching the subdomain foo .your-domain.tld in this case. 在这种情况下,匹配子域foo .your-domain.tld。

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

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