Question

Nginx as Reverse Proxy for Apache - too many redirects

Posted June 5, 2018 12.4k views
NginxApacheUbuntu 16.04

I followed this guide to have Nginx function as a forward proxy for apache.. However, when I attempt to allow Nginx to serve static files and pass php stuff off to Apache, I get an error about being redirected too many times.

My nginx server block for apache proxy-ing looks like this

server {
    listen 80;
    listen 443 ssl;
    server_name harmonherring.win www.harmonherring.win;
    root /var/www/harmonherring.win;
    index index.php index.html index.html;

    #ssl on;
    ssl_certificate /etc/letsencrypt/live/www.harmonherring.win/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.harmonherring.win/privkey.pem;

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

    location ~ \.php$ {
        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-Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://159.65.176.109:8080;
    }

    location ~ /\. {
        deny all;
    }
}

Any help would be much appreciated, thanks!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
2 answers

Hi,

try to specify all possible extensions and everything else ( non static files ) proxy_pass to Apache2 to handle it. Something like this:

server {
    listen 80;
    listen 443 ssl;
    server_name harmonherring.win www.harmonherring.win;
    root /var/www/harmonherring.win;
    index index.php index.html index.html;

    #ssl on;
    ssl_certificate /etc/letsencrypt/live/www.harmonherring.win/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.harmonherring.win/privkey.pem;

    location ~ /\. {
        deny all;
    }

    location ~* \.(bmp|gif|ico|jpeg|jpg|pict|png|svg|swf|tif)$ {
        try_files $uri =404;
    }

    location ~* \.(class|css|csv|doc|docx|ejs|eot|eps|jar|js|mid|midi|otf|pdf|pls|ppt|pptx|ps|svgz|tiff|ttf|txt|webp|woff|woff2|xls|xlsx)$ {
        try_files $uri =404;
    }

    location / {
        proxy_set_header Host $http_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-Host $http_host;
        proxy_set_header X-Forwarded-Proto $scheme;
        ### use localhost:8080 and firewall for public network to port 8080 to disable access from internet;
        proxy_pass http://127.0.0.1:8080;
    }
}

I have the same problem with Wordpress and is creating 21 redirects(301) and then the browser will show an error, I created tried to exclude the extensions but didn’t work. Any other suggestion?

Submit an Answer