2017-05-29 82 views
7

Nginx'i proxy'yi başka bir sunucuya iletme isteğini, yalnızca $ request_body değişkeni belirli bir normal ifadeyle eşleşiyorsa yapılandırmaya çalışıyorum.Ama benim için çalışmıyor.nginx koşullu proxy geçişi, gövde içeriğine bağlı olarak

server{ 
     listen 80 default; 
     server_name www.applozic.com; 

     location/{ 
       proxy_set_header X-Real-IP $remote_addr; 
       proxy_set_header X-Forwarded-For $remote_addr; 
       proxy_set_header Host $http_host; 

       if ($request_body ~* (.*)appId(.*)) { 
        proxy_pass http://apps.applozic.com; 
       } 
     } 

}

istek organıdır :: En iyi anlarım

   { 
       "applicationId": "appId", 
       "authenticationTypeId": 1, 
       "enableEncryption": false, 
       "notificationMode": 0, 
       "deviceType": 4, 
       } 
+1

Olası kopyalar (https://stackoverflow.com/questions/7888165/nginx-match-specific-word-in-location) – AlexD

cevap

1
I found the solution. 

I did following changes in nginx(open resty) config file 

upstream algoapp { 
    server 127.0.0.0.1:543; 
} 
upstream main { 
    server 127.0.0.1:443; 
} 

location /rest/ws/login { 
    proxy_set_header X-Forwarded-Host $host; 
    proxy_set_header X-Forwarded-Server $host; 
    proxy_set_header X-Real-IP $remote_addr; 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    proxy_set_header X-Url-Scheme $scheme; 
    proxy_set_header X-Forwarded-Proto $scheme; 
    proxy_set_header Host $http_host; 
    proxy_redirect off; 
    if ($request_method = OPTIONS) { 
    proxy_pass https://127.0.0.1:543; 
    } 
    if ($request_method = POST) { 
    set $upstream ''; 
    access_by_lua ' 
    ngx.req.read_body() 
    local data = ngx.req.get_body_data() 
    local match = ngx.re.match(ngx.var.request_body, "appId") 
    if match then 
     ngx.var.upstream = "algoapp" 
    else 
    ngx.var.upstream = "main" 
    end 
    '; 
    proxy_pass https://$upstream; 
    } 
    } 
[konumda nginx maçı belirli bir kelime] arasında