2017-09-17 50 views
0

我有一個由運行在後端專用網絡中的docker管理的微服務,並且希望設置一個由nginx管理的動態路由,以便它可以根據URI選擇微服務。我有三個問題:Nginx重寫到根目錄下的子文件夾

  1. 我需要的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服務器創建一個單獨的位置?

  2. 我還需要它根據相同的第一個URI路徑段重寫到根目錄下的文件夾。我寫了一個配置,但得到404錯誤。我是nginx的新手,只注意到nginx和php-fpm容器中的路徑不匹配。 404錯誤可能與這個事實有關嗎?

  3. 這樣的路由是否可能(詳情請參閱下面的配置)?另一種選擇是爲每個微服務創建一個單獨的位置,但我不希望每次添加或刪除微服務時都更改此配置。

這裏是我的配置:

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" "-" 

還有一個問題是,我沒有看到任何重寫日誌,猜測它可能已經與兩個地點都不匹配的事實相關。

+0

微服務是以JSON還是html格式回覆?因爲對於html的工作服務需要期望一個基地址 –

+0

@TarunLalwani JSON只 – Sergey

回答

0

我已經想出以下nginx的配置,允許將請求路由到應用程序在不同的泊塢窗容器。感謝@TarunLalwani的提示:

server { 
    server_name _; 

    location ~ ^/(?P<service>\w+)(/|$) { 
     root /services/$service/web; 

     rewrite^/app_dev.php$is_args$args break; 

     resolver 127.0.0.11; 
     fastcgi_pass $service:9000; 
     fastcgi_split_path_info ^(.+\.php)(/.*)$; 
     include fastcgi_params; 

     fastcgi_param SCRIPT_FILENAME /app/web/app_dev.php; 
     fastcgi_param DOCUMENT_ROOT /app/web; 
    } 

    location ~ \.php$ { 
     return 404; 
    } 

    error_log /var/log/nginx/error.log debug; 
    access_log /var/log/nginx/access.log; 
    rewrite_log on; 
} 

的應用途徑還需要一個前綴匹配到微服務的名字,我的文件app/config/routing.yml(我使用的Symfony)中添加它:

app: 
    resource: '@AppBundle/Controller/' 
    type: annotation 
    prefix: /media 

微服務的代碼被安裝到應用程序容器的/ services目錄下的nginx容器中。 nginx容器中的絕對路徑和應用程序的容器不匹配,因此它使用fastcgi參數進行映射。

1

所以這是有可能在同一位置塊本身

location ~ ^/(?P<app>\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 $app:9000; 
    fastcgi_split_path_info ^(.+\.php)(/.*)$; 
    include fastcgi_params; 

    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; 
    fastcgi_param DOCUMENT_ROOT $realpath_root; 
} 
相關問題