Trying to use nginx + passenger for a rails 6 + angular 14 app...

Patrick J. Collins patrick at collinatorstudios.com
Thu Feb 15 20:48:01 UTC 2024


I have a rails app that is purely an api server, with an angular frontend living under a subfolder in the public directory..  So the server's file structure is like this:

```
home/
  my-app/
    app/
      controllers/
      ..etc
    config/
      ..etc
    public
      favicon.ico
      404.html
      angular-app/
        index.html
        12345.js
        45678.js
        some-picture.jpg
```

I had a wildcard route in my rails app that was manually loading the angular files and serving them, and I am trying to let nginx server them as static files and no longer do this.  The desired behavior that I am looking for is:

* visiting '/api/*' will render the response from the rails server
* visiting '/' will render the index file inside `public/angular-app/index.html`
* visiting '/404.html' will render `public/404.html`
* visiting '/some-picture.jpg' will render `public/angular-app/some-picture.jpg`

it seems that passenger is breaking the behavior that I want...  If I have just a vanilla nginx config that has:

```
server {
  ...
  root '/home/my-app/public'

  location = / {
    index angular-app/index.html;
  }

  location {
    try_files $uri angular-app/$uri;
  }
  ...
}
```

Then the behavior for visiting `/`, `/404.html`, and `/some-picture.jpg` is all perfect...  But to get the rails server stuff to work, I need passenger... and as I add to the config:
```
    passenger_app_root /home/my-app/;
    passenger_enabled on;
```

Then trying to go to anything other than an api route (routes defined in the rails app's config) will result in a 500...

I tried experimenting with restructuring my nginx config to something like:
```
server {
  ...
  root '/home/my-app/public'

  location /api/ {
    passenger_app_root /home/my-app/;
    passenger_enabled on;
  }

  location ^/api/ {
    index angular-app/index.html;
    passenger_enabled off;
  }
  ...
}
```
And I still end up with 500s going anywhere other than api endpoints...  Does
anyone one know what I can do to make this work?

Patrick J. Collins
https://collinatorstudios.com


More information about the nginx mailing list