Search code examples
apachenginxserverdebianreverse-proxy

Nginx reverse proxy to Apache2 - not working - too many redirects


at the beginning, I want to apologize, but I am a newbie with Nginx.

I have a VPS where I have some PHP/Symfony projects (hosted in Apache2). Now I need to add a new application (in Vue especially NuxtJS) to the VPS.

So I decided to setup Nginx with reverse proxy to Apache2. My idea was that on Nginx could run the NuxtJS app and the PHP apps could still be on Apache2.

I followed this tutorial: https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-as-a-web-server-and-reverse-proxy-for-apache-on-one-ubuntu-18-04-server

  • Firstly I changed /etc/apache2/ports.conf file. Listen 80 to Listen 8080 and remove 443 SSL options. In this file is only Listen 8080 now.
  • Then I changed configs in /etc/apache/sites-available. I edited my existing configs for PHP projects as:
  1. remove SSL configs
  2. edit www.domain.com.conf and domain.com.conf -> rewrite *80 to *8080
  3. sudo a2dissite on domains in apache2
  • Next I installed Nginx, uncomment gzip options in /etc/nginx/nginx.conf and add proxy_cache_path /var/cache levels=1:2 keys_zone=reverse_cache:60m inactive=90m max_size=1000m;
  • After that, I removed a default file in /etc/nginx/sites-available and created an apache file in the same directory.
  • Linked an apache file with ln -s apache /etc/nginx/sites-enabled.
  • Reject direct access to Apache using sudo iptables -I INPUT -p tcp --dport 8080 ! -s your_server_ip -j REJECT --reject-with tcp-reset

Nginx config file Apache looks like this:

server {
    server_name my_domain.cz www.my_domain.cz;
    root /var/www/my_domain/web;
    index index.php app.php index.htm index.html;

    location / {
        try_files $uri $uri/ /app.php;
    }

    location ~ \.php$ {
        proxy_pass http://localhost: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;
    }

    location ~ /\.ht {
        deny all;
    }   

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/my_domain.cz/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/my_domain.cz/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 {
    server_name adminer.my_domain.cz www.adminer.my_domain.cz;
    root /var/www/adminer;
    index index.php index.htm index.html;

    location / {
        try_files $uri $uri/ /index.php;
    }

    location ~ \.php$ {
        proxy_pass http://localhost: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;
    }

    location ~ /\.ht {
        deny all;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/adminer.my_domain.cz/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/adminer.my_domain.cz/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


}

In proxy_pass option I tried localhost or 127.0.0.1 or my public IP of the server. No one of these options works.

Nginx running (sudo nginx -t is OK), Apache2 too. When I am trying to access my website I got ERR_TOO_MANY_REDIRECTS. I am desperate. None of the advice that I found worked.


Solution

  • The ERR_TOO_MANY_REDIRECTS error basically means that you have a redirection loop. Since you are reconfiguring your server there can be several causes of the loop. First, just make sure to delete all cookies and cache in your browser and also, if any, caches on the server.

    If that is not solving the loop, it might be caused by a redirect to https in a .htaccess or apache config file on your apache php server.

    Most servers these days are configured to redirect all traffic from http to https. In the .htaccess file you might find something like this:

    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} 
    

    Basically, this means that all requests which are not https, will be redirected to https.

    Since you have reconfigured the apache server to http and all requests from the Nginx reverse proxy will be http, the server will send a redirect request (301) to the browser. The browser will send a https request to Nginx, which forwards it as a http request to Apache, which sends a redirect to https to the browser. This will be an infinite loop. The browser recognizes this loop and will throw the ERR_TOO_MANY_REDIRECTS.

    Deleting the redirect to https in a .htaccess or the apache virtual host config may solve your problem.