我有以下設置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.com
即https://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
的方式。避免這種情況的唯一方法是使用^~
我已經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
@DroidOS,答案是正確的,你不需要重寫。如果由於優先級順序而有其他位置正在處理您的請求,您將需要'^〜'。所以如果你檢查我的編輯,你會看到爲什麼和什麼時候不需要重寫 –