简体   繁体   English

是否可以使用 nginx 并根据子域重定向到正确的路径?

[英]Is it possible to use nginx and redirect to correct path based on sub domains?

I spot an issue when trying to configure the server.我在尝试配置服务器时发现了一个问题。 On the server side I am using Nginx.在服务器端,我使用的是 Nginx。

The issue is that I got 1 domain and 2 subdomains like:问题是我有 1 个域和 2 个子域,例如:

  1. www.test.com www.test.com
  2. www.api.test.com www.api.test.com
  3. www.panel.test.com www.panel.test.com

And these 3 urls are redirected to server with IP by Record A.并且这 3 个 url 被记录 A 重定向到带有 IP 的服务器。

All i want to do is recognize from what url the user comes and return:我想做的就是从 url 中识别用户来并返回:

  1. if user comes from www.test.com - show static html如果用户来自www.test.com - 显示 static html
  2. if user comes from api - redirect to nodejs server which will serve data如果用户来自 api - 重定向到将提供数据的 nodejs 服务器
  3. if user comes from panel - redirect to nodejs server which will serve html如果用户来自面板 - 重定向到 nodejs 服务器,该服务器将为 html

I don't know how to recognize from what url the user comes.我不知道如何识别用户来自url。 Is it possible to do that?有可能这样做吗? If it is - just if you got some tutorials - i would be grateful, Unfortunately, I checked lots of tutorials.如果是的话——只要你有一些教程——我将不胜感激,不幸的是,我检查了很多教程。 but I wasn't able to manage it.但我无法管理它。

Just as a information, I'm really bad at that piece (servers and configuration) - but want to learn it:)作为一个信息,我在这方面真的很糟糕(服务器和配置) - 但想学习它:)

Thank you!谢谢!

The server_name directive assigns rules to server blocks for specific domain/host names: server_name指令为特定域名/主机名的server块分配规则:

server {
  server_name test.com www.test.com;

  listen      80;  # IPv4
  listen [::]:80;  # IPv6
  
  root /var/www/static-files;
}

server {
  server_name api.test.com;
  ...
}

server {
  server_name panel.test.com;
  ...
}

Alternatively, the $host variable could be used within server blocks that serve multiple hosts:或者,可以在服务多个主机的服务器块中使用$host变量:

server {
  server_name test.com www.test.com
              api.test.com
              panel.test.com;

  return 200 "This hostname is $host";
  ...
}

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

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