dongwen7813 2013-02-05 18:26
浏览 214
已采纳

Nginx反向代理上的应用程序重定向错误

I am running an nginx (0.7.67) as a reverse proxy and a golang application. The nginx server is configured as follows:

...
location /bar/ {
  proxy_pass http://localhost:8088/;
  proxy_redirect off;
  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_set_header  X-Forwarded-Proto  https;
}
...

The source of my golang application (it doesn't make sense, only an example):

func root(w http.ResponseWriter, r *http.Request) {
  fmt.Fprint(w, "You reached root")
}

func foo(w http.ResponseWriter, r *http.Request) {
  http.Redirect(w, r, "/", http.StatusFound)
}

func main() {
  http.HandleFunc("/", root)
  http.HandleFunc("/foo", foo)
  http.ListenAndServe("localhost:8088", nil)
}

Problem: When I direct my browser to the URL of my application (https://domain.tld/bar/) I can see the text "You reached root". However, if I try to open https://domain.tld/bar/foo, I get redirected to https://domain.tld instead of https://domain.tld/bar. It seems that my application is redirecting to the root of the nginx server and not the application.

Question: How can I redirect from the inside of my application (running behind an nginx reverse proxy) to the root of my application, instead of the root of the server?

  • 写回答

1条回答 默认 最新

  • dongtong5242 2013-02-05 19:34
    关注

    This is a problem that cannot be solved automatically in any language, even PHP. What applications such as wordpress do is have a setting to specify the root. Some applications require a full url (https://domain.tld/bar) or can just have a path (/bar).

    Either way, you need to create a setting and then manually configure all redirects to be based from there. You can create your own redirect function so you don't need to implement it each time you do a redirect.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog