2016-08-09 64 views
0

我正在運行一個Heroku應用程序,利用Nginx將另一個運行WordPress的heroku應用程序的代理反向。在Heroku上使用基於Heroku的Wordpress到端口80在Heroku上使用Nginx

Nginx的Buildpack:https://github.com/ryandotsmith/nginx-buildpack 的WordPress的Heroku模板的起點:https://github.com/xyu/heroku-wp

這裏是服務器Nginx的配置和反向代理:

server { 
    listen <%= ENV["PORT"] %>; 
    server_name _; 
    keepalive_timeout 15; 

    location/{ 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     proxy_set_header Host $http_host; 
     proxy_redirect off; 
     proxy_pass http://app_server; 
    } 

    location /locations/ { 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     proxy_set_header Host [wordpress-app].herokuapp.com; 
     proxy_pass http://[wordpress-app].herokuapp.com/locations/; 
     proxy_redirect default; 
    } 
    } 

反向代理工作在大多數情況下,當Worpdress不除重定向。

如果我訪問[rails_app].herokuapp.com/locations/location1,它會嘗試反轉代理[wordpress_app].herokuapp.com/locations/location1。然而,Wordpress會將這個網址重定向到[wordpress_app].herokuapp.com/locations/location1/(追加尾隨'/')。 當Wordpress重定向時,Nginx會嘗試重定向到端口號爲dyno端口的[rails_app].herokuapp.com:[portnumber]/locations/location1。在這種情況下,我不希望端口插入到URL中,因爲它會創建一個錯誤的地址。

我相信問題可以通過修改Nginx配置來解決,但我還沒有成功。

回答

0

解決方案是將port_in_redirect off;添加到服務器配置中。例如:?

server { 
    port_in-redirect off; 

    location/{ 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     proxy_set_header Host $http_host; 
     proxy_redirect off; 
     proxy_pass http://app_server; 
    } 
    } 
+1

不會對我的Heroku :( – moohkooh

+0

@moohkooh工作,你可以上傳你的整個nginx的文件 – adibble

+0

我創建了一個新的問題https://stackoverflow.com/questions/44586218/heroku-nginx-上端口-80 – moohkooh