重定向URL路径到端口

huangapple go评论108阅读模式
英文:

redirecting url path to port

问题

我在服务器上运行着许多网络应用程序。其中一些是我自己制作的,另一些是开源的(如open-cloud)。我的所有网络应用程序都在特定的端口上运行。我的80端口仍然空闲。

我想在80端口上创建一个漂亮的欢迎网页,让我的同事可以在本地网络上看到它们。

我的问题是设置URL映射。我希望路径http://机器名:80/open-cloud/http://机器名:8080/完全相同(包括静态文件,如果可能的话)。

另一个可能的解决方案是使用子域名,例如http://open-cloud.机器名:80/用于每个应用程序。但我不知道如何在本地网络中实现这一点。

我正在寻找Python、Node.js或Go的解决方案(那将是很棒的!)。重要的是要注意,我的所有网络应用程序都不运行在SSL上,而且可能处理的流量非常低。毕竟,这只是一个本地网络!

任何帮助和参考都将受到欢迎。

谢谢!

英文:

I have many web apps running on a server. Some of them I have made, some of them are open source (such as open-cloud). All my web apps run on a specific port. My port 80 is still free.

I would like to make a nice welcome web page to all my apps on the port 80 to allow my co-worker to see them on the local network.

My problem is to setup the urls map. I would like the path http://machine-name:80/open-cloud/ to be the exact same thing then http://machine-name:8080/ (including the static files if possible).

One other possible solution would be to use sub-domaines such as http://open-cloud.machine-name:80/ for each application. But I do not know how to do that inside a local network.

I am looking for a solution in Python, node.js or Go (that would be awesome!). It is important to notice that none of my web app runs on ssl and that it will probably handle very low trafic. It is only a local network after all!

Any help and reference would be welcome.

Cheers!

答案1

得分: 3

你只需要一种代理,比如 http://nginx.org 作为例子。

然后将你想要的域名传递给你的应用程序端口:

server {
  listen 80;
  server_name app1.local;

  location / {
    proxy_pass          http://127.0.0.1:8080/;
    proxy_set_header    HOST $http_host;
    proxy_set_header    X-Real-IP $remote_addr;
  }
}
英文:

You just need some kind of proxy, like http://nginx.org for example.

Then pass the domains you want to your application ports:

server {
  listen 80;
  server_name app1.local;

  location / {
    proxy_pass          http://127.0.0.1:8080/;
    proxy_set_header    HOST $http_host;
    proxy_set_header    X-Real-IP $remote_addr;
  }
}

答案2

得分: 3

如果你坚持要用Go编写自己的解决方案,你可以为每个URL设置一个http.Redirect,像这样:

import "net/http"

var urlMap = map[string]string {
    "open-cloud": "http://machine-name:8080/",
    "some-other": "http://machine-name:9090/",
}

func redirectHandler(target string) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        http.Redirect(w, r, target, 301)
    }
}

func main() {
    for key, target := range urlMap {
        http.HandleFunc("/" + key, redirectHandler(target))
    }

    http.ListenAndServe(":80", nil)
}

然而,我更倾向于依赖像joewhite86已经提议的nginx这样的工具。

英文:

If you insist on writing your own solution in Go, you could setup a http.Redirect for each URL
like this:

import "net/http"

var urlMap = map[string]string {
	"open-cloud": "http://machine-name:8080/",
	"some-other": "http://machine-name:9090/",
}

func redirectHandler(target string) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		http.Redirect(w, r, target, 301)
	}
}

func main() {
	for key, target := range urlMap {
		http.HandleFunc("/" + key, redirectHandler(target))
	}

	http.ListenAndServe(":80", nil)
}

However, I would rather rely on a tool like nginx as joewhite86 already proposed.

huangapple
  • 本文由 发表于 2013年7月9日 20:39:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/17548376.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定