0

I am completely new to nginx and I am trying to set something up so that it is possible to navigate from an existing application on a remote server to a node application. The node app will be on the same server. For now, everything is local on my own machine and I have fired up a simple nginx instance with it's default index page, i.e., navigating to https://localhost will render the default Welcome to Nginx page. I would like to be able to navigate to my node app with https://localhost/app, where the app part of the url is just an alias. There is no app directory in reality. Is this possible?

Currently if I run localhost:3000 in my browser, it will redirect to localhost:3000/login if the user is not authenticated. Therefore https://localhost/app should also redirect to https://localhost/app/login and so on. I just cannot get this to work as I'm getting 404s with the server trying to request static resources with app in the uri i.e, .../public/app/css. It should be .../public/css. Same for js and images. So obviously my alias is not working. My config is as follows:

upstream nodeapp {
    server localhost:3000;
}

server {
    listen 80;
    listen [::]:80;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;
    include snippets/snakeoil.conf; # Temp cert for testing

    location /app/ {
            alias /home/path/to/public;
            proxy_pass http://nodeApp;
            proxy_http_version 1.1;
            proxy_set_header Upgrade websocket;
            proxy_set_header Connection upgrade;
    }

I have looked at lots of similar questions and tried various configurations, but I'm completely out of ideas now. Any help would be most appreciated!

EDIT: if I add a slash to the end of proxy_pass proxy_pass http://nodeApp/; It redirects to localhost/login where I get a 404 as obviously there is no page set up there. Would I really have to add a location for every route in my node app? There must be a way of doing this. :-/

1 Answer 1

0

The redirect problem can be easily solved with the proxy_redirect directive:

proxy_redirect http://nodeApp/ /app/;

or more easily:

location /app/ {
    proxy_pass http://nodeApp/;
    proxy_redirect default;
    # rest of directives
}

However this will be the first of the problems you'll encounter with incorrect URLs: the links generated by your application, which use absolute paths, will lack the /app component. The easiest solution to this problem is to deploy your application at http://localhost:3000/app by tweaking the Express Router.

1
  • Thank you very much! Redeploying to localhost:3000/app was the fix here. Makes sense! Thanks again!
    – Metaman
    Feb 14, 2020 at 10:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .