Static files and proxy (Prerender io)

1. Output of caddy version:

v2.6.2 h1:wKoFIxpmOJLGl3QXoo6PNbYvGW4xLEgo32GPBEjWL8o=

2. How I run Caddy:

systemctl restart caddy

a. System environment:

debian 11

My complete Caddy config:

doman.com {
        @path path /?_escaped_fragment_=
        handle @path {
                reverse_proxy @path localhost:3000
        }

        root * /usr/share/nginx/static
        handle {
                file_server
        }
}

3. The problem I’m having:

I have a domain, the server should give statics, but on a certain url should go to the proxy server

/ - statics
/?escaped_fragment= - proxy

this config doesn’t work like it should

I checked a lot of configurations, no help

Here is my nginx file:

    map $args $prerender_args {                          
        "~(^|&)_escaped_fragment_=" 1;
    }

    map $http_x_prerender $x_prerender {              
        default $prerender_args;
        "1" 0;
    }

    map $uri $prerender {
        default $x_prerender;
        "~*.(js|css|xml|less|png|jpg|jpeg|gif|pdf|doc|txt|ico|rss|zip|mp3|rar|exe|wmv|doc|avi|ppt|mpg|mpeg|tif|wav|mov|psd|ai|xls|mp4|m>
    }


    server {
        server_name scout.hyoo.ru;
        root /usr/share/nginx/scout.hyoo.ru/static/;

        location / {

            if ($prerender = 1) {
                rewrite (.*) /prerenderio last;
            }
            try_files $uri /index.html = 404;
        }

        location /prerenderio {
            if ($prerender = 0) {
                return 404;
            }

            proxy_hide_header Cache-Control;
            add_header Cache-Control "private,max-age=600,must-revalidate";

            #resolve using Google's DNS server to force DNS resolution and prevent caching of IPs
            resolver 8.8.8.8.8.4.4;
            set $prerender_host "127.0.0.1:3000";
            proxy_pass http://$prerender_host;
            rewrite .* /$scheme://$host$request_uri? break;
        }

4. Error messages and/or full log output:

Does work

From my understanding, you are trying to match “https://scout.hyoo.ru/~&_escaped_fragment_=” from expression "~(^|&)_escaped_fragment_=" in your Nginx config , reverse_proxy it to your server at "http://127.0.0.1:3000/prerenderio/_escaped_fragment_=? " based on your rewrite rewrite .* /$scheme://$host$request_uri? break;

Here is my take, but I didn’t try

scout.local {
        root * /usr/share/nginx/scout.hyoo.ru/static/

        @prerender_args {
                expression {path}.matches("(?i)/(^|&)_escaped_fragment_=")
                not path /prerender*
        }
        handle @prerender_args {
                rewrite / /prerenderio{uri}
        }

        @prerender {
               path /prerenderio/*
               expression {path}.matches("(?i)/(^|&)_escaped_fragment_=")
               path_regexp ^/\.(js|css|xml|less|png|jpg|jpeg|gif|pdf|doc|txt|ico|rss|zip|mp3|rar|exe|wmv|doc|avi|ppt|mpg|mpeg|tif|wav|mov|psd|ai|xls|mp4)$
        }
        handle @prerender {
            route {
                header +Cache-Control "private,max-age=600,must-revalidate"
                reverse_proxy http://127.0.0.1:3000 {
                    rewrite /{uri}$3F
                }
            }
        }

        file_server
        try_files {path} {path} /index.html =404
}
1 Like

doesn’t work.

No, https://scout.hyoo.ru/~&_escaped_fragment_=https://127.0.0.1:3000/https://scout.hyoo.ru/~&_escaped_fragment_= or https://127.0.0.1:3000/https://scout.hyoo.ru

We need to pass on the entire link

Just to make sure we’re clear, https://scout.hyoo.ru/~&_escaped_fragment_= is the actual, literal (i.e. not decoded) URL you want to match?

https://scout.hyoo.ru/?_escaped_fragment_= - yes

Ok; so, the path on that URL is /. The query string is ?_escaped_fragment=, but the path matcher doesn’t match query strings.

What you can do is combine them:

@proxy {
    path /
    query _escaped_fragment=
}
handle @proxy ...

Or something like that.

1 Like
scout.hyoo.ru {
        root * /usr/share/nginx/scout.hyoo.ru/static/

        @prerender {
                path /
                query _escaped_fragment=
        }
        
        route @prerender {
                redir https://google.com
        }

        file_server
        try_files {path} {path} /index.html =404
}

Not work

in both cases it gives away static

as if the query condition does not work

Sorry, I omitted a _ in my example, which your config is also missing… please double-check and try again!

Also, try_files has a higher directive order than route, so that rewrite will happen first, causing the path / matcher to no longer match. You don’t need route in this case. Try this instead:

	@prerender {
		path /
		query _escaped_fragment=
	}
	redir @prerender https://google.com
1 Like

(but with the _ added in there :joy: )

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.