PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, June 25, 2022

[FIXED] How to make reverse proxy?

 June 25, 2022     go, reverse-proxy     No comments   

Issue

I have a server written in Go. I want to write for it a reverse proxy server. The server is compiled into one binary file. When I try to access it through a proxy server, it returns only the HTML page without bindings to CSS and JS scripts. How can I organize the transfer of static files?

package main

import (
    "log"
    "net/http"
    "net/http/httputil"
    "net/url"
    "time"
)

func main() {
    mux := http.NewServeMux()
    u1, _ := url.Parse("http://localhost:8080/")
    proxy := httputil.NewSingleHostReverseProxy(u1)
    mux.Handle("/app1", proxy)

    serv := &http.Server{
        Addr:           ":9090",
        ReadTimeout:    10 * time.Second,
        WriteTimeout:   10 * time.Second,
        Handler:        mux,
    }

    err := serv.ListenAndServe()
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }

}

Solution

Writing a reverse proxy is a complex task. If you plan to expose it on the public internet, I suggest to use an existing one, e.g. Caddyserver, which is written in Go and still free if you compile it yourself instead of using the pre-compiled binaries. The source code of Caddyserver can also give you ideas how to implement it on your own, if you still want to pursue that idea.

To your more specific question about serving files: This can be done with the http.ServeFile method or easier with httprouter's ServeFiles or FileServer methods. See https://github.com/julienschmidt/httprouter#static-files.



Answered By - Chris
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing