Question

Nginx as Reverse Proxy for Apache - too many redirects

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!


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Accepted Answer

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;
    }
}

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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?