Score:0

How do I make my reverse proxied Nginx server access other routed files in an express app running on port 3000?

it flag

Currently I have an express app inside /usr/share/nginx/myexpress

The express application is listening and working perfectly on port 3000. My NGINX server is reverse proxying to it and I can view the default express page by going to kodix.com.br.

The problem is, I can't go to kodix.com.br/hello for example after routing it. I can only access it by going through the ip address. (Like this example but with other numbers: 143.94.233.176:3000/hello)

Here's my current sites-available nginx configuration:

upstream node_app{
server 127.0.0.1:3000;
}

server {

    root /usr/share/nginx/myexpress;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name kodix.com.br www.kodix.com.br;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
        proxy_pass http://node_app;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

}

How do I make it so I can access other express pages using my nginx server?

djdomi avatar
za flag
please fix your question, it's not clear what you like to do
Gerard H. Pille avatar
in flag
If I remember correctly, a location ending in /, should proxy_pass to a location ending in /, as in "proxy_pass http : //node_app/;" (leave out the blanks arround the :).
Gabriel avatar
it flag
@djdomi I want to access the other routed express pages, for example accessing kodix.com.br/hello would send me to the routed view page I set up inside the views folder from express.
Gabriel avatar
it flag
@GerardH.Pille Doing this just makes sudo nginx -t return with an error.
Gerard H. Pille avatar
in flag
"I can't go": what happens if you try?
Score:0
it flag

I am not sure if this is the correct way of doing it, but I managed to make it work by adding a location block to the server block:

location /hello/ {
    proxy_pass http://localhost:3000/hello;
}

Then, accessing myserver.com.br/hello redirects me to the correct page.

djdomi avatar
za flag
but for what reason do you have root /usr/share/nginx/myexpress; in case you use reverse_proxy?
Score:0
cz flag

The normal soluton to this is to use a named location. In this setup, nginx will serve static content first. If no static content exists for the URL path, instead of returning a 404, it is then passed to the named location, which sends it to your app.

It's a very simple addition:

    location / {
        try_files $uri $uri/ @express;
    }

    location @express {
        proxy_pass http://node_app;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

P.S. It is not a good idea to store your web files in /usr/share/nginx. This directory is owned by the package manager and it has the authority to delete everything in it whenever the nginx package is updated or removed. (It most likely will not do, but this is a risk that I would not take.)

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.