2017-09-11 175 views
1

我有以下設置Nginx的proxy_pass只能部分

  • 主服務器 - 稱之爲https://master.com
  • 從服務器 - 稱之爲https://slave.com

兩個運行Nginx的在Ubuntu 16.04

在主服務器上,我在/etc/nginx/sites-available/default文件中創建了以下配置塊

location /test 
{ 
rewrite ^/test(.*) /$1 break; 
proxy_pass https://slave.com; 
proxy_read_timeout 240; 
proxy_redirect off; 
proxy_buffering off; 
proxy_set_header Host $host; 
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
proxy_set_header X-Forwarded-Proto https; 
} 

service nginx reload稍後master.com和我可以執行以下操作

  • 瀏覽https://master.com/test和查看從slave.com\index.php的輸出。
  • 瀏覽https://master.com/test/test.txt和查看文件slave.com\test.txt
  • 瀏覽https://master/com/test/test.jpg文本和查看文件slave.com\test.jpg圖像。

然而,我不能做任何以下

  • 瀏覽的以https://master.com/test/test.php而不是顯示我從https://slave.com/test.php輸出其示出了我404錯誤消息
  • 瀏覽https://master.com/test/adminer/adminer.php其中而不是顯示我從設備上的Adminer實例的登錄屏幕https://slave.com/adminer/adminer.php向我顯示Adminer實例的登錄屏幕master.comhttps://master.com/adminer/adminer.php

這顯然是因爲我在我的Nginx配置中丟失了一些東西,在master.com。但是,我無法看到可能是什麼。

在完整性的利益,這是我的兩臺服務器上的配置:

Ubuntu的 - 16.04.3 Nginx的 - 1.10.3 PHP - 7.0.22

我應該解釋爲什麼^~是必需的,因爲這從我原來的問題不清楚。我有另一個塊設置來處理master.com上的PHP腳本。

location ~ \.php$ 
{ 
try_files $uri =404; 
fastcgi_split_path_info ^(.+\.php)(/.+)$; 
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; 
fastcgi_index index.php; 
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
include fastcgi_params; 
} 

的Nginx的因爲處理這些指令,此塊的優先級,當涉及到處理.php文件和master.com結束了當地的.php腳本,實際上是在尋找slave.com的方式。避免這種情況的唯一方法是使用^~

回答

1

您的方法是錯誤的。在處理/test的塊內,您重寫它並將其從塊中發出。proxy_pass從來沒有實際發生,因爲新網址中沒有/test。解決方法很簡單,不要用改寫

location /test/ 
{ 
proxy_pass https://slave.com/; 
proxy_read_timeout 240; 
proxy_redirect off; 
proxy_buffering off; 
proxy_set_header Host $host; 
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
proxy_set_header X-Forwarded-Proto https; 
} 

在位置路徑的終點,也是proxy_pass服務器追加/會讓知道什麼是/test/後發送到您的proxy_pass地址

編輯 - 1

這是我在發佈此答案之前設置的示例測試用例。

events { 
    worker_connections 1024; 
} 
http { 
server { 
    listen 80; 

    location /test1 { 
    proxy_pass http://127.0.0.1:81; 
    } 

    location /test2 { 
    proxy_pass http://127.0.0.1:81/; 
    } 

    location /test3/ { 
    proxy_pass http://127.0.0.1:81; 
    } 

    location /test4/ { 
    proxy_pass http://127.0.0.1:81/; 
    } 

} 

server { 
    listen 81; 

    location/{ 
    echo "$request_uri"; 
    } 
} 
} 

現在的結果說明了所有4塊位置的塊

$ curl http://192.168.33.100/test1/abc/test 
/test1/abc/test 

$ curl http://192.168.33.100/test2/abc/test 
//abc/test 

$ curl http://192.168.33.100/test3/abc/test 
/test3/abc/test 

$ curl http://192.168.33.100/test4/abc/test 
/abc/test 

之間的區別正如你在/test4網址看到代理服務器只能看到/abc/test

+0

我已經upvoted你的答案,因爲你正確地提我需要'/'在'/ test'上終止斜線。但是,這不是正確的答案。由於在目標服務器上所請求的資源是在文檔根目錄中,而不是在子文件夾「/ test」中,所以'rewrite'是必需的。我記得剛纔我遇到了這個問題,並得到了[解決方案] [https://stackoverflow.com/questions/29212655/nginx-php-scripts-not-being-called-from-reverse-proxy](解決方案)來自@SeriousDron的SO。長話短說'location/test /'需要改爲讀取'location ^〜/ test /',這一切都可行! – DroidOS

+0

@DroidOS,答案是正確的,你不需要重寫。如果由於優先級順序而有其他位置正在處理您的請求,您將需要'^〜'。所以如果你檢查我的編輯,你會看到爲什麼和什麼時候不需要重寫 –