NGINX 301 Infinite Redirect (Mattermost Bitnami AWS)

I’m trying to set up Mattermost on Bitnami AWS with Cloudflare being my DNS record config. I have an application load balancer that handles the redirects. At first, I was getting a bad gateway error but I fixed that. Once that was fixed, I began getting an ERR_TOO_MANY_REDIRECTS error and I’ve been stuck on this issue for 3 weeks now.

When I run curl http://localhost in PowerShell I get the following:

<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>

My /opt/bitnami/nginx/conf/nginx.conf file:

(I read a few posts on stackoverflow and saw that changing absolute_redirect on; to aboslute_redirect off; fixed the issue for a few, so I tried it but no luck.)

  GNU nano 5.4                                                                                                                                                                                         /opt/bitnami/nginx/conf/nginx.conf                                                                                                                                                                                                  # Based on https://www.nginx.com/resources/wiki/start/topics/examples/full/#nginx-conf
user              daemon daemon;  ## Default: nobody

worker_processes  auto;
error_log         "/opt/bitnami/nginx/logs/error.log";
pid               "/opt/bitnami/nginx/tmp/nginx.pid";

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format    main '$remote_addr - $remote_user [$time_local] '
                       '"$request" $status $body_bytes_sent "$http_referer" '
                       '"$http_user_agent" "$http_x_forwarded_for"';
    access_log    "/opt/bitnami/nginx/logs/access.log" main;
    add_header    X-Frame-Options SAMEORIGIN;

    client_body_temp_path  "/opt/bitnami/nginx/tmp/client_body" 1 2;
    proxy_temp_path        "/opt/bitnami/nginx/tmp/proxy" 1 2;
    fastcgi_temp_path      "/opt/bitnami/nginx/tmp/fastcgi" 1 2;
    scgi_temp_path         "/opt/bitnami/nginx/tmp/scgi" 1 2;
    uwsgi_temp_path        "/opt/bitnami/nginx/tmp/uwsgi" 1 2;

    sendfile           on;
    tcp_nopush         on;
    tcp_nodelay        off;
    gzip               on;
    gzip_http_version  1.0;
    gzip_comp_level    2;
    gzip_proxied       any;
    gzip_types         text/plain text/css application/javascript text/xml application/xml+rss;
    keepalive_timeout  65;
    ssl_protocols      TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_ciphers        HIGH:!aNULL:!MD5;
    client_max_body_size 80M;
    server_tokens off;

    absolute_redirect  off;
    port_in_redirect   on;

    include  "/opt/bitnami/nginx/conf/server_blocks/*.conf";

    # HTTP Server
    server {
        # Port to listen on, can also be set in IP:PORT format
        listen  80;

        include  "/opt/bitnami/nginx/conf/bitnami/*.conf";

        location /status {
            stub_status on;
            access_log   off;
            allow 127.0.0.1;
            deny all;
        }
    }
}

My /opt/bitnami/nginx/conf/server_blocks/mattermost-server-block.conf file:

server {
    listen 80 default_server;
    server_name _;
    root /opt/bitnami/mattermost;
    return 301 https://$host$request_uri;
}

If anyone is able to guide me in the right direction or assist me with this issue, then thank you in advance.

This issue is, in your mattermost setup on Bitnami AWS with cloudflare and an application load balancer, suggests a possible misconfiguration causing a redirect loop. In your mattermost-server-block.conf file the return 301 https://$host$request_uri line is causing all HTTP traffic to be redirected to HTTPS, which might be conflicting with similar redirects handled by cloudflare or the load balancer.

Check that the redirects are not being duplicated across cloudflare, the load balancer and the Nginx configuration. You might need to disable HTTPS redirection at one of these points in the Nginx configuration to stop the infinite loop.

Hey, so Cloudflare Always Use HTTPS is disabled. My AWS ALB is redirecting HTTP to HTTPS and so is my NGINX web server. The issue is when I disable the return 301 https://$host$request_uri; my website takes me to an NGINX page in which it says “NGINX is successfully installed but needs further configuration”. Are you able to tell me how to do what you’re suggesting?

You can modify the server block to handle HTTPS redirection.
Like this.

server {
    listen 80;
    server_name your_domain.com;

    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;
    server_name your_domain.com;

    # SSL configuration goes here
    # Your other configurations
}

This setup checks HTTP traffic redirects to HTTPS while handling SSL connections properly. After making changes, check the redirection using a tool like https://redirectchecker.com/, to verify the correct setup and eliminate potential redirection loops.