MyBB Community Forums

Full Version: nginx recommendations!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
(2015-06-18, 09:57 AM)Euan T Wrote: [ -> ]
(2015-06-18, 07:48 AM)blindpet Wrote: [ -> ]
(2015-06-18, 06:38 AM)Euan T Wrote: [ -> ]
(2015-06-17, 11:06 PM)blindpet Wrote: [ -> ]Does fastcgi caching with nginx mess up mybb always?

Not if you configure it correctly. You need to have it only cache for guests or else new posts and such won't behave quite as expected. The same issue exists in all other forum software I know of.

I will be running WordPress on the same VPS and Wordpress benefits from fast cgi caching, is it easier to separate the vhosts and set up the fast cgi caching separately that way? Do you happen to have a config for fast cgi caching for guests only?

I would separate the vhosts if you're using subdomains, otherwise I'd just a single vhost. I've actually been meaning to write a guide about using fastcgi caching for a while now, so I'll get my config separated out and post it. It hasn't had much of a production test yet, as I've only been testing locally.

That's exactly the phase I am in now, testing environment only, my combined wordpress and mybb vhost config is as follows. Enabling cgi caching for wordpress only would be excellent, so would making separate log files, do you know if you can do that per location?

#This configuration has the following structure
#Wordpress is in the /var/www/htpcguides.com folder
#The myBB forum is in /var/www/htpcguides.com/forum
server {
        server_name default_server;
        listen 80;
        access_log   /var/log/nginx/htpcguides.com.access.log;
        error_log    /var/log/nginx/htpcguides.com.error.log;

        root /var/www/htpcguides.com/;
        index index.php index.htm index.html;

        location / {
	try_files $uri $uri/ /index.php?$args;
	port_in_redirect off;
	}

location /forum {
    server_tokens off;
    index index.html index.htm index.php;
    rewrite ^/forum-([0-9]+)\.html$ /forumdisplay.php?fid=$1;
    rewrite ^/forum-([0-9]+)-page-([0-9]+)\.html$ /forumdisplay.php?fid=$1&page=$2;
    rewrite ^/thread-([0-9]+)\.html$ /showthread.php?tid=$1;
    rewrite ^/thread-([0-9]+)-page-([0-9]+)\.html$ /showthread.php?tid=$1&page=$2;
    rewrite ^/thread-([0-9]+)-lastpost\.html$ /showthread.php?tid=$1&action=lastpost;
    rewrite ^/thread-([0-9]+)-nextnewest\.html$ /showthread.php?tid=$1&action=nextnewest;
    rewrite ^/thread-([0-9]+)-nextoldest\.html$ /showthread.php?tid=$1&action=nextoldest;
    rewrite ^/thread-([0-9]+)-newpost\.html$ /showthread.php?tid=$1&action=newpost;
    rewrite ^/thread-([0-9]+)-post-([0-9]+)\.html$ /showthread.php?tid=$1&pid=$2;
    rewrite ^/post-([0-9]+)\.html$ /showthread.php?pid=$1;
    rewrite ^/announcement-([0-9]+)\.html$ /announcements.php?aid=$1;
    rewrite ^/user-([0-9]+)\.html$ /member.php?action=profile&uid=$1;
    rewrite ^/calendar-([0-9]+)\.html$ /calendar.php?calendar=$1;
    rewrite ^/calendar-([0-9]+)-year-([0-9]+)\.html$ /calendar.php?action=yearview&calendar=$1&year=$2;
    rewrite ^/calendar-([0-9]+)-year-([0-9]+)-month-([0-9]+)\.html$ /calendar.php?calendar=$1&year=$2&month=$3;
    rewrite ^/calendar-([0-9]+)-year-([0-9]+)-month-([0-9]+)-day-([0-9]+)\.html$ /calendar.php?action=dayview&calendar=$1&year=$2&month=$3&day=$4;
    rewrite ^/calendar-([0-9]+)-week-(n?[0-9]+)\.html$ /calendar.php?action=weekview&calendar=$1&week=$2;
    rewrite ^/event-([0-9]+)\.html$ /calendar.php?action=event&eid=$1;
    rewrite ^/archive/index.php/forum-([0-9]+)\.html$ /forum/archive/index.php?forum-$1.html;
    rewrite ^/archive/index.php/thread-([0-9]+)\.html$ /forum/archive/index.php?thread-$1.html;

    }
    
location ~* \.(jpg|jpeg|gif|png|css|ico|js|xml)$ {
    access_log        off;
    log_not_found     off;
    expires           360d;
        }

location ~ ^/(bin|SQL|\.ht)/ {
                deny all;
       }
	   
##This passes the php to the socket for processing for both Wordpress and myBB thanks to the bottom line
location ~ \.php$ {
		try_files $uri =404;
		include fastcgi_params;
		fastcgi_pass unix:/var/run/php5-fpm.sock;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
##Block access to ht access
#location ~ /\.ht {
#        deny all;
#        }
location /phpmyadmin {
        root /usr/share/;
        index index.php index.html index.htm;
        location ~ ^/phpmyadmin/(.+\.php)$ {
            try_files $uri =404;
            root /usr/share/;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include /etc/nginx/fastcgi_params;
        }
        location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
            root /usr/share/;
        }
    }
    location /phpMyadmin {
        rewrite ^/* /phpmyadmin last;
    }

}
(2015-06-18, 10:02 AM)blindpet Wrote: [ -> ]
(2015-06-18, 09:57 AM)Euan T Wrote: [ -> ]
(2015-06-18, 07:48 AM)blindpet Wrote: [ -> ]
(2015-06-18, 06:38 AM)Euan T Wrote: [ -> ]
(2015-06-17, 11:06 PM)blindpet Wrote: [ -> ]Does fastcgi caching with nginx mess up mybb always?

Not if you configure it correctly. You need to have it only cache for guests or else new posts and such won't behave quite as expected. The same issue exists in all other forum software I know of.

I will be running WordPress on the same VPS and Wordpress benefits from fast cgi caching, is it easier to separate the vhosts and set up the fast cgi caching separately that way? Do you happen to have a config for fast cgi caching for guests only?

I would separate the vhosts if you're using subdomains, otherwise I'd just a single vhost. I've actually been meaning to write a guide about using fastcgi caching for a while now, so I'll get my config separated out and post it. It hasn't had much of a production test yet, as I've only been testing locally.

That's exactly the phase I am in now, testing environment only, my combined wordpress and mybb vhost config is as follows. Enabling cgi caching for wordpress only would be excellent, so would making separate log files, do you know if you can do that per location?

#This configuration has the following structure
#Wordpress is in the /var/www/htpcguides.com folder
#The myBB forum is in /var/www/htpcguides.com/forum
server {
        server_name default_server;
        listen 80;
        access_log   /var/log/nginx/htpcguides.com.access.log;
        error_log    /var/log/nginx/htpcguides.com.error.log;

        root /var/www/htpcguides.com/;
        index index.php index.htm index.html;

        location / {
	try_files $uri $uri/ /index.php?$args;
	port_in_redirect off;
	}

location /forum {
    server_tokens off;
    index index.html index.htm index.php;
    rewrite ^/forum-([0-9]+)\.html$ /forumdisplay.php?fid=$1;
    rewrite ^/forum-([0-9]+)-page-([0-9]+)\.html$ /forumdisplay.php?fid=$1&page=$2;
    rewrite ^/thread-([0-9]+)\.html$ /showthread.php?tid=$1;
    rewrite ^/thread-([0-9]+)-page-([0-9]+)\.html$ /showthread.php?tid=$1&page=$2;
    rewrite ^/thread-([0-9]+)-lastpost\.html$ /showthread.php?tid=$1&action=lastpost;
    rewrite ^/thread-([0-9]+)-nextnewest\.html$ /showthread.php?tid=$1&action=nextnewest;
    rewrite ^/thread-([0-9]+)-nextoldest\.html$ /showthread.php?tid=$1&action=nextoldest;
    rewrite ^/thread-([0-9]+)-newpost\.html$ /showthread.php?tid=$1&action=newpost;
    rewrite ^/thread-([0-9]+)-post-([0-9]+)\.html$ /showthread.php?tid=$1&pid=$2;
    rewrite ^/post-([0-9]+)\.html$ /showthread.php?pid=$1;
    rewrite ^/announcement-([0-9]+)\.html$ /announcements.php?aid=$1;
    rewrite ^/user-([0-9]+)\.html$ /member.php?action=profile&uid=$1;
    rewrite ^/calendar-([0-9]+)\.html$ /calendar.php?calendar=$1;
    rewrite ^/calendar-([0-9]+)-year-([0-9]+)\.html$ /calendar.php?action=yearview&calendar=$1&year=$2;
    rewrite ^/calendar-([0-9]+)-year-([0-9]+)-month-([0-9]+)\.html$ /calendar.php?calendar=$1&year=$2&month=$3;
    rewrite ^/calendar-([0-9]+)-year-([0-9]+)-month-([0-9]+)-day-([0-9]+)\.html$ /calendar.php?action=dayview&calendar=$1&year=$2&month=$3&day=$4;
    rewrite ^/calendar-([0-9]+)-week-(n?[0-9]+)\.html$ /calendar.php?action=weekview&calendar=$1&week=$2;
    rewrite ^/event-([0-9]+)\.html$ /calendar.php?action=event&eid=$1;
    rewrite ^/archive/index.php/forum-([0-9]+)\.html$ /forum/archive/index.php?forum-$1.html;
    rewrite ^/archive/index.php/thread-([0-9]+)\.html$ /forum/archive/index.php?thread-$1.html;

    }
    
location ~* \.(jpg|jpeg|gif|png|css|ico|js|xml)$ {
    access_log        off;
    log_not_found     off;
    expires           360d;
        }

location ~ ^/(bin|SQL|\.ht)/ {
                deny all;
       }
	   
##This passes the php to the socket for processing for both Wordpress and myBB thanks to the bottom line
location ~ \.php$ {
		try_files $uri =404;
		include fastcgi_params;
		fastcgi_pass unix:/var/run/php5-fpm.sock;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
##Block access to ht access
#location ~ /\.ht {
#        deny all;
#        }
location /phpmyadmin {
        root /usr/share/;
        index index.php index.html index.htm;
        location ~ ^/phpmyadmin/(.+\.php)$ {
            try_files $uri =404;
            root /usr/share/;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include /etc/nginx/fastcgi_params;
        }
        location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
            root /usr/share/;
        }
    }
    location /phpMyadmin {
        rewrite ^/* /phpmyadmin last;
    }

}

It should be possible, yeah. I've never tried it though, so I could be wrong. You can definitely change log files per location.
Just tried adding a separate log file under location /forum and it does create the log files but writes nothing to them. I'm afraid it'll have to be a reverse proxy with separate server blocks at which point I think it makes sense to just use separate vhosts.

Do let me know if you work the caching out as that is more important to me than the log files currently
Ok, here's my config, not 100% it's right but it appears to work. Output is only cached for guests, and is cached for 10 minutes in /etc/nginx/cache/.

http {
	# The rest of your HTTP level configuration...

	# FastCGI Cache
	fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=fastcgicache:10m inactive=10m max_size=100m;
	fastcgi_cache_lock on;
	fastcgi_cache_use_stale error timeout invalid_header updating http_500;
	fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
}

server {
	# Server level config - listen port, domain, etc.

	fastcgi_cache_key "$host$cookie_sid$request_method$uri$args";

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

		fastcgi_split_path_info ^(.+\.php)(/.+)$;

		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

		fastcgi_index index.php;
		include fastcgi.conf;

		fastcgi_pass php-fpm;

		add_header X-Cache $upstream_cache_status;
		fastcgi_cache fastcgicache;
		fastcgi_cache_bypass $cookie_mybbuser $cookie_adminsid;
		fastcgi_no_cache $cookie_mybbuser $cookie_adminsid;
		fastcgi_cache_valid  200 302 15m;
		fastcgi_cache_valid  301 1d;
		fastcgi_cache_valid  404 5m;
		fastcgi_cache_valid  403 5m;
		fastcgi_cache_valid  any 15m;
	}
}

As I said, this appears to work on my server. A header is added to responses called "X-Cache", which will be either "HIT", "MISS" or "BYPASS" - indicating if the response came from the cache or not. Guest requests should be cached, but user ones shouldn't be. I haven't load tested this or really tested it at all, but it's live here: http://lothlorien.mybbstuff.com/index.php
(2015-06-19, 10:56 PM)Euan T Wrote: [ -> ]Ok, here's my config, not 100% it's right but it appears to work. Output is only cached for guests, and is cached for 10 minutes in /etc/nginx/cache/.

http {
 # The rest of your HTTP level configuration...

 # FastCGI Cache
 fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=fastcgicache:10m inactive=10m max_size=100m;
 fastcgi_cache_lock on;
 fastcgi_cache_use_stale error timeout invalid_header updating http_500;
 fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
}

server {
 # Server level config - listen port, domain, etc.

 fastcgi_cache_key "$host$cookie_sid$request_method$uri$args";

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

 fastcgi_split_path_info ^(.+\.php)(/.+)$;

 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

 fastcgi_index index.php;
 include fastcgi.conf;

 fastcgi_pass php-fpm;

 add_header X-Cache $upstream_cache_status;
 fastcgi_cache fastcgicache;
 fastcgi_cache_bypass $cookie_mybbuser $cookie_adminsid;
 fastcgi_no_cache $cookie_mybbuser $cookie_adminsid;
 fastcgi_cache_valid  200 302 15m;
 fastcgi_cache_valid  301 1d;
 fastcgi_cache_valid  404 5m;
 fastcgi_cache_valid  403 5m;
 fastcgi_cache_valid  any 15m;
 }
}

As I said, this appears to work on my server. A header is added to responses called "X-Cache", which will be either "HIT", "MISS" or "BYPASS" - indicating if the response came from the cache or not. Guest requests should be cached, but user ones shouldn't be. I haven't load tested this or really tested it at all, but it's live here: http://lothlorien.mybbstuff.com/index.php

Thank you! I will try this with a reverse proxy and separate vhosts which should make separate fastcgi caches easier
Just set something similar up for Wordpress and the magic lines seem to be, not sure how much the speed benefit is for fastcgi caching, maybe I will do some load tests at some point.

Do you know the benefit of split php cgi?

fastcgi_cache_bypass $cookie_mybbuser $cookie_adminsid;
fastcgi_no_cache $cookie_mybbuser $cookie_adminsid;
Quote:Do you know the benefit of split php cgi?

I'm afraid I don't understand what you mean?

The two lines you posted prevent fastcgi caching for logged in users, as otherwise they wouldn't see new posts and such until after the cache expired.
Pages: 1 2