Trying to mimic apache alias. Running into rewrite issues

This is a long shot. I’ve been stuck on this for 8 hours or so and I can’t seem to figure out the issue. I have an angular frontend which i want to connect to my api-platform backend. I have a LAMP stack that is working wonderfully, however, I want to play around with vulcan so i have been exploring caddy+frankenphp.

My LAMP stack is setup like so:
angular is at site.[com] (/app/angular/public)
api-platform is at site.[com]/api. ( alias to /app/api/public)

This is what my vhost looks like and it works fine.

	ServerName site.com
	DocumentRoot "/app/angular/public"
	<Directory /app/angular/public>
		Options Indexes ExecCGI SymLinksIfOwnerMatch FollowSymlinks MultiViews
    	AllowOverride All
    	Require all granted
	</Directory>

	Alias "/api" "/app/api/public"
  	<Directory "/app/api/public">
	    Options Indexes FollowSymlinks MultiViews
    	AllowOverride All
    	Require all granted
  	</Directory>
  ## Logging
</VirtualHost>

When I try to convert this to a CaddyFile equivalent I’m running into an issue with my api side of things. My CaddyFile below serves up the angular site just fine. When I try to access site.com/api I’m getting a symfony error No route found for “GET site.com/api/”, which is strange because on my LAMP stack this works, I’m thinking this is a rewrite issue and I can’t seem to figure out the problem.

//CaddyFile

site.com {
    # Serve Angular frontend from this path
    tls /certs/certificate.crt /certs/private.key

    //I also tried handle_path, same results. 
    handle /api/* {
        root * /app/api/public
        try_files {path} /index.php

        php_server
    }
    handle {
        root * /app/angular/public
        try_files {path} /index.html
        file_server
    }
}

If I modify the caddy file to the following, my API serves up fine at site.[com].

site.com {
    # Serve Angular frontend from this path
    tls /certs/certificate.crt /certs/private.key

    handle {
        root * /app/api/public
        php_server
    }
}

Anyone have any ideas as to whats causing my problem?

Try adding redir to your configuration:

site.com {
    redir /api /api/
    # your other configuration settings
}
1 Like

Thanks for the reply. I tried the redir and it didn’t work unfortunately.

I did, however, make a bit of progress. The only way I could get it working (without modifying my index.php) was to setup a reverse proxy.

site.com {
    tls /certs/certificate.crt /certs/private.key
    root * /app/angular/dist/site.com/browser
    # Now proxy all /api/* requests to the backend
    handle_path /api/* {
        reverse_proxy http://127.0.0.1:8081 {
            header_up Host {http.request.host}
            header_up X-Real-IP {http.request.remote}
            header_up X-Forwarded-For {http.request.remote}
            header_up X-Forwarded-Proto {http.request.scheme}
            header_up X-Forwarded-Prefix /api
        }
    }

    handle {
    	@notFileOrDir {
    		not {
    			file {
    				try_files {path}
    			}
    		}
    	}
    	rewrite @notFileOrDir /index.html
        file_server
    }
}

:8081 {
    vulcain
    handle {
        root * /app/api/public
        php_server
        file_server
    }
}
1 Like

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