Nginx forwards static content to a subdirectory within the main root directory

I have an application running at http://192.168.0.2:8080/. The main index.html page is located in the /web directory and it fetches static files, such as css, from the /css folder.

My aim is to use nginx as a reverse proxy to redirect myapp.mydomain.com to my application. In my current nginx.conf configuration, I have:

server {
        listen       80;
        server_name  myapp.mydomain.com;
        satisfy any;
        location / {
                proxy_pass  http://192.168.0.2:8080/web/;
                index  index.html index.htm;
        }
}

Unfortunately, this setup does not work for fetching css files as it expects them to be in the /web/css path. To work around this issue, I adjusted my nginx.conf by removing the /web path like so:

server {
            listen       80;
            server_name  myapp.mydomain.com;
            satisfy any;
            location / {
                    proxy_pass  http://192.168.0.2:8080/;
                    index  index.html index.htm;
            }
    }

With this modification, I need to access http://myapp.mydomain.com/web each time. However, I would prefer to simply request http://myapp.mydomain.com/ and let nginx handle the routing.

I believe that implementing something similar to the below snippet could potentially solve my issue, but I haven't been able to figure out how:

location ~ .(css|img|js)/(.+)$ {
                try_files $uri /$uri /$1/$2;
        }

location / {
                proxy_pass  http://192.168.0.2:8080/web/;
                index  index.html index.htm;
        }

Below is my complete configuration file, which also includes authentication settings:

