nginx / unit

NGINX Unit - universal web app server - a lightweight and versatile open source server that simplifies the application stack by natively executing application code across eight different programming language runtimes.
https://unit.nginx.org
Apache License 2.0
5.37k stars 323 forks source link

Multiple proxy doesn't work #739

Closed Pavlusha311245 closed 2 years ago

Pavlusha311245 commented 2 years ago

Unit version 1.27.0

{
    "listeners": {
        "*:80": {
            "pass": "routes"
        },
        "*:443": {
            "pass": "routes",
            "tls": {
                "certificate": [
                    "testbundle"
                ]
            }
        }
    },

    "routes": [
        {
            "match": {
                "uri": [
                    "!/index.php",
                    "!/test/*",
                    "!/lala/*"
                ]
            },
            "action": {
                "share": "/var/www/laravel/public$uri",
                "fallback": {
                    "pass": "applications/laravel"
                }
            }
        },
        {
            "match": {
                "uri": "/test/*"
            },
            "action": {
                "proxy": "http://127.0.0.1:8000"
            }
        },
        {
            "match": {
                "uri": "/lala/*"
            },
            "action": {
                "proxy": "http://127.0.0.1:80"
            }
        }
    ],

    "applications": {
        "laravel": {
            "type": "php",
            "root": "/var/www/laravel/public/",
            "script": "index.php"
        }
    }
}

I have configuration file like this. But I get problem with it. I tried to set paths to use proxy to any application. Expecting: :80 - laravel :80/api - application 2 :80/test - wordpress

Result

Screenshot 2022-07-27 at 11 14 51 AM Screenshot 2022-07-27 at 11 15 06 AM

I haven't idea why redirect didn't work

tippexs commented 2 years ago

Hi @Pavlusha311245 thanks for reaching out.

As all of your applications are PHP-based, why did you not run all the applications on Unit and use the application object to route traffic to your applications.

Anyhow, please note that rewrites are currently not a thing in Unit. That means if you want to use location based routes for different applications, the application has to be aware of it.

Wordpress for example need to be configured to use /test/ as your home location instead of /. Otherwise you would the the 301 redirect.

I will check the configuration and let you know my findings.

Pavlusha311245 commented 2 years ago

I can't use only PHP application, beacause other ones started on docker

tippexs commented 2 years ago

Hi @Pavlusha311245 I have looked at your configuration and created a smal test on my side using the following configuration

{
        "listeners": {
            "*:80": {
                "pass": "routes"
            }
        },

        "routes": [
            {"match": {"uri": "!/test/*"}, "action": {"return": 413}},
            {"match": {"uri": "/test/*"}, "action": {"proxy": "http://127.0.0.1:8000"}}
        ],

        "applications": {}
}

The backend for port 8000 is a simple Python HTTP Server started with python3 -m http.server 8000. This configuration works for me and I can reach my Python backend issuing curl localhost/test/.

That said, the proxy configuration itself is correct. I assume the problem is based on your WP configuration. As already mentioned, if wordpress is NOT configured to be hosted with a location like /test/ it will redirect the request to /.

Can you share the output of curl -v http://localhost/test/?

One last thing. Your /lala/ location will create a loop.

 {
            "match": {
                "uri": "/lala/*"
            },
            "action": {
                "proxy": "http://127.0.0.1:80"
            }
        }

as you are proxying back to Unit and Unit will find /lala/ in its routes again it will end up in an request loop and that will end up in running out of available file descriptors after some time.

Pavlusha311245 commented 2 years ago

Hi @Pavlusha311245 I have looked at your configuration and created a smal test on my side using the following configuration

{
      "listeners": {
          "*:80": {
              "pass": "routes"
          }
      },

      "routes": [
          {"match": {"uri": "!/test/*"}, "action": {"return": 413}},
          {"match": {"uri": "/test/*"}, "action": {"proxy": "http://127.0.0.1:8000"}}
      ],

      "applications": {}
}

The backend for port 8000 is a simple Python HTTP Server started with python3 -m http.server 8000. This configuration works for me and I can reach my Python backend issuing curl localhost/test/.

That said, the proxy configuration itself is correct. I assume the problem is based on your WP configuration. As already mentioned, if wordpress is NOT configured to be hosted with a location like /test/ it will redirect the request to /.

Can you share the output of curl -v http://localhost/test/?

One last thing. Your /lala/ location will create a loop.

 {
            "match": {
                "uri": "/lala/*"
            },
            "action": {
                "proxy": "http://127.0.0.1:80"
            }
        }

as you are proxying back to Unit and Unit will find /lala/ in its routes again it will end up in an request loop and that will end up in running out of available file descriptors after some time.

I will try

tippexs commented 2 years ago

Any update on this @Pavlusha311245