Too many redirected error with Nginx on PHP

Jordan Baron asked:

I have 2 node apps running on different ports and a PHP app. When I visit the node apps I get no errors but if I try to go to the PHP app (on /sourcebans) I get This site redirected you too many times How can I fix this?

My config

server {
  server_name 108.61.142.108;
  return 301 https://hwgaming.tf$request_uri;
}

#server {
#  server_name www.hwgaming.tf;
#  return 301 https://hwgaming.tf$request_uri;
#}

server {
  server_name hwgaming.tf;
#       rewrite ^/(.*)/$ /$1 permanent;
        root /var/www/html/hwgaming/;
        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;


        location / {
#                First attempt to serve request as file, then
#                as directory, then fall back to displaying a 404.
                proxy_pass http://localhost:9000;
                proxy_http_version 1.1;
                proxy_connect_timeout 300;
                proxy_read_timeout 300;
                proxy_send_timeout 300;
                send_timeout 300;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }

        location /sourcebans/ {
                return 301 https://hwgaming.tf/sourcebans;
        }

        location /guardbans/ {
                return 301 https://hwgaming.tf/guardbans;
        }

        # pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;

                # With php-fpm (or other unix sockets):
                fastcgi_pass unix:/run/php/php7.4-fpm.sock;
                # With php-cgi (or other tcp sockets):
                # fastcgi_pass 127.0.0.1:9000;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
                deny all;
        }

        location /guardbans {
                proxy_pass http://localhost:3000/;
                proxy_http_version 1.1;
                proxy_connect_timeout 300;
                proxy_read_timeout 300;
                proxy_send_timeout 300;
                send_timeout 300;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }

        location /api {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header Host $http_host;
                proxy_http_version 1.1;
                proxy_set_header Connection "";
                proxy_pass http://localhost:3001/api;
        }

        location /_next/ {
                alias /var/www/html/hwgaming/guardbans/.next/;
        }
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/hwgaming.tf/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/hwgaming.tf/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {
    if ($host = www.hwgaming.tf) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80 ;
        listen [::]:80 ;
    server_name www.hwgaming.tf;
    return 404; # managed by Certbot


}


server {
    if ($host = hwgaming.tf) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


  server_name hwgaming.tf;
    listen 80;
    return 404; # managed by Certbot


}

My answer:


Remove the inappropriate 301 redirect from /sourcebans/ to /sourcebans.


View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.