Question

Help needed with Nginx reverse proxy config...

Referencing article:

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

Thanks for the write-up, it was (for the most part) successful however I’ve gotten to a problematic point and cannot find a solution.

In step 11 describing the setup of static caching, the location directives change. Now, when my server config only uses the following location directive, proxying to Apache works well:

   location / {
        proxy_pass http://your_server_ip: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;
    }

However, the moment I change the location directive to "location ~ \.php$ {", nothing works. The website attempts to load but it’s mostly broken.

I’ve tried numerous iterations of the config you’ve described in step 11 but nothing works, unless I change the location directive back to "location / {".

I’m trying to setup FastCGI caching in reverse proxy mode but so far have hit a major brick-wall based on parsing of the config file.

Would really appreciate some help with this.


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.

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.

Hi there,

If you are proxying the connection to a PHP-FPM backend directly without going through Apache, it is not enough to only update "location ~ \.php$ {" part but you also need to change the proxy rules. Uou could use the following configuration:

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

Note that you need to change the 7.4 with your actual PHP version.

For setting up Nginx with PHP-FPM I could suggest this tutorial here:

https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-ubuntu-18-04

Let me know how it goes. Regards, Bobby