Set Up Nginx Reverse Proxy to Apache2 with HTTPS


Install SSL/TLS Certificates
Use a trusted SSL certificate. You can:

Edit Nginx Configuration
Update the Nginx server block (e.g., /etc/nginx/sites-available/default) to handle HTTPS and proxy to Apache

server {
    listen 443 ssl;
    server_name yourdomain.com;

    ssl_certificate /etc/nginx/ssl/nginx-selfsigned.crt;
    ssl_certificate_key /etc/nginx/ssl/nginx-selfsigned.key;

    location / {
        proxy_pass http://127.0.0.1:8080; # Apache backend
        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;
    }

    location ~* ^/wp-content/.*\.(?:png|jpg|jpeg|gif|css|js|ico|woff|ttf|svg|eot)$ {
        root /var/www/html/wordpress/sharedcrumbs;
        try_files $uri @proxy;
    }

    location @proxy {
        proxy_pass http://127.0.0.1:8080; # Apache backend
        proxy_set_header Host $host;
    }
}

server {
    listen 80;
    server_name yourdomain.com;

    return 301 https://$host$request_uri;
}
  • Replace yourdomain.com with your domain name.
  • proxy_pass points to Apache, which will run on port 8080.

Test and Reload Nginx

sudo nginx -t sudo systemctl reload nginx

Configure Apache to Listen on a Non-Standard Port (e.g., 8080)

Edit Apache Ports Configuration
Listen 8080
Edit Apache Virtual Host Configuration
<VirtualHost *:8080>
    ServerName yourdomain.com
    DocumentRoot /var/www/html

    <Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Restart Apache
sudo systemctl restart apache2

Additional Configuration

Forward HTTPS Info to Apache
Add the following to the Apache configuration to handle the HTTPS proxy:

SetEnvIf X-Forwarded-Proto “https” HTTPS=on

<VirtualHost *:8080>
    ServerName yourdomain.com
    DocumentRoot /var/www/html

    <Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    # Pass HTTPS information from Nginx
    SetEnvIf X-Forwarded-Proto "https" HTTPS=on

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Optional: Adjust Firewall Rules
Ensure ports 80, 443, and 8080 are open:
sudo ufw allow 80
sudo ufw allow 443
sudo ufw allow 8080