skip to Main Content

I’m new at digitalocean, trying to deploy my aiohttp app. I have got a domain name and it’s already set in digitalocean dns records.

So, in my backend I have a route "/persons", it’s available to get it if I go "{row_ip}/persons", but if ill try to get it with domain name "{domain_name}/persons" it will always redirect me to "/" page ( and also make "/" request from backend). Any suggestions how to make available to get "{domain_name}/persons" with domain name? Thanks for your time.

2

Answers


  1. The following works for me:

    I created a basic Golang web server:

    package main
    
    import (
        "flag"
        "fmt"
        "log"
        "net/http"
    )
    
    var (
        port = flag.Int("port", 0, "HTTP server port")
    )
    
    func hello(w http.ResponseWriter, req *http.Request) {
        fmt.Fprintf(w, "hellon")
    }
    
    func main() {
        flag.Parse()
    
        http.HandleFunc("/x", hello)
        http.HandleFunc("/y", hello)
    
        log.Printf("Starting server: %d", *port)
        log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))
    }
    

    Build it:

    go build -o server .
    

    Create Droplet and grab its IP:

    doctl compute droplet create ${INSTANCE} 
    --region=${REGION} 
    --size=${SIZE} 
    --ssh-keys=${SSH_KEY} 
    --image=${IMAGE}
    
    IP=$(doctl compute droplet get ${INSTANCE} 
    --format PublicIPv4 
    --no-header)
    

    Update DNS:

    doctl compute domain records create ${DOMAIN} 
    --record-type=A 
    --record-name=${INSTANCE} 
    --record-data=${IP} 
    --record-ttl=3600
    
    nslookup ${INSTANCE}.${DOMAIN}
    

    Copy the binary:

    scp 
    -i ${ID} 
    ${PWD}/server 
    root@${INSTANCE}.${DOMAIN}:.
    

    SSH in and start the server:

    PORT="6666"
    ssh 
    -i ${ID} 
    root@${INSTANCE}.${DOMAIN} 
    ./server --port=${PORT}
    

    Then test it:

    curl http://${INSTANCE}.${DOMAIN}:${PORT}/x
    hello
    
    Login or Signup to reply.
  2. You can achieve this with a rewrite in an htaccess file in your website’s directory.

    You will want to create an .htaccess file in that directory with the following (replacing the IP and domain with your own):

    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^12.34.56.789$
    RewriteRule ^(.*)$ http://www.domainname.com/$1 [L,R=301]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search