简体   繁体   English

使用Nginx反向代理进行服务器

[英]Go server with nginx reverse proxy

I've been trying to set up a go web application with docker and nginx as a reverse proxy. 我一直在尝试使用docker和nginx作为反向代理来设置go Web应用程序。

My plan is to use a single domain for multiple applications eg: mydomain.com/myapp1 . 我的计划是将单个域用于多个应用程序,例如: mydomain.com/myapp1

However whenever I try to access my app in with an url like localhost/myapp/something , the request is redirected to http://localhost/something . 但是,每当我尝试使用localhost/myapp/something类的URL访问我的应用程序时,请求都会重定向到http://localhost/something

I've gone through all kinds of nginx configs, none of them worked, so I suspect that the problem is on the go side. 我已经完成了各种Nginx配置,但都没有用,所以我怀疑问题出在哪里。

In the app itself, I'm using gorilla mux for routing, and also negroni for some middleware. 在应用程序本身中,我使用大猩猩多路复用器进行路由,还使用negroni进行一些中间件。

The relevant code looks something like this: 相关代码如下所示:

baseRouter := mux.NewRouter()
baseRouter.HandleFunc("/something", routes.SomeHandler).Methods("GET")
baseRouter.HandleFunc("/", routes.IndexHandler).Methods("GET")

commonMiddleware := negroni.New(
    negroni.HandlerFunc(middleware.Debug),
)

commonMiddleware.UseHandler(baseRouter)
log.Fatal(http.ListenAndServe(":5600", commonMiddleware))

According to this, every request should go through my debug middleware, which just prints some request info to stdout, however when the redirects happen, it doesn't work. 据此,每个请求都应通过我的调试中间件,该中间件仅将一些请求信息打印到stdout,但是当重定向发生时,它不起作用。

But if the path doesn't match any handlers, everything works just fine, the standard go 404 message appears as expected, and the request is printed by the debug middleware as well. 但是,如果路径与任何处理程序都不匹配,则一切正常,标准的go 404消息将按预期显示,并且调试中间件也会打印请求。

My GET handlers generally only do something like this: 我的GET处理程序通常只执行以下操作:

templ, _ := template.ParseFiles("public/something.html")
templ.Execute(w, utils.SomeTemplate{
    Title: "something",
})

And finally, the relevant part in my nginx config: 最后,我的nginx配置中的相关部分:

server {
    listen 80;
    server_name localhost;

    location /myapp/ {
        # address "myapp" is set by docker-compose
        proxy_pass http://myapp:5600/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_cache_bypass $http_upgrade;
    }
}

This kind of nginx config used to be enough for nodeJS apps in the past, so I don't understand why it wouldn't work. 过去,这种nginx配置足以满足nodeJS应用的需求,所以我不明白为什么它不起作用。 If anyone could point out what the hell I'm doing wrongly, I would appreciate it a lot. 如果有人能指出我到底在做什么错,我将不胜感激。

Your nginx looks fine to me. 您的nginx在我看来还不错。

In your Go code, when you create your router, you may use the myapp as the PathPrefix like below: 在Go代码中,创建路由器时,可以将myapp用作PathPrefix,如下所示:

baseRouter := mux.NewRouter()
subRouter := baseRouter.PathPrefix("/myapp").Subrouter()
subRouter.HandleFunc("/something", routes.SomeHandler).Methods("GET")

Or simply add myapp to the path: baseRouter.HandleFunc("/myapp/something", routes.SomeHandler).Methods("GET") 或者直接将myapp添加到路径: baseRouter.HandleFunc("/myapp/something", routes.SomeHandler).Methods("GET")

Your nginx configuration is perfectly fine. 您的nginx配置非常好。

The path you mentioned (/myapp/something) will show you 404 because you have not registered that in your routes. 您提到的路径(/ myapp / something)将显示404,因为您尚未在路径中注册该路径。

I would suggest that if you wish to host multiple applications using the same domain, prefer using subdomains (myapp1.mydomain.com) instead of path (mydomain.com/myapp1). 我建议,如果您希望使用同一域托管多个应用程序,则最好使用子域(myapp1.mydomain.com)而不是路径(mydomain.com/myapp1)。

For each subdomain, you can create a separate nginx server block by changing the server_name value only and keeping the rest of the nginx server file the same. 对于每个子域,您可以通过仅更改server_name值并使其余nginx服务器文件保持相同来创建单独的nginx服务器块。

Then, while using middleware, you may filter out the domains and provide the requested resource. 然后,在使用中间件时,您可以过滤出域并提供请求的资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM