2011-04-29 129 views
47

我試圖使用Thin應用程序服務器,並有一個問題。如何使用nginx proxy_pass保存請求url

當nginx proxies使用proxy_pass http://my_app_upstream;請求瘦(或獨角獸)時,應用程序會收到由nginx發送的修改後的URL(http://my_app_upstream)。

我想要的是通過原始URL和原始請求從客戶端沒有任何修改,因爲應用程序嚴重依賴它。

nginx的」 doc說:

如果有必要傳送URI在 未處理形式然後指令 proxy_pass應毫不URI 部分被使用。

但我不明白究竟如何配置,作爲相關的樣品實際上是使用URI:

location /some/path/ { 
    proxy_pass http://127.0.0.1; 
} 

所以,請你幫我搞清楚如何保留原始請求URL來自客戶?

回答

90

我覺得proxy_set_header指令可以幫助:

location/{ 
    proxy_pass http://my_app_upstream; 
    proxy_set_header Host $host; 
    # ... 
} 
+0

剛剛發現相同。謝謝。 – 2011-04-29 16:31:27

+57

注意到其他人的發現:使nginx不處理URL的解決方案的核心是刪除'proxy_pass'指令末尾的斜線。 'http:// my_app_upstream' vs'http:// my_app_upstream /' – 2012-06-25 11:54:41

+1

謝謝@HugoJosefson - 那是我的問題,所以感謝發佈! – dsldsl 2012-12-27 08:30:20

9

只是 proxy_set_header $ host主機 小姐端口,用於我的情況。通過解決:



    location/{ 
    proxy_pass http://BACKENDIP/; 
    include /etc/nginx/proxy.conf; 
    } 

然後在proxy.conf



    proxy_redirect off; 
    proxy_set_header Host $host:$server_port; 
    proxy_set_header X-Real-IP $remote_addr; 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 

+0

謝謝,這是我缺少的一部分($ server_port),以便在代理後面的端點上進行OAuth驗證。 – renier 2014-11-21 14:45:35

+0

我正在使用sinatra進行機架保護,並在POST URL上獲取禁止訪問。將端口添加到主機代理標題爲我解決了它。 – pduey 2015-02-26 17:15:16

+0

這不再適用於最新版本的Nginx我覺得http://stackoverflow.com/questions/31482796/why-is-proxy-set-header-host-host-no-longer-working-in-nginx- 1-8-0?s = 1 | 3.2072 – iwein 2015-07-17 18:58:25

1

要完全向前而不斬波請求的absoluteURI和標頭中的Host

server { 
    listen 35005; 

    location/{ 
     rewrite   ^(.*)$ "://$http_host$uri$is_args$args"; 
     rewrite   ^(.*)$ "http$uri$is_args$args" break; 
     proxy_set_header Host  $host; 

     proxy_pass   https://deploy.org.local:35005; 
    } 
} 

這裏找到的:https://opensysnotes.wordpress.com/2016/11/17/nginx-proxy_pass-with-absolute-url/

+0

我愛你的人......只有這一個人解決了我...... – 2017-11-16 14:14:28

0

nginx也提供es將爲您通過端口的$ http_host變量。其主機和端口串聯在一起。

所以你只需要做: proxy_set_header Host $ http_host;