2013-02-05 18:26
浏览 177


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?

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

我正在运行nginx(0.7.67)作为反向代理和golang应用程序。 nginx服务器的配置如下:

location / bar / {
 proxy_pass http:// localhost:8088 /; 
 proxy_redirect off; 
  proxy_set_header主机$ 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; 


  func root(w http  .ResponseWriter,r * http.Request){
func foo(w http.ResponseWriter,r * http.Request){
 http.Redirect  (w,r,“ /”,http.StatusFound)
func main(){
 http.HandleFunc(“ /”,root)
 http.HandleFunc(“ / foo”,foo)\  n http.ListenAndServe(“ localhost:8088”,nil)

问题:当我将浏览器定向到URL时 我的应用程序( https://domain.tld/bar/ ) 文本“您已达到根”。 但是,如果我尝试打开 https://domain.tld/bar/foo ,则会重定向到 https://domain.tld 而不是 HTTPS://domain.tld/bar 。 看来我的应用程序正在重定向到Nginx服务器的根目录,而不是应用程序。

问题:如何从应用程序内部重定向( 在Nginx反向代理后面运行)到我的应用程序的根目录,而不是服务器的根目录?

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

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.

    点赞 评论

相关推荐 更多相似问题