unit icon indicating copy to clipboard operation
unit copied to clipboard

Multiple proxy doesn't work

Open Pavlusha311245 opened this issue 2 years ago • 4 comments

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

Pavlusha311245 avatar Jul 27 '22 08:07 Pavlusha311245

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.

tippexs avatar Jul 27 '22 12:07 tippexs

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

Pavlusha311245 avatar Jul 28 '22 08:07 Pavlusha311245

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.

tippexs avatar Jul 28 '22 11:07 tippexs

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

Pavlusha311245 avatar Jul 29 '22 05:07 Pavlusha311245

Any update on this @Pavlusha311245

tippexs avatar Aug 23 '22 10:08 tippexs