gorilla / mux

Package gorilla/mux is a powerful HTTP router and URL matcher for building Go web servers with 🦍
https://gorilla.github.io
BSD 3-Clause "New" or "Revised" License
20.93k stars 1.85k forks source link

How use nginx with mux params? #671

Closed udonetsm closed 2 years ago

udonetsm commented 2 years ago

Describe the problem you're having

Hello. I have proxing with nginx to my golang server. When i start server with mux params(/url/{param}/{param2} for example) i have 404 in nginx server when i try get url/some/some/ in browser. How fix it? Thanks

Versions

Go version: 1.15.15

package version: run git rev-parse HEAD inside the repo

"Show me the code!"

package main

            import (
                "log"
                "net/http"

                "github.com/gorilla/mux"
            )

            func main() {
                log.Println(make_server().ListenAndServe())
            }

            func make_server() *http.Server {
                router := mux.NewRouter()
                router.HandleFunc("/{name}/", handler)
                return &http.Server{
                    Handler: router,
                    Addr:    ":8282",
                }
            }

            func handler(w http.ResponseWriter, r *http.Request) {
                w.Write([]byte("Hello, " + mux.Vars(r)["name"]))
            }

A simple nginx confinguration

    server {
            listen 80;
            listen [::]:80;
            root /var/www/html;
            index index.html index.htm index.nginx-debian.html;

            server_name _;

            location / {
                        try_files $uri $uri/ =404;
                    proxy_pass http://192.168.1.109:8282;
            }
    }

Hint: wrap it with backticks to format it