xenforo server optimization

The site don't work. The Wordpress (different domains) on the same server work just fine but the forum doesn't. I can see it running Apache but not on nginx. I don't know what's wrong.
 
The site don't work. The Wordpress (different domains) on the same server work just fine but the forum doesn't. I can see it running Apache but not on nginx. I don't know what's wrong.
Are you using nginx frontend - apache backend or pure nginx or pure apache?
 
Actually, I can see on my browser an index.html I created for testing but not the xenforo index.php.

Any help would be appreciated.
 
Check you php fpm and nginx error logs. There must be there something about why is not working.

I assume you used official nginx configuration for forum?
 
OK, I commented out #fastcgi_pass 127.0.0.1:9000; in nginx config and all work fine now.

Page load time is less than a second! A HUGE improvement compared with the old server.
 
OK, I commented out #fastcgi_pass 127.0.0.1:9000; in nginx config and all work fine now.
I don't see how you could run PHP without a fastcgi_pass, is probably into an include file.
What did you to in your Nginx config to enable PHP?
 
I really have no idea...

This is my config.

Code:
server {
## listen for ipv4; this line is default and implied
    listen   80;
    server_name www.domain.com domain.com;
## rewrite www, try to keep your internal linking consistent
    #rewrite ^(.*)$ http://domain.com$request_uri? permanent; (for some reason, this doesn't work).
    root /var/www/vhosts/maindomain.com/domain.com.folder;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    location / {
        try_files $uri $uri/ /index.php?$uri&$args;
        index index.php index.html;
    }
   
    #protect the innocent
        location /(internal_data|library)/ {
        internal;
        }

    location ~ \.php$ {
                try_files $uri =404;
                #fastcgi_pass 127.0.0.1:9000;
                # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
                }
## Robots.txt to Xenforo
    location /robots.txt {
    rewrite ^(.*)$ http://domain.com/index.php?xfa-robots/index ;
    }

# 301 Permanent URL Redirects

location ~ /forum/$ {
    rewrite ^(.*)$ / permanent;
}
    # static resource routing - favicon.ico
    location ~* ^/favicon.ico {
        # Per RFC2616 - 1 year maximum expiry
            # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
            expires 1y;
            add_header Cache-Control public;

            # Some browsers still send conditional-GET requests if there's a
            # Last-Modified header or an ETag header even if they haven't
            # reached the expiry date sent in the Expires header.
            add_header Last-Modified "";
            add_header ETag "";
            break;
      }

    #gzip it, gzip it good
      gzip                    on;
      gzip_http_version      1.1;
      gzip_vary              on;
      gzip_min_length        1100;
      gzip_buffers            64 8k;
      gzip_comp_level        2;
      gzip_proxied            any;
      gzip_types              text/plain text/css application/x-javascript text/xml application/xml;
      gzip_disable "MSIE [1-6]\.(?!.*SV1)";
     
              #add some expires
        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires 1y;
        log_not_found off;
        }
       
        # deny access to apache .htaccess files
        location ~ /\.ht
        {
        deny all;
    }

}
 
I really have no idea...

This is my config.

Code:
server {
## listen for ipv4; this line is default and implied
    listen   80;
    server_name www.domain.com domain.com;
## rewrite www, try to keep your internal linking consistent
    #rewrite ^(.*)$ http://domain.com$request_uri? permanent; (for some reason, this doesn't work).
    root /var/www/vhosts/maindomain.com/domain.com.folder;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    location / {
        try_files $uri $uri/ /index.php?$uri&$args;
        index index.php index.html;
    }
  
    #protect the innocent
        location /(internal_data|library)/ {
        internal;
        }

    location ~ \.php$ {
                try_files $uri =404;
                #fastcgi_pass 127.0.0.1:9000;
                # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
                }
## Robots.txt to Xenforo
    location /robots.txt {
    rewrite ^(.*)$ http://domain.com/index.php?xfa-robots/index ;
    }

# 301 Permanent URL Redirects

location ~ /forum/$ {
    rewrite ^(.*)$ / permanent;
}
    # static resource routing - favicon.ico
    location ~* ^/favicon.ico {
        # Per RFC2616 - 1 year maximum expiry
            # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
            expires 1y;
            add_header Cache-Control public;

            # Some browsers still send conditional-GET requests if there's a
            # Last-Modified header or an ETag header even if they haven't
            # reached the expiry date sent in the Expires header.
            add_header Last-Modified "";
            add_header ETag "";
            break;
      }

    #gzip it, gzip it good
      gzip                    on;
      gzip_http_version      1.1;
      gzip_vary              on;
      gzip_min_length        1100;
      gzip_buffers            64 8k;
      gzip_comp_level        2;
      gzip_proxied            any;
      gzip_types              text/plain text/css application/x-javascript text/xml application/xml;
      gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    
              #add some expires
        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires 1y;
        log_not_found off;
        }
      
        # deny access to apache .htaccess files
        location ~ /\.ht
        {
        deny all;
    }

}
Where do you get that configuration?
 
