我有一個由運行在後端專用網絡中的docker管理的微服務,並且希望設置一個由nginx管理的動態路由,以便它可以根據URI選擇微服務。我有三個問題:Nginx重寫到根目錄下的子文件夾
我需要的nginx採取域名從URI路徑的第一段
fastcgi_pass
指令,因此「應用程序」從語句fastcgi_pass app:9000;
應該來自這裏:https://example.com/app/foo/bar
。如果URI中的URI是https://example.com/another-app/foo/bar
,那麼指令看起來會像fastcgi_pass another-app:9000;
這樣動態地實現,或者我必須爲每個FastCGI服務器創建一個單獨的位置?我還需要它根據相同的第一個URI路徑段重寫到根目錄下的文件夾。我寫了一個配置,但得到404錯誤。我是nginx的新手,只注意到nginx和php-fpm容器中的路徑不匹配。 404錯誤可能與這個事實有關嗎?
這樣的路由是否可能(詳情請參閱下面的配置)?另一種選擇是爲每個微服務創建一個單獨的位置,但我不希望每次添加或刪除微服務時都更改此配置。
這裏是我的配置:
server {
server_name _;
root /services;
#rewrite to the subfolder under root depending on the first section in the path
rewrite ^/(\w+)(/|$) /$1/app_dev.php$is_args$args last;
location/{
# try to serve file directly, fallback to app.php
try_files $uri /app_dev.php$is_args$args;
}
location ~ ^/(\w+)/(app_dev|config)\.php(/|$) {
#further rewrite to the /web subfolder with the front controller
rewrite ^/(\w+)/(app_dev|config)\.php(/|$) /$1/web/$2.php$is_args$args break;
fastcgi_pass media:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
location ~ \.php$ {
return 404;
}
error_log /var/log/nginx/error.log notice;
access_log /var/log/nginx/access.log;
rewrite_log on;
}
這裏是日誌:
backend_1 | 2017/09/17 20:03:54 [error] 8#8: *1 open() "/usr/share/nginx/html/media" failed (2: No such file or directory), client: 172.25.0.1, server: localhost, request: "GET /media HTTP/1.1", host: "localhost:8082"
backend_1 | 172.25.0.1 - - [17/Sep/2017:20:03:54 +0000] "GET /media HTTP/1.1" 404 571 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36" "-"
還有一個問題是,我沒有看到任何重寫日誌,猜測它可能已經與兩個地點都不匹配的事實相關。
微服務是以JSON還是html格式回覆?因爲對於html的工作服務需要期望一個基地址 –
@TarunLalwani JSON只 – Sergey