Monday, September 26, 2011

Setup Caching and Proxy with Nginx in Centos/Fedora P2


user  nobody nobody;
worker_processes     4;
worker_rlimit_nofile 8192;
pid /var/run/nginx.pid;
events {
  worker_connections 2048;
}
 http {
    include       mime.types;
    default_type  application/octet-stream;
     log_format main ‘$remote_addr – $remote_user [$time_local] ‘
                    ‘”$request” $status  $body_bytes_sent “$http_referer” ‘
                    ‘”$http_user_agent” “$http_x_forwarded_for”‘;
    access_log  logs/nginx_access.log  main;
    error_log  logs/nginx_error.log debug;
    server_names_hash_bucket_size 64;
    sendfile on;
    tcp_nopush     on;
    tcp_nodelay    off;
    keepalive_timeout  30;
     gzip  on;
    gzip_comp_level 9;
    gzip_proxied any;
     proxy_buffering on;
    proxy_cache_path /usr/local/nginx/proxy levels=1:2 keys_zone=one:15m inactive=7d max_size=1000m;
    proxy_buffer_size 4k;
    proxy_buffers 100 8k;
    proxy_connect_timeout      60;
    proxy_send_timeout         60;
    proxy_read_timeout         60;
     include /usr/local/nginx/vhosts/*.conf;
}
Lets take a moment to review some of the more important options in nginx.conf before we move along…
user nobody nobody;
If you are running this on a server with an apache install or other software using the user ‘nobody’, it might be wise to create a user specifically for nginx (i.e: useradd nginx -d /usr/local/nginx -s /bin/false)
worker_processes 4;
This should reflect the number of CPU cores which you can find out by running ‘cat /proc/cpuinfo | grep processor‘ — I recommend a setting of at least 2 but no more than 6, nginx is VERY efficient.
proxy_cache_path /usr/local/nginx/proxy … inactive=7d max_size=1000m;
The ‘inactive’ option is the maximum age of content in the cache path and the ‘max_size’ is the maximum on disk size of the cache path. If you are serving up lots of object heavy content such as images, you are going to want to increase this.
proxy_send|read_timeout 60;
These timeout values are important, if you run any scripts through admin interfaces or other maintenance URL’s, these values will cause the proxy to time them out — that said increase them to sane values as appropriate, anything more than 300 is probably excessive and you should consider running such tasks from cronjobs.
Apache style MaxClients
Finally, maximum amount of connections, or MaxClients, that nginx can accept is determined by worker_processes * worker_connections/2 (2 fd per session) = 8192 MaxClients in our configuration.
Moving along we need to create two paths that we defined in our configuration, the first is the content caching folder and the second is where we will create our vhosts.
# mkdir /usr/local/nginx/proxy /usr/local/nginx/vhosts /usr/local/nginx/client_body_temp /usr/local/nginx/fastcgi_temp  /usr/local/nginx/proxy_temp
# chown    nobody.nobody    /usr/local/nginx/proxy    /usr/local/nginx/vhosts       /usr/local/nginx/client_body_temp /usr/local/nginx/fastcgi_temp   /usr/local/nginx/proxy_temp
Lets go ahead and get our initial vhosts file created, my template is available from here and should be saved to ‘/usr/local/nginx/vhosts/myforums.com.conf’, the contents of which are as follows:

No comments:

Post a Comment