Code:
server {
## listen for ipv4; this line is default and implied
listen 80;
server_name www.domain.com domain.com;
## rewrite www, try to keep your internal linking consistent
#rewrite ^(.*)$ http://domain.com$request_uri? permanent; (for some reason, this doesn't work).
root /var/www/vhosts/maindomain.com/domain.com.folder;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
You try to redirect www to non www but failed? I guess maybe because you had www.domain.com in server_name. Try this:

Code:
server {
listen 80;
server_name www.domain.com;
return 301 http://domain.com$request_uri;
}

Code:
 server {
listen 80;
server_name domain.com;
....
}

Code:
# 301 Permanent URL Redirects
location ~ /forum/$ {
rewrite ^(.*)$ / permanent;
}
# static resource routing - favicon.ico
location ~* ^/favicon.ico {
# Per RFC2616 - 1 year maximum expiry
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
expires 1y;
add_header Cache-Control public;

What is this favicon location function if you had:

Code:
#add some expires
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 1y;
log_not_found off;
}
 
I really have no idea...
You do use fastcgi_pass: fastcgi_pass unix:/var/run/php5-fpm.sock;
Where did you get that configuration? There are a lot of things wrong with it:
  1. no ip defined, security issue
  2. dual location allowed (domain.com and www.domain.com)
  3. index setting should not be into location
  4. location /(internal_data|library) does not work the way you want it, it does not work at all in fact
  5. you expose your site to hacking with your current php configuration
  6. fastcgi_index is used wrong and not needed in your case
  7. locations use regex
  8. you are using expensive rewrites, instead of 301's
  9. useless cache and incorrect setup of .ico file
  10. gzip settings should be into http section, some as useless as you use default values
  11. incorrect cache of static files (js, css, png, etc.)
  12. useless deny and expensive regex for .htaccess files, nginx does not understand it
I see that problem 2 and 9 were solved, you need to fix the other 10 issues.
Take a look at this basic configuration file, to get you started.
 
Last edited:
Where did you get that configuration? There are a lot of things wrong with it:
Wow, thanks!

OK, lets see...
  1. What ip should I use?
  2. Done.
  3. I got that from here: http://xenforo.com/help/friendly-urls/
  4. Done. Check now please.
  5. Any suggestions?
  6. Removed.
  7. Give me an example please.
  8. *
  9. Done.
  10. Removed.
  11. Check now please.
  12. Removed.
* I have about 200 hundreds rewrites. Can you give me what is the right way with these two as an example
Code:
location /oldpage/15/ {
  rewrite ^(.*)$ http://domain.com/newpage/341/ permanent;
}

location /oldpage/16/ {
  rewrite ^(.*)$ http://domain.com/newpage/812/ permanent;
}

This is the new configuration.
Code:
    server {
        listen 80;
        server_name www.domain.com;
        return 301 http://domain.com$request_uri;
    }

    server {
        listen 80;
        server_name domain.com;
        access_log /var/log/nginx/domain-access.log;
        error_log /var/log/nginx/domain-error.log;
        root /var/www/vhosts/maindomain.com/domain;
        index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$uri&$args;
    }
   
    location ~ ^/(internal_data|library)/ {
        internal;
        allow 127.0.0.1;
        deny all;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include         fastcgi_params;
    }
   
    location ~* \.(?:ico|css|gif|jpe?g|js|png|swf)$ {
        access_log             off;
        log_not_found          off;
        expires                1y;
    }

    ## Robots.txt to Xenforo
    location /robots.txt {
        rewrite ^(.*)$ http://domain.com/index.php?xfa-robots/index ;
    }

    ## 301 Permanent URL Redirects
.....

}
 
The new config is much better, yet you have major issues with it. :)
1) Note the listen where I added the server IP and the generic server_name. This does not work the other way around (domain.com redirected to www.domain.com).
Since you are redirecting everything from www.domain.com to domain.com, the proper way to do it is:
Code:
server {
        listen          192.168.1.2:80;
        server_name     _;
        return          301 http://domain.com$request_uri;
}
2) For (internal_data|library) regex, replace that with defined locations:
Code:
        location /data {
                location ~ \.html$ {
                        internal;
                }
                internal;
        }

        location /internal_data {
                location ~ \.(?:data|html|php)$ {
                        internal;
                }
                internal;
        }

        location /library {
                location ~ \.(?:default|html|php|txt|xml)$ {
                        internal;
                }
                internal;
        }
3) For .php location you are missing the fastcgi_split_path_info directive.
Without it, someone could hack easy your server. See my tutorial how to implement it.

4) For robots.txt, define an exact location and do a proper 301 redirect, not an expensive rewrite:
Code:
        location = /robots.txt {
                return 301 http://domain.com/index.php?xfa-robots/index;
        }
5) For your redirects, I would add them on a separate file so I don't clutter the main config.
Add this line at the bottom of your config file:
Code:
       include         localhost.redirect;
}
Then insert inside localhost.redirect proper redirects, not rewrites (note the exact locations used):
Code:
location /oldpage {
       location = /oldpage/15/ {
                return 301 http://domain.com/newpage/341/;
       }
       location = /oldpage/16/ {
                return 301 http://domain.com/newpage/812/;
       }
    ...
}
You are still missing A LOT of important elements. I want you to start reading the Nginx documentation I linked into my tutorial so you understand how to configure the server properly. STOP copying things from the Internet assuming they are right (because 90% of the time they are not), everything I pasted above is explained into Igor's documentation with nice examples.
 
Last edited:
OK, lets start from this:
Code:
server {
        listen          192.168.1.2:80;
        server_name     _;
        return          301 http://domain.com$request_uri;
}
This is not redirecting www.domain.com to domain.com but www.domain.com to a another domain hosted in a sub directory on the same server.

The only thing that works is:
Code:
    server {
        listen 80;
        server_name www.domain.com;
        return 301 http://domain.com$request_uri;
    }
Also
5) For your redirects, I would add them on a separate file so I don't clutter the main config.
I'm getting this when restarting nginx:
nginx: [emerg] "location" directive is not allowed here in /etc/nginx/sites-available/localhost.redirect:2
 
The message is clear, no? Sorry, I don't spoon feed.
Three hours before I write this post I was searching for a solution and when I realized that I can't make it work, I asked for help. If my message bothered, you could ignore it.

Anyway, thanks for the info.
 
Top Bottom