Apache redirect config

I have issue configuring my apache server to redirect properly. Below is what my config look like:

<VirtualHost *:80>
    ServerName domain

    default config...
</VirtualHost>

<VirtualHost *:80>
    ServerName www.domain

    { in this www host, I'm forwarding the traffic to inner flask server }
    ProxyPreserveHost On
    ProxyPass / http://flask-server
    ProxyPassReverse / http://flask-server
</VirtualHost>

it works as expected when I havent installed https, since I used letsencrypt to get https, I have been battling with redirection error as the server enter infinite redirect loop. I have used .htaccess to write redirection config, it doesnt work. I have used this RedirectMatch permanent ^/(.*)$ https://domain/$1 nothing change, still the improper redirect error.

i am not quite familier with apache , why dont u try nginx its qite stright forword there .

# Redirect HTTP to HTTPS
server {
    listen 80;
    server_name domain www.domain;

    # Redirect all HTTP requests to HTTPS
    return 301 https://$host$request_uri;
}

# HTTPS configuration
server {
    listen 443 ssl;
    server_name domain www.domain;

    # SSL settings for Nginx (Let’s Encrypt certificates)
    ssl_certificate /etc/letsencrypt/live/domain/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/domain/chain.pem;

    # Proxy settings for Flask (running HTTPS)
    location / {
        proxy_pass https://flask-server;  # Use https since Flask is running over HTTPS
        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;

        # Optional: Disable caching for dynamic requests
        proxy_cache_bypass $http_upgrade;

        # Add this line to ignore SSL certificate verification when proxying
        proxy_ssl_verify off;
    }
}

Thanks boss. I’m not familiar with nginx, I will use this as instructed. Please, provide a course on nginx. It will help a lot before I go for my sysadmin course.

1 Like