2015-05-08 36 views
5

我有這樣的設置:重寫重定向到包含使用的端口訪問服務

enter image description here

我流浪的發展環境工作的用戶他們的主機,它被轉發到流浪的訪問localhost:8080到NGINX在localhost:80在客人上運行。一些請求被轉發到我的應用程序服務器上,在localhost:8080上運行,其中一些是從NGINX提供的靜態文件。

當我訪問我的網站時會發生一件奇怪的事情。我有一個成功重定向的登錄頁面,URL從http://localhost:8080/login更改爲http://localhost:80/login

下面是該網站我NGINX配置:

upstream appserver { 
    server 127.0.0.1:8080; 
} 

upstream production { 
    server www.mysite.com:443; 
} 

server { 
    listen 80 default_server; 
    server_name _; 

    client_max_body_size 20M; 

    access_log /var/log/nginx/project.access.log; 
    error_log /var/log/nginx/project.error.log; 

    index /index; 

    location ~ ^(/js/testpage.js) { 
     alias /vagrant/artifacts/www/js/testpage.js; 
    } 

    location ~ ^(/test/js/app.js) { 
     alias /vagrant/test/js/app.js; 
    } 

    location ~ /test/js/app_router.js { 
     alias /vagrant/test/js/app_router.js; 
    } 

    location ~ /test/js/app_layout_controller.js { 
     alias /vagrant/test/js/app_layout_controller.js; 
    } 

    location ~ /test/js/apps/navbar/sections/layout/navbar_layout_controller.js { 
     alias /vagrant/test/js/apps/navbar/sections/layout/navbar_layout_controller.js; 
    } 

    location ~ /test/js/apps/navbar/sections/navbar/navbar_options_view.js { 
     alias /vagrant/test/js/apps/navbar/sections/navbar/navbar_options_view.js; 
    } 

    location ~ /test/js/apps/navbar/sections/navbar_all_views.js { 
     alias /vagrant/test/js/apps/navbar/sections/navbar_all_views.js; 
    } 

    location ~ ^/test/js/apps/(.*/testpage_.*\.js)$ { 
     alias /vagrant/test/js/apps/$1; 
    } 

    location ~ ^/test/js/(.*)$ { 
     alias /vagrant/js/$1; 
    } 

    location ~ ^/build/js/(.*)$ { 
     alias /vagrant/artifacts/www/js/$1; 
    } 

    location ~ ^/build/css/(.*)$ { 
     alias /vagrant/artifacts/www/css/$1; 
    } 

    location ~ ^/(.*main.*)\.[@\-_\/\d\w]+\.(js|css)$ { 
     alias /vagrant/$1.$2; 
    } 

    location ~ ^/(css|js|fonts|favicon.ico) { 
     root /vagrant; 
    } 

    location ~ ^/receipts/js/(.*)$ { 
     alias /vagrant/receipts/js/$1; 
    } 

    location ~ ^/bower_components/(.*)$ { 
     alias /vagrant/bower_components/$1; 
    } 

    location ~ ^/login-promo/ { 
     access_log off; 
     proxy_pass https://production; 
     proxy_set_header X-Real-IP $remote_addr; 
     proxy_set_header Host $host; 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    } 

    location ~ ^/(admin|login|logout|index|build|testpage|receipts|open|reset|resetpage|privacy|change|activeUser|personPrivacyAcceptances) { 
     access_log off; 

     proxy_pass http://appserver; 
     proxy_set_header X-Real-IP $remote_addr; 
     proxy_set_header Host $host; 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    } 
} 

我不知道在哪裏重定向實際上是從,它可以從應用程序服務器後端或從前端的JavaScript是未來的到來。有沒有一種方法可以確保所有重定向都採用訪問客戶端使用的端口?


更新:簡單地增加一個<a href="/login">login</a>鏈接到根頁面,並試圖導航與鏈接重定向我http://localhost:80/login而非http://localhost:8080/login

回答

0

這似乎已經引起做重定向錯誤在應用程序代碼。

如果您有與上述類似的體系結構,並且遇到問題,則可能是您遇到此問題的原因是因爲嘗試重定向時沒有考慮到標頭Host

你背後nginx的應用程序會像這樣的請求:

GET /path/to/resource 
Host: virtualinstance:8888 

不幸的是,當它構建了重定向,它做了愚蠢和重建重定向,而不考慮Host頭,轉而選擇主機和它知道它正在使用的端口。這是打破我的重定向代碼看起來是這樣的:

return redirect("http://" + getHostName() + "/" + getPath()) 

首先,這是不好的,它的硬編碼協議,但更糟糕的是,它使用它獲取主機名,而不用於端口方面的方法。這將返回重定向到http://localhost/path,而不是http://localhost:8888/path

開發者,這就是爲什麼你應該總是使用Host標題爲您的重定向來源:

return redirect("%s://%s/%s" % (getProtocol(), getHeader("Host"), 
    getPath()) 

這使得一個理智的重定向,使用原來的協議,從Host全文和路徑。

0

很可能發生由於port_in_redirect是默認on。請參閱:http://wiki.nginx.org/HttpCoreModule#port_in_redirect

禁用這可能會阻止一些混淆爲什麼端口正在被應用。

從那裏,這是一個簡單的設置適當的host_portremote_port在您的代理傳遞重定向,如有必要。通過查看你的配置,看起來你很喜歡使用核心模塊變量,包含這些變量會很簡單。

參見:

http://wiki.nginx.org/HttpCoreModule#.24remote_port http://wiki.nginx.org/HttpCoreModule#.24server_port