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

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?

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.