Help needed with Nginx reverse proxy config...

Posted July 24, 2021 127 views
NginxApacheUbuntu 20.04

Referencing article:

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.

edited by bobbyiliev

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.

Submit an Answer
1 answer

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:

Let me know how it goes.

by Justin Ellingwood
by Mark Drake
This tutorial details the process for installing and configuring the components that constitute a LEMP stack on an Ubuntu 18.04 server, including Nginx, MySQL, and PHP. It also includes instructions for testing that these components can communicate effectively and serve your content correctly.