What is the best way to host a single page application within a sub-directory using nginx?

Trying to set up nginx to host an Angular application from a unique child path. Adjusted the app to use a base href of fish, able to serve root page and assets correctly.

However, encountering a 404 error when attempting to reload the page on a child route.

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
  worker_connections  1024;
}


http {
  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;

  log_format  main  '$request_uri --- $remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

  access_log  /var/log/nginx/access.log  main;

  sendfile        on;
  keepalive_timeout  65;


    upstream docker-backend-stream {
      server fish-api:80;
    }

    server {
      listen 80;
      autoindex off;

      location / {
            rewrite ^ http://www.google.com redirect;
      }

      location /fish {
        alias /app;
        index index.html;
        try_files $uri $uri/ /index.html;
      }

      location /fish/api {
            proxy_pass http://docker-backend-stream;
            auth_basic          off;
            client_max_body_size  0;
        }
    }
}

Issue: Why does a GET request to http://localhost:80/fish/foo result in a 404 Not Found instead of serving index.html?

Logs:

as-web_1       | 2017/12/01 18:16:14 [error] 8#8: *2 open() "/etc/nginx/html/index.html" failed (2: No such file or directory), client: 172.24.0.1, server: , request: "GET /fish/foo HTTP/1.1", host: "localhost"
as-web_1       | /fish/foo --- 172.24.0.1 - - [01/Dec/2017:18:16:14 +0000] "GET /fish/foo HTTP/1.1" 404 571 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36" "-"

Seems like the location directive isn't being matched, causing nginx to look for assets in the default root location of /etc/nginx/html.

Answer №1

There appears to be an issue with the historical nginx bug that is causing problems with both root and alias in a location directive.

To work around this, I have implemented a conditional check followed by a regex & rewrite:

  location /fish {
    alias /app;
    if (!-e $request_filename) {
      rewrite ^[^.]*$ /fish/index.html last;
    }
  }

Please note that there is room for improvement by checking for file extensions.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to ensure string comparison safety in Angular templates when dealing with null or undefined values?

Okay, so I encountered the following scenario: <span *ngIf="!someItem?.description==''"> The situation is: If someItem is defined and the description is not an empty string. Essentially, this can be interpreted as not equal to any value X ...

Having trouble fetching values from an object in Angular? Receive an undefined result

Currently, I am attempting to extract values from a backend object. The initial demonstration works fine: export class Test { public StringValue: string | undefined; public BoolValue: boolean | undefined; public TestList: Array<User>; } Additi ...

Displaying icons representing different countries using Angular framework

Seeking assistance with Angular - I obtained a collection of country icons (svg format) from flat icon and intend to display them based on the respective countries in my project. With 870 icons, what would be the simplest approach to accomplish this with ...

Angular 2 radio button problem with model-driven form

I'm facing a challenge with getting radio buttons to function correctly in my reactive form. Below is the code for my component: export class SettingsFormComponent implements OnInit { public sForm: FormGroup; public submitted: boolean; d ...

Angular-fontawesome icons are experiencing issues with their background color not scaling properly

Currently utilizing angular-fontawesome, I am seeking to alter the background color of a font-awesome fa-icon <fa-icon class="vue-icon" [icon]="faVue" ></fa-icon> To change the color, I modified the CSS using ...

Having trouble launching simple ionic template: Issue with locating module 'fast-deep-equal'

Recently I started using Ionic and followed the steps to install the basic blank template as shown below. First, I installed Ionic and Cordova by running the command: npm install -g ionic Cordova. After that, I created my first Ionic project using the f ...

What could be causing my node server to display a blank page when it is pointing to the /dist folder of an Angular application?

Currently, my node server setup is as follows: const express = require('express'); const app = express(); app.get('*', (req, res) => { res.sendFile(__dirname + '/dist/page/index.html'); }) app.listen(3335, () => { c ...

Tips for utilizing the @Input() property of a component to set the initial value of an input field

Is there a way to utilize the value of an @Input() property on Component B as the initial value for an input field in that component when it is contained within Component A? I attempted passing the value during form construction, but found that it only wo ...

Is there a way to run a node script from any location in the command line similar to how Angular's "

Currently, I am developing a node module that performs certain functions. I want to create a command similar to Angular's ng command. However, I am facing compatibility issues with Windows and Linux operating systems. Despite my attempts to modify the ...

Is it possible for Angular Components to be dynamically generated based on a parameter?

Is it feasible to achieve the following functionality in Angular? I am interested in creating multiple components that share a common base interface of properties; for instance, a string component, a date component, and an integer component, each with uni ...

The Exception: Angular Two ExpressionChangedAfterItHasBeenCheckedError

I have thoroughly reviewed the documentation regarding this error and I believe I comprehend the underlying cause. However, I am currently struggling to find an alternative architecture that would be suitable for my needs. Within the root component of my ...

What could be causing my function to not provide the expected output?

Whenever I try to invoke the function from another part of the code, I encounter an issue where it returns undefined before actually executing the function. The function is stored in a service. login.page.ts: ngOnInit(){ console.log(this.auth.getRole()) ...

Is it possible to verify if the @Output is correctly wired up within an Angular component?

When working with Angular and TypeScript, it is possible to access the bound @Input values in the ngOnInit method of a component. However, there isn't a straightforward way to check if a particular @Output event binding has been set up on the componen ...

The REST API seems to be functioning correctly when accessed through Postman, but there are issues when attempting to call

When I include @PreAuthorize("hasRole('ROLE_SUPER_ADMIN')") in my controller and make a call from Angular, it doesn't work. However, it works fine when called from Postman. @GetMapping("/getAllOrgniz") @PreAuthorize("hasRole('ROLE_SUPE ...

Tips for assigning a value to a Reactive Form control within Angular 6

I am looking to dynamically set the value of a text box when clicking on a button that is located outside of the form. How can I achieve this? <form [formGroup]='student' (ngSubmit)='click()'> <input type='text' form ...

Creating HTML code using an array of objects

My goal is to create HTML tags based on the object response shown below: "view": [{ "type": 'text', "depth": 0, "text": "This is a sample text" }] The objective here is to iterate through each type and add the corresponding HTML tags. &l ...

What could be the reason behind my Ionic app receiving a 401 error from an API call while Postman is not encountering the

Following the instructions from a tutorial on Smart of the Home, I implemented the code below in a provider for my Ionic 2 App. return new Promise(resolve => { // Using Angular HTTP provider to make a request and process the response let headers = ...

Navigating with the router on a different page

My appcomponent contains all the routes, and on the next page I have several links that are supposed to route to the same router outlet. How can I navigate when a link is clicked? I attempted using [routerLink]="['PersonInvolved']", but I encoun ...

When the @Input() contains an unaltered boolean value, it does not initiate change detection

I have developed an Angular application featuring a popup component. The visibility of the popups can be controlled both from its parent and the popup itself. app.component.ts import { Component } from '@angular/core'; @Component({ selector: ...

Angular and WEB API experiencing issues with the update function synchronization

Currently, I'm developing a CRUD example using dotnet core and Angular. In the backend, I have implemented a function in the CarController.cs as shown below: CarController.cs [Route("UpdateCar")] [HttpPut] public IActionResult Put([ ...