Score:0

Nginx Show Upstream Server Name and Backend Port With URL Without Trailing Slash

om flag

I have setup a Nginx server as proxy for a back-end. If the back-end is down, Nginx serves from a backup of the back-end. The proxy works when the URL ends with a trailing slash. If I omit the trailing slash, the URL shown becomes the name of the upstream block plus the port of the backup back-end.

  • What works: www.chingu.asia, www.chingu.asia/, and www.chingu.asia/wiki/.
  • What doesn't work: www.chingu.asia/wiki (no trailing slash), my browser is redirected to http://chingu.servers:8111/wiki/, which cannot be found (of course).

Here is my configuration:

upstream chingu.servers {
    server 192.168.0.12      fail_timeout=300s  max_fails=1;
    server 192.168.0.10:8111 backup;
}
server {
    listen 80;
    server_name www.chingu.asia;
    location / {
        proxy_pass http://chingu.servers/;
    }
}
server {
    listen 192.168.0.10:8111;
    server_name _;
    root        /...some path.../Chingu;
    index       index.html index.php;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        try_files     $uri =404;
        include       fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass  unix:/run/php/php7.3-fpm.sock;
        fastcgi_param FQDN true;
    }
}

I have tried using regexes to match any URI with the location blocks. I also tried port_in_redirect off; and absolute_redirect off;.

I could add location block for wiki and others but it seems to defeat the purpose of a proxy if I must add manually every possible sub-directories.

What is it that I'm missing?

Lex Li avatar
vn flag
The change in URL was caused by your upstream server. It tries to beautify the URLs by adding missing `/` but breaks your reverse proxy. You can redirect to `/` ending URLs proactively in your nginx config to avoid redirection from upstream.
Score:0
us flag

Your configuration means that nginx will use chingu.servers as the HTTP Host header when sending the request to upstream.

You need to use

location / {
    proxy_set_header Host $host;
    proxy_pass http://chingu.servers/;
}

in your configuration.

Also, you need to make sure that your Chingu root URL is set to www.chingu.asia.

Tygre avatar
om flag
Thanks @tero, this definitely go into the right direction, I have now `http://www.chingu.asia:8111/wiki/`. How could I get rid of the port number? (I didn't understand what you meant by "make sure that your Chingu root URL is set to www.chingu.asia".)
us flag
You need to check your Chingu application's settings and make sure the root URL defined in the settings is `www.chingu.asia`.
Tygre avatar
om flag
Thanks a lot @Tero!
I sit in a Tesla and translated this thread with Ai:

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.