Proxy & HistoryApi

I want to translate following nginx.conf (simplified)

root /path/to/project/dist;

# For APIs
location ^~ /api/xxx {
  rewrite '^/api(.*)' $1;
  add_header X-Cache-Status $upstream_cache_status;
  expires off;
  proxy_pass http://xxx_server;
  proxy_set_header Host            $host;
  proxy_set_header X-Forwarded-For $remote_addr;

location /api {
  return 404;

# For HTML5 history api
location / {
  try_files $uri $uri/ /index.html;

to Caddyfile

root /path/to/project/dist;

# For APIs
proxy /api/xxx {
  without /api
  upstream xxx_server

# For HTML5 history api
rewrite {
  if {path} not_match ^\/api\/
  to {path} {path}/ /index.html;


  1. Proxies work but it doesn’t seem that they will send GET params at together.
  2. Rewrite doesn’t seem to work. For example http://localhost/login still got 404.
  3. Can I get detailed information / logs for where are these API requests actually proxied to?

Thanks in advance.

Not 100% sure what you mean, are you referring to your upstream server not receiving query parameters?

This can be solved by replacing {path} with either {uri} or {path}?{query} in the to subdirective.

Try ditching the ; on the end of /index.html. If that’s not the problem, it might be the regex, but it looks good to me. You could also try logging the rewrites by appending the {rewrite_uri} placeholder to a custom logging format (see below).

From the docs for the proxy directive:

This middleware adds a placeholder that can be used in log formats: {upstream} - the name of the upstream host to which the request was proxied.

To define a custom logging format, you can take a look at the docs for the log directive. If you just want the default format, but with the upstream appended, you can use log / stdout "{common} {upstream}".

Oops, my fault.

Oh, it turns out to be that I miss-typed the port of API server. Everything worked as expected after I corrected it.

Tried and it worked.

Thanks for your help.

1 Like

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