Nnginx 在一個位置指令中將 url 對應到 proxy_pass

Nnginx 在一個位置指令中將 url 對應到 proxy_pass

我有多個節點 websockets,可以透過與 UNIX 套接字路徑相符的 url 來使用。我不想重複位置指令,而是想要一個類似 URL 清單之類的東西,將用作套接字名稱。使用正規表示式不起作用,因為我需要套接字名稱的路徑。

目前我正在使用這個位置:

location /application1/
{
   proxy_pass http://unix:/var/run/nodejs/application1;
   proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection "upgrade";
}

目標是擁有類似的東西:

$list = [app1, app2, app3];           #list of possible names
location /$list/   #match all names in the list
{
   proxy_pass http://unix:/var/run/nodejs/$list_matched;   #use matched name
   proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection "upgrade";
}

基本上,清單中的每個 URL 都應該重定向到具有相同名稱的套接字。

先感謝您的幫助:)

答案1

location ~* /(<regexp>)/ {
    proxy_pass http://unix:/var/run/nodejs/$1;   #use matched name
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

答案2

您需要使用正規表示式。像這樣的東西應該​​可以工作,但是你必須自己計算出正規表示式。有很多教程和正規表示式實驗站點

location ~ (app1|app2|app3)$ {

相關內容