Nginx proxy_pass only works partially

后端 未结 1 1417
天涯浪人
天涯浪人 2021-01-22 05:40

I have the following setup

  • Master server - call it https://master.com
  • Slave server - call it https://slave.com

Bo

相关标签:
1条回答
  • 2021-01-22 06:09

    Your approach is wrong. Inside the block which handles /test your rewrite it and send it out of the block. The proxy_pass never actually happens because the new URL doesn't have /test in it. Solution is simple, don't use rewrite

    location /test/
    {
     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;
    }
    

    Appending / at the end of the location path and also the proxy_pass server will make sure what is after /test/ is sent to your proxy_pass address

    Edit-1

    Here is a sample test case I had set before posting this answer.

    events {
        worker_connections  1024;
    }
    http {
    server {
       listen 80;
    
       location /test1 {
         proxy_pass http://127.0.0.1:81;
       }
    
       location /test2 {
         proxy_pass http://127.0.0.1:81/;
       }
    
       location /test3/ {
         proxy_pass http://127.0.0.1:81;
       }
    
       location /test4/ {
         proxy_pass http://127.0.0.1:81/;
       }
    
    }
    
    server {
       listen 81;
    
       location / {
         echo "$request_uri";
       }
    }
    }
    

    Now the results explains the difference between all 4 location blocks

    $ curl http://192.168.33.100/test1/abc/test
    /test1/abc/test
    
    $ curl http://192.168.33.100/test2/abc/test
    //abc/test
    
    $ curl http://192.168.33.100/test3/abc/test
    /test3/abc/test
    
    $ curl http://192.168.33.100/test4/abc/test
    /abc/test
    

    As you can see in /test4 url the proxied server only sees /abc/test

    0 讨论(0)
提交回复
热议问题