简体   繁体   English

如何使用nginx作为反向代理将localhost:9292定向到子域foo.localhost /?

[英]How to use nginx as reverse proxy to direct localhost:9292 to a sub domain foo.localhost/?

I've learned how to pass localhost:9292 to localhost/foo with the following directive: 我已经学习了如何使用以下指令将localhost:9292传递给localhost/foo

location /foo {
    proxy_pass http://localhost:9292;
}

but I want do something like 但我想做类似的事情

foo.localhost -> localhost:9292

Is there a way I can do that? 有办法吗?

If foo.localhost is your sub domain name and you want to proxy pass sub-domain to main-domain, you can use proxy_pass and you can learn a little more about server directive if needed. 如果foo.localhost是您的子域名,并且您希望将子域代理传递到主域,则可以使用proxy_pass,并且如果需要,可以了解有关server指令的更多信息。 An example: 一个例子:

server {
  listem 8080;
  host sub.main.com;
  ...
  location / {
    proxy_pass http://main.com;
    break;
  }
}
server {
  listen 8081;
  host main.com;
  ...
  location / {
    //do something
  }
}

This is proxy pass, means when access sub.main.com, actually it finally dealt by main.com, but the client side still shows sub.main.com. 这是代理通行证,意味着访问sub.main.com时,实际上它最终是由main.com处理的,但是客户端仍然显示sub.main.com。 If you want client side shows main.com, here should use redirect but not proxy_pass. 如果要让客户端显示main.com,请在此处使用重定向,而不要使用proxy_pass。

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

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