Nginx 用户子域,我应该使用 proxy_pass 吗?
我正在尝试设置用户子域,从特定文件夹提供内容:从 username.example.com
提供的 www.example.com/username
(就像 github 页面一样)。
我研究过 Nginx 重写,但我不希望浏览器重定向——我希望域名是 username.example.com。 无论如何,对这个问题的评论说我无法重写主机,只能代理它。 我尝试设置 proxy_pass,但所有文档和示例都显示它用于(显然)代理到另一个主机或端口上的服务,但就我而言,我只想代理到另一个主机或端口位于同一主机和端口上。
这是解决这个问题的适当方法吗?如果是,正确的 Nginx 配置语法是什么?
I am trying to setup user subdomains, serving content from specific folders: www.example.com/username
served from username.example.com
(just like github pages).
I've looked at Nginx rewrites, but I don't want the browser to redirect--I want the domain to be username.example.com.
Anyway, a comment on this question says that I cannot rewrite host, only proxy to it.
I tried to setup a proxy_pass
, but all of the documentation and examples show it being used to (obviously) proxy to a service on another host or port, but in my case I want to just proxy to another location on the same host and port.
Is this the appropriate way to tackle this problem, and if so, what is the right Nginx config syntax?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样的东西应该几乎可以工作:
但我怀疑它会按预期工作,因为代理网址中的“/user1”部分,但我不确定后果。
另一种肯定有效的方法是使用同一个 nginx 服务器为所有应用程序提供服务,或者为每个其他 nginx 分配给定端口。
我会这样做:
上次我这样做是为了一个大学项目,我使用了一个特定的插件来处理它,可能还有一些插件可以为 nginx 执行此操作,但在快速搜索后我没有找到任何插件。
Something like this should almost work:
But I doubt it will work as expected because of the "/user1" part in the proxied url, I am not sure of the consequences though.
Another way of doing this which will surely work is to either serve all the applications with the same nginx server or assign a given port to each of the other nginx.
Here is how I would do this:
Last time I did this was for a college project and I used a specific plugin to handle it, there may also be some plugin which can do this for nginx but I did not found any after a quick search.