2018-04-01 00:58
浏览 222


I've been trying to set up a go web application with docker and nginx as a reverse proxy.

My plan is to use a single domain for multiple applications e.g.: 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.

I've gone through all kinds of nginx configs, none of them worked, so I suspect that the problem is on the go side.

In the app itself, I'm using gorilla mux for routing, and also negroni for some middleware.

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(

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.

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.

My GET handlers generally only do something like this:

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

And finally, the relevant part in my nginx config:

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. If anyone could point out what the hell I'm doing wrongly, I would appreciate it a lot.

图片转代码服务由CSDN问答提供 功能建议

我一直在尝试使用docker和nginx作为反向代理来设置go web应用。

我的计划是将单个域用于多个应用程序,例如: mydomain.com/myapp1

但是无论何时我尝试 使用 localhost / myapp / something 之类的URL访问我的应用程序,该请求将重定向到 http:// localhost / something




  baseRouter:= mux.NewRouter()
baseRouter.HandleFunc(“ / something”,route.SomeHandler).Methods(“ GET”)
baseRouter.HandleFunc(“ /”,routes.IndexHandler).Methods(“  GET“)
commonMiddleware:= negroni.New(
log.Fatal(http.ListenAndServe(”:5600“,  commonMiddleware))


但是,如果该路径与任何处理程序都不匹配,则一切正常,标准的go 404消息将按预期显示,并且请求已打印


  templ,_:=模板。  ParseFiles(“ public / something.html”)
templ.Execute(w,utils.SomeTemplate {
标题:“ something”,


  server {
 listen 80; 
 server_name localhost; 
 location / myapp / {
#地址 “ myapp”由docker-compose设置
 proxy_pass http:// myapp:5600 /; 
 proxy_http_version 1.1; 
 proxy_set_header升级$ http_upgrade; 
 proxy_set_header主机$ 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; 

此 Nginx的配置过去过去足以满足nodeJS应用的需求,所以我不明白为什么它不起作用。 如果有人能指出我到底在做什么错,我将不胜感激。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doushanmo7024 2018-04-01 08:19

    Your nginx looks fine to me.

    In your Go code, when you create your router, you may use the myapp as the PathPrefix like below:

    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")

    打赏 评论
  • dongtazu3080 2018-04-01 08:11

    Your nginx configuration is perfectly fine.

    The path you mentioned (/myapp/something) will show you 404 because you have not registered that in your routes.

    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).

    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.

    Then, while using middleware, you may filter out the domains and provide the requested resource.

    打赏 评论

相关推荐 更多相似问题