Help with rewrites and caching issues on nginx

I have a LEMP stack running on my site. The main site is a basic CRUD site, with pages of information, using CodeIgniter as the framework (may change this in future as it's getting old, but it works perfectly fine for now). I also have a Q&A section (similar to a forum) which is more dynamic. I also have some custom editing tools for the main site which are under /admin.

I tried to set up caching in nginx to ease the load, but it's not actually working how it's supposed to. I set it so that the Q&A section wasn't cached and everything else was, but those setting are actually not affecting what is cached at all. It seems like setting up a PHP session prevents nginx caching stuff. A couple of times there have been errors in the Q&A section and those URLs ended up getting cached because the page just displayed an error message and did not start any sessions. Similarly, the /admin area was not included in the caching rules but is still not cached anyway.

So anyway here is my site config:

server {
  listen 80;
  server_name  example.com;
  access_log   /var/log/nginx/example.com/access.log custom;
  error_log    /var/log/nginx/example.com/error.log;
  root         /srv/www/example.com/web;

  limit_conn conn_limit_per_ip 10;
  limit_req zone=req_limit_per_ip burst=10 nodelay;

  index index.php index.html;

  set $no_cache "";

  # QA section
  location /qa/ {
    set $no_cache "1";
    if (!-e $request_filename) {
      rewrite ^/qa/(.*)$ /qa/index.php?qa-rewrite=$1 last;
    }
  }

  # CodeIgniter rewriting
  location / {
    try_files $uri /index.php?$request_uri;
  }

  location ~ \.php$ {
    try_files $uri =404;

    fastcgi_cache one;
    fastcgi_cache_key $scheme$host$request_uri;
    fastcgi_cache_valid  200 302 304 20m;
    fastcgi_cache_valid  301 12h;

    fastcgi_cache_bypass $no_cache;
    fastcgi_no_cache $no_cache;

    include /etc/nginx/fastcgi_params;
    fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /srv/www/example.com/web$fastcgi_script_name;
    fastcgi_param HTTPS off;
  }
}

What am I doing wrong?

0 Replies

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct