Non so perché questo proxy inverso non funzioni. Ho visto diversi esempi e non riesco a trovare nulla di sbagliato in questo.golang ReverseProxy non funziona
package main
import (
"log"
"net/url"
"net/http"
"net/http/httputil"
)
func report(r *http.Request){
log.Print("URL: " + r.URL.Path)
log.Print("Scheme: " + r.URL.Scheme)
log.Print("Host: " + r.URL.Host)
//r.URL.Scheme = "http"
//r.URL.Host = "stackoverflow.com"
//r.Header.Set("Host", "stackoverflow.com")
//log.Print("Header Host: " + r.Header.Get("Host"))
}
func main() {
proxy := httputil.NewSingleHostReverseProxy(&url.URL{Scheme:"http",Host:"myrealserver.com"})
proxy.Director = report
// http.Handle("/", proxy)
error := http.ListenAndServe("mylocalhost.com:8080", proxy)
if(error != nil) {
log.Fatal(error)
}
}
Si registra:
2014/04/18 21:32:50 URL: /arg/es
2014/04/18 21:32:50 Scheme:
2014/04/18 21:32:50 Host:
2014/04/18 21:32:50 http: proxy error: unsupported protocol scheme ""
2014/04/18 21:32:51 URL: /favicon.ico
2014/04/18 21:32:51 Scheme:
2014/04/18 21:32:51 Host:
2014/04/18 21:32:51 http: proxy error: unsupported protocol scheme ""
Se io rimuovere il commento dalla linea che ridefinisce lo schema il messaggio di errore diventa:
2014/04/18 21:38:05 http: proxy error: http: no Host in request URL
Se io rimuovere il commento dalla linea che ridefinisce l'host anche, quindi il server di destinazione diventa stackoverflow.com (voglio dire, non usa mai "myrealserver.com").
Se chiedo mylocalhost.com:8080/somepath (o even /), ricevo un 404 da Stackoverflow, non importa se stackoverflow.com/somepath esiste o meno. Dice:
Couldn't find mylocalhost.com:8080
The Q&A site mylocalhost.com:8080 doesn't seem to exist... yet
Non converte automaticamente l'intestazione Host.
Se poi annullo la riga che imposta (e l'altra che stampa) l'intestazione "Host". Quindi posso leggere "stackoverflow.com" nel registro, ma ottengo comunque la stessa segnalazione di pagina 404 che sto tentando di accedere a "mylocalhost.com".
sto usando go1.2.1 linux/amd64
Come è possibile che dovrei far funzionare il programma come un proxy?