0

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?

Tygre
  • 1
  • 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. – Lex Li Dec 31 '22 at 04:10

1 Answers1

0

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.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
  • 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".) – Tygre Dec 31 '22 at 20:21
  • You need to check your Chingu application's settings and make sure the root URL defined in the settings is `www.chingu.asia`. – Tero Kilkanen Jan 03 '23 at 11:23
  • Thanks a lot @Tero! – Tygre Jan 04 '23 at 12:13