0

I'm trying to use Nginx as a caching reverse proxy for Apache running a PHP site. It's kind of working but the problem comes when trying to rewrite the URL to the Apache setup, it doesn't work as I would have hoped. Here's the setup.

I have a Joomla based PHP site running on Apache, ports 8080 (http) and 44343 (https) - this works fine and there's no problem here. I have removed the Joomla .htaccess file as I assume Nginx will do the rewriting to index.php itself and take care of caching the necessary PHP files.

Joomla rewrites the URL like this example.com/page1 => example.com/index.php/page1

I have Nginx listening on ports 80, 443. Below is the site config

server {
   listen 80;
   listen [::]:80;

   server_name    www.example.com
   return         301 https://$server_name$request_uri;
}
server {
    client_max_body_size 100M;
    autoindex off;

    listen 443 ssl;
    listen [::]:443 ssl;

    server_name www.example.com;
    server_name_in_redirect off;

    ssl_certificate /example.com/ssl.crt;
    ssl_certificate_key /example.com/ssl.key;

    root /var/www/site;

    index index.php index.html index.htm default.html default.htm;

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

    # Reverse Proxy and Proxy Cache Configuration
    location ~ \.php {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass https://127.0.0.1:44343;

        # Cache configuration
        proxy_cache reverse_cache;
        proxy_cache_valid 3s;
        proxy_no_cache $cookie_PHPSESSID;
        proxy_cache_bypass $cookie_PHPSESSID;
        proxy_cache_key "$scheme$host$request_uri";
        add_header X-Cache $upstream_cache_status;
    }

    ...
}

So this loads the site but no matter what page I go to, it always loads the homepage. It doesn't appear to be sending the $uri to index.php as expected in the try_files part.

The only way I can make this work is by modifying it in the following way:

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

    # Reverse Proxy and Proxy Cache Configuration
    location / {
        ...
    }

This just sends the entire request to Apache, and I have to bring back the .htaccess file which allows Apache to do the rewriting.

The site loads as expected, but the problem is that no PHP files are cached (as checked in /var/cache). So Nginx will have to do the rewriting in order to know which PHP files are cached for which request.

Any idea how I can do this?

0

Just a suggestion, but your upstream app may also require the query string together with the original URI. You could try modifying your default URI as either:

try_files $uri $uri/ /index.php$request_uri;

or:

try_files $uri $uri/ /index.php$uri$is_args$args;

The nginx variables are documented here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.