Use of variables with an include statement (nginx)

You can use variables in config files to make life easier like :

But how do you use a variable with an include statement.

When you use Wordpress on Nginx with the W3 Total Cache plugin you need to include the nginx.conf file in the document root:

include /srv/www/example.com/public_html/nginx.conf

Basically you could create one config file for wordpress and include it in the config files in sites-available/ for all your wordpress sites. The one thing I haven't figured out is how to pass a variable to an include statement. The following statements don't work:

  • include $document_root.'nginx.conf'

  • include $documentroot$wpnginxconf (where $wpnginx_conf = nginx.conf)

One of the error messages I got is that it couldn't find the file /etc/nginx/$documentroot$wpnginxconf.

So, how can I solve this? Or is there a way around this? Or is this the one statement that I will have to put in the conf files for every wordpress site?

4 Replies

AFAIK you cannot use variables in nginx config file includes. This is because includes are processed before any variables are evaluated.

Google this bit of NginX awesomeness and you should find some good nginX.conf examples:

if (!-e $request_filename) {

rewrite ^.+/?(/wp-.*) $1 last;

rewrite ^.+/?(/.*.php)$ $1 last;

rewrite ^(.+)$ /index.php?q=$1 last;

}

I'm hosting about a dozen WordPress blogs and my NginX.conf doesn't have any includes or anything specific to WordPress besides those rewrites I just posted. My nginx.conf is 125 lines with comments. IMO, no need to make something so simple more complex by breaking it into pieces.

That said, unless you're a top-tier blog (ranked in the top 10,000 domains) I bet you don't even need this cache plugin because WordPress has its own cache and Linode+NginX is pretty darn fast.

For example, petsmartfeedback.com is ranked around 10,000 in Quantcast and they have only about 200,000 visits/month. Are you in the top 10k?

If that petsmartfeedback site ran WordPress with a few plugins, Linode plus NginX, I bet the CPU would be easily under 50% all day long with no cache plugin. I think you will run out of bandwidth before you run out of Linode CPU power running WordPress on the Linode 512.

Not trying to belittle your site, but I hope you really need that plugin before you go tearing your hair out like I see a lot of people doing with WordPress. You did the right thing moving to Linode + Nginx ;-) I'm just guessing but now I think you can relax about speed. If you're new to Linode–just see what your new Linode can do without this cache plugin, you should be pleasantly surprised.

In my experience, if a site is slow after moving to Linode, there's probably a serious flaw somewhere and the cache plugin only makes that harder to pinpoint and correct--the slowness is still there, but now only intermittently when the cache is reset.

Before installing a WordPress cache plugin I would try a simpler, more elegant solution--check your my.cnf settings. Install "mysqltuner" and follow the recommendations. Your experience may be different, but when I install MySQL with Fedora, by default the caching is not the best and you can see a huge improvement with very small my.cnf adjustments.

200,000 visits/month! Not yet, not even close :lol:

W3TC plugin has its uses. When I first started blogging and adding plugins the page load time increased to 8 seconds. I managed to get it under 2 seconds by removing plugins, replacing some other plugins, and following advice given by YSlow. I just installed W3TC (still on shared hosting) and page load times (using http://whichloadsfaster.com/) decreased by 30%. That alone justifies using it. Moving to my Linode and some more tweaking might reduce it even further (to less than a second?).

Will have a look at mysqltuner. Thanx for the tip.

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