upstream app { server 192.168.0.2:8080; }
server {
        listen       80;
        server_name  myapp.mydomain.com myapp.myOtherDomain.com;
        satisfy any;
        allow 192.168.0.0/24;
        deny all;
        auth_basic "closed site";
        auth_basic_user_file /etc/nginx/auth/passwords;
        location / { proxy_pass http://app/web/; }
        location /css { proxy_pass http://app; }
        location /img { proxy_pass http://app; }
        location /js { proxy_pass http://app; }
}

If anyone has any suggestions on how I can resolve this issue, I would greatly appreciate it.

Thank you.

Answer №1

Based on my understanding, your current setup is functioning properly and the only issue you are facing is redirecting the URL http://myapp.mydomain.com/ to http://192.168.0.2:8080/web/.

Your existing configuration looks like this:

server {
  listen       80;
  server_name  myapp.mydomain.com;
  satisfy any;
  location / {
    proxy_pass  http://192.168.0.2:8080/;
    index  index.html index.htm;
  }
}

To resolve this, a straightforward approach would be to add a specific match for the / URI. Here's how you can achieve that:

server {
  listen       80;
  server_name  myapp.mydomain.com;
  satisfy any;
  location = / { rewrite ^ /web/; }
  location / {
    proxy_pass  http://192.168.0.2:8080/;
    index  index.html index.htm;
  }
}

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

Develop a TypeScript class in a distinct file

I currently have ag-grid implemented in an Angular project with a CustomFilter. The problem is that the file containing the code for the CustomFilter function is becoming quite large and difficult to manage. I am now looking to move the CustomFilter to a s ...

Load a Javascript file from the local server

My website is encountering a problem when trying to load a script from a localhost server. The error message displayed is PROTOCOL ERROR: https://localhost:4200/script/test.js net::ERR_SSL_PROTOCOL_ERROR Here is the code snippet causing the issue: <!DO ...

Tips for accessing child elements within an Angular component

I'm struggling to get a reference of child elements within a component. I have experimented with ElementRef, TemplateRef, QueryList, ViewChild, ViewChildren, ContentChild, and ContentChildren without success. <app-modal> <section #referenc ...

unable to retrieve the chosen item from the dropdown menu

How can I retrieve the value of the selected item from a dropdown menu without getting an undefined error? You can find my code on Jsfiddle. Can anyone spot what might be causing this issue? <select class="ddl_status" id="status_ddl_20" style="display ...

Displaying a pop-up message over the "Login button" for users who are not currently logged in

I am currently in the process of developing a website using node.js and express. I have successfully integrated all the login functionality through passport, allowing users to easily log in or out by utilizing res.user. However, I now want to enhance the ...

Inconsistent Functionality of Bootstrap Popover

I am experiencing an issue with the bootstrap Popover feature. It seems to work inconsistently - sometimes it works and other times it doesn't. I am using it to create a popover that displays a user's information when a visitor hovers over the us ...

Designing a horizontal CSS form with boxes positioned above the text, all done without relying on tables for layout

Do I need to divide everything into divs in order to create a table like the picture I made below? ...

Struggling with a TypeError in React/Next-js: Why is it saying "Cannot read properties of undefined" for 'id' when the object is clearly filled with data?

Encountering an issue with a checkbox list snippet in Next-js and React after moving it to the sandbox. Each time I click on a checkbox, I receive the error message: TypeError: Cannot read properties of undefined (reading 'id') This error is co ...

Creating a loop in Javascript that accesses values from a JSON file and uses the nested data within the loop as a separate JSON object

I am working with a list of user uuids stored in the myContactsUuids array. I am using the forEach() method to iterate through them and add each user, retrieved using the new ChatEngine.User(uuid) function, to the myContacts array. myContactsUuids = ["joh ...

Please replace the text with only letters and then submit it without any spaces, periods, commas, or other symbols

Whenever I encounter a form like this, I need to submit it and replace the text in the name field with only letters - no commas, spaces, or special characters. https://i.sstatic.net/9FQnT.png myform.html <script> function fixInput(event) { na ...

The essential criteria for script tag and page validation requirements

There are instances where I have pages that contain only a script without any content (such as sending data through postMessage and then closing itself). In these cases, is the page considered valid with just <script>doSomeStuff</script> or do ...

What is the best way to control the number of fetch calls sent to an endpoint service ensuring that only a maximum of 10 calls are made every second?

Is there a way to prevent the browser from encountering the "Error 429 Too Many Requests" by adjusting the code below to ensure that no more than 10 calls per second are made? Please note that the rate limit of 10 calls per second is set by a 3rd-party AP ...

Creating randomized sequences using JavaScript

One of my hobbies involves organizing an online ice hockey game league where teams from different conferences compete. It's important to me that every team gets an equal number of home and away matches throughout the season. To simplify this task, I&a ...

How to trigger a file download instead of opening it in a new tab when clicking on a txt or png file in AngularJS

After retrieving my file URL from the backend API, I am trying to enable downloading when the user clicks a button. Currently, the download function works smoothly for Excel files (`.xlsx`), but for text (`.txt`) files or images (`.jpeg`, `.png`), it only ...

What is the proper way to define an element's style property in strict mode?

Assuming: const body = document.getElementsByTagName('body')[0]; const iframe = document.createElement('iframe'); iframe.src = protocol + settings.scriptUrl + a; iframe.height = 1; iframe.width = 1; iframe.scrolling = 'no'; ...

Tick the checkboxes that are not disabled, and leave the disabled ones unchecked

Currently, I am employing Jquery for the purpose of checking and unchecking checkboxes. However, some of these boxes are disabled, thus there is no need for them to be checked. Is there a method by which I can instruct the script to disregard disabled che ...

I am looking to create a unique JavaScript variable within my GTM container that will automatically return a value of true when the user approves sharing

Is there a way to trigger a tag when the user shares their location with the browser? I attempted using the following code within a custom JavaScript variable in my GTM Container, but it didn't seem to work. navigator.permissions && navigator ...

retrieve information using Python in JavaScript

I am in the process of developing a website using Python, Javascript (JQuery), and AJAX. While I know how to initiate a Python script with Ajax, I am unsure of how to send data back to Javascript from Python. For instance, if there is an error in a form s ...

Dealing with numerous condition matches in Node.js: Tips and Tricks

Currently, I am developing an API in Express.js where I have to check for certain conditions before sending a response. The issue I'm facing is that if two conditions are met, my code ends up responding twice. Is there a way to prevent the other condi ...

Utilize/Absolve/Add a Prefix to angular material scss styles

Issue I am facing a challenge with integrating angular material SCSS into my application. I want to ensure that these styles are isolated and scoped specifically for my application, as it will be embedded within a larger monolith. The goal is to prevent a ...