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

Infinite redirect on static share resources #975

Closed ryan-marshall-evident closed 11 months ago

ryan-marshall-evident commented 11 months ago

I have been trying to set up a PHP application that also serves static files. Unfortunately, when I request the static assets, I get a 301 redirect to the same file with a / appended.

So http://localhost/file.html redirects to http://localhost/file.html/ which redirects to http://localhost/file.html//.

I have tried to strip back the configuration to just serve the static files (see config below), however, this still results in the redirects.

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

    "routes": [
        {
            "action": {
                "share": "/www/public$uri"
            }
        }
    ],
    "settings": {
        "http": {
            "log_route": true
        }
    }
}

image

image

lcrilly commented 11 months ago

I cannot reproduce. This looks like a Docker container (judging by the hostname). Could you share a Dockerfile that exhibits this behaviour?

ryan-marshall-evident commented 11 months ago

Here's a stripped-down docker file I've tested this on:

FROM unit:1.31.0-php8.2@sha256:3489384ba2bdfa707d08431b335b0f776c206863e93e5998892199036d767468 as dev

COPY <<EOF /docker-entrypoint.d/config.json
{
    "listeners": {
        "*:80": {
            "pass": "routes"
        }
     },

    "routes": [
        {
            "action": {
                "share": "/www/public$uri"
            }
        }
    ],
    "settings": {
        "http": {
            "log_route": true
        }
    }
}
EOF

COPY <<EOF /www/public/file.html
<html>
    <head>
        <title>Unit</title>
    </head>
    <body>
        <h1>Unit</h1>
        <p>Hello world!</p>
    </body>
</html>
EOF

WORKDIR /www

EXPOSE 80

Running on docker desktop version 4.24.0 (122432).

tippexs commented 11 months ago

I had another look on this. The configuration you are creating in you Dockerfile will not arrive in your container as you think. $uri WILL NOT BE interpreted as a string as it includes a $ and therfore it should be variable name.

Simple Test:

COPY <<EOF /tets.txt

abc $123

EOF
$ docker run --rm -it -P tippexs/unit:fi cat /tets.txt

abc

We have to escape the $ to make this work:

COPY <<EOF /docker-entrypoint.d/config.json
{
    "listeners": {
        "*:80": {
            "pass": "routes"
        }
     },

    "routes": [
        {
            "action": {
                "share": "/www/public\$uri"
            }
        }
    ],
    "settings": {
        "http": {
            "log_route": true
        }
    }
}
EOF

Going to close this issue now.

ryan-marshall-evident commented 11 months ago

@tippexs, thanks for your response. I've just tested this and it worked a treat. I had a feeling it was going to be something straightforward.