Nginx custom html folder

2020-05-01 00:59发布

I'm new to nginx server and have some trouble. Im trying to server a static file but it is only working if i place it in: /var/www/html

I would like to place my index.html in this folder: /home/bowa/go/go-web/frontend

This is my server config:

server {
    server_name braurl.se www.braurl.se;

    root /home/bowa/go/go-web/frontend/;
    index.html;

    location / {
        proxy_pass http://localhost:9990;
    }


    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/braurl.se/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/braurl.se/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

This is my golang code to serve the file:

(my main.go is located in the folder: /home/bowa/go/go-web/ )

func rootHandler(w http.ResponseWriter, r *http.Request) {
    http.ServeFile(w, r, "/frontend/index.html")
}

The only way i succeed in serving index.html is if i define no root in my server config and if i change my go code like this:

http.ServeFile(w, r, "/var/www/html/index.html")

This is driving me a little crazy and i would love if someone could point me in the right direction.

I got this working in localhost but not on nginx.

func serveFiles(w http.ResponseWriter, r *http.Request) {
    fmt.Println(r.URL.Path)
    p := "." + r.URL.Path
    if p == "./" {
        p = "./frontend/index.html"
    }
    http.ServeFile(w, r, p)
}
func main() {
    r := mux.NewRouter()
    api := r.PathPrefix("/api/v1").Subrouter()

    // index.html
    r.HandleFunc("/", serveFiles)
}

Structure: Pic of server structure

标签: ubuntu go nginx
0条回答
登录 后发表回答