duangouyan3328 2018-04-01 00:58
浏览 262
已采纳

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

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

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.

  • 写回答

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