skip to Main Content

I’ve spent about 5 hours trying to get a simple nginx to apache2 reverse proxy set up and I get dozens and dozens of redirects. It’s a loop that I can’t find the cause of.

I used grep to recursively search for RewriteEngine and return 301 in my etc/ and my /var/www folders. So that includes a thorough search in my apache2 files, my nginx files, and all WordPress files. I can’t find anything.

I have no .htaccess file and all plugins are disabled. There’s one 301 redirect in my nginx site file. Here are my configs:

apache2.conf

# This file contains the main configuration directives for Apache2. It is used to
# configure the behavior of Apache2 server.

# Load necessary modules for MIME types
<IfModule mod_mime.c>
    # Configure MIME types for compressed files
    <FilesMatch ".php.br$">
        AddType "text/html" .br
        AddEncoding br .br
    </FilesMatch>
    <FilesMatch ".html.br$">
        AddType "text/html" .br
        AddEncoding br .br
    </FilesMatch>
    <FilesMatch ".js.br$">
        AddType "text/javascript" .br
        AddEncoding br .br
    </FilesMatch>
    <FilesMatch ".css.br$">
        AddType "text/css" .br
        AddEncoding br .br
    </FilesMatch>
    <FilesMatch ".svg.br$">
        AddType "image/svg+xml" .br
        AddEncoding br .br
    </FilesMatch>
</IfModule>

# Define runtime directory
Define APACHE_RUN_DIR /var/run/apache2$SUFFIX

# Disable memory-mapping for file delivery
EnableMMAP Off

# Enable sendfile kernel support for file delivery
EnableSendfile On

# Define process ID file
Define APACHE_PID_FILE /var/run/apache2/apache2.pid

# Set timeout for receives and sends
Timeout 100

# Enable KeepAlive for persistent connections
KeepAlive On
MaxKeepAliveRequests 2000
KeepAliveTimeout 6

# Define default user and group
User www-data
Group www-data

# Disable hostname lookups
HostnameLookups Off

# Set error log location and log level
ErrorLog ${APACHE_LOG_DIR}/error.log

# Include module configuration
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf

# Include list of ports to listen on
Include ports.conf

# Set default security model
<Directory />
    Options FollowSymLinks
    AllowOverride None
    Require all denied
</Directory>

<Directory /usr/share>
    Options -Indexes
    AllowOverride None
    Require all granted
</Directory>

<Directory /var/www/>
    Options -Indexes +FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

<Directory /var/www/html>
    Options -Indexes +FollowSymLinks 
    DirectoryIndex index.php
    AllowOverride All
    Require all granted
</Directory>

# Define access file name and prevent viewing of sensitive files
AccessFileName .htaccess
<FilesMatch "^.ht">
    Require all denied
</FilesMatch>

# Define log formats
LogFormat "%v:%p %h %l %u %t "%r" %>s %O "%{Referer}i" "%{User-Agent}i"" vhost_combined
LogFormat "%h %l %u %t "%r" %>s %O "%{Referer}i" "%{User-Agent}i"" combined
LogFormat "%h %l %u %t "%r" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

# Include generic configuration snippets
IncludeOptional conf-enabled/*.conf

# Include virtual host configurations
IncludeOptional sites-enabled/*.conf

# Disable ETag for better performance
FileETag None

000-default-le-ssl.conf

<VirtualHost *:8080>
    ServerAdmin [email protected]
    ServerName wiserrhinodesigns.com
    DocumentRoot /var/www/html

    # Logging
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # PHP-FPM via FastCGI
    <FilesMatch .php$>
        SetHandler "proxy:unix:/run/php/php8.3-fpm.sock|fcgi://localhost/"
    </FilesMatch>

    # Brotli Compression
    <IfModule mod_brotli.c>
        AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/xml text/css text/javascript application/javascript application/json application/x-font-ttf application/vnd.ms-fontobject image/x-icon
    </IfModule>

    # GZIP Compression
    <Location />
        SetOutputFilter DEFLATE
        BrowserMatch ^Mozilla/4 gzip-only-text/html
        BrowserMatch ^Mozilla/4.0[678] no-gzip
        BrowserMatch bMSI[E] !no-gzip !gzip-only-text/html
        SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png|zip|gz|rar|bz2|7z|xz)$ no-gzip dont-vary
        Header append Vary User-Agent env=!dont-vary
    </Location>
</VirtualHost>

Default (nginx – I originally had 301, but I changed it to 302 just to test things. Imagine it’s a 301…)

server {
    listen 80;
    listen [::]:80;
    server_name wiserrhinodesigns.com www.wiserrhinodesigns.com wiserrhino.com www.wiserrhino.com;

    # Redirect HTTP to HTTPS only if not already HTTPS
    if ($scheme != "https") {
        return 302 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name wiserrhinodesigns.com wiserrhino.com;

    ssl_certificate /etc/letsencrypt/live/wiserrhinodesigns.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/wiserrhinodesigns.com/privkey.pem;

    location / {
        proxy_pass http://wiserrhinodesigns.com:8080; # Assuming Apache is running on port 8080
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Can you help me with this? I’m basically a beginning developer and I can’t seem to solve this 301 error.

Here is an additional screenshots that may help. It’s of Dev tools:
Dev Tools
Dev Tools Details

  • I’ve grep searched for offending directives.
  • I’ve cleaned up my file system.
  • I’ve disabled all plugins.
  • I removed www to non-www DNS records to transfer the responsibility locally.
  • I’ve used ChatGPT to try to find and fix the error.
  • I’ve Googled and read and now my eyes are bleeding. (Same with YouTube)
  • I’ve spent 5 hours on a task everyone says only takes 10 minutes.
  • I’ve changed the listening port in Apache’s port.conf to listen to 8080 only. listening on a secure port has been commented out because I don’t think I need SSL on the Apache2 back-end.

2

Answers


  1. Chosen as BEST ANSWER

    The exact details of this is something I do not yet understand, but I followed the directions on the following page and things are working 100% now.

    Digital Ocean: Redirect loops


  2. I fixed this problem, but the truth is I’m not exactly sure how. So, I’m sorry , but I can’t contribute properly.

    However, I will remember your request, and try to make any further posts a lot more informative.

    Thank you for replying.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search