Nginx Basic Configuration

Start configuring your Nginx server the right way

  1. Floren
    Many people assume Nginx is easy to optimize. That is a false conception, Nginx performs best if you know how to adjust properly its configuration files and understand what each option does. I see many users simply Google'ing some info and pasting blindly all kind of parameters and values without wondering what they do. Please don't make the mistake and assume that whatever you find on the Internet is OK, because is not. Nginx is not Apache, it takes a lot of tweaking and you really have to work closely with your server logs in debug mode to properly tune everything.

    See below a basic Nginx configuration for Red Hat servers, used as start-up canvas. It will help you start the right way, while you study the documentation at your own pace.

    Install Nginx with built-in debug mode enabled:
    pcre_jit                                on;
    user                                    nginx nginx;
    pid                                     /var/run/;
    worker_processes                        8;
    worker_rlimit_nofile                    1024;
    events {
            worker_connections              2048;
            use epoll;
    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" "$gzip_ratio"';
            access_log                      off;
            error_log                       /var/log/nginx/error.log error;
            include                         /etc/nginx.d/*.conf;
    You can determine the number of worker_processes with the following command:
    server {
            listen                ;
            server_name           ;
            access_log                      off;
            error_log                       /var/log/nginx/localhost.error.log   error;
            return                          301$request_uri;
    server {
            listen                 default_server;
            server_name           ;
            access_log                      /var/log/nginx/localhost.access.log  main buffer=64k flush=5m;
            error_log                       /var/log/nginx/localhost.error.log   error;
            root                            /var/www/html;
            index                           index.php index.html;
            location / {
                    try_files               $uri $uri/ /index.php =404;
            location ~ \.php$ {
                    try_files               $uri =404;
                    fastcgi_split_path_info ^(.+\.php)(/.+)$;
                    fastcgi_pass  ;
                    fastcgi_param           PATH_INFO        $fastcgi_path_info;
                    fastcgi_param           PATH_TRANSLATED  $document_root$fastcgi_script_name;
                    include                 fastcgi.conf;
            location ~* \.(?:ico|css|gif|jpe?g|js|png|swf)(\?.+)?$ {
                    access_log              off;
                    log_not_found           off;
                    expires                 1y;
    From there, you can add new options into either http or server sections.
    A good start on testing your configuration would be to emulate your site traffic with Siege, on a similar server. That should give your a lot of clues into Nginx logs. Then, when you get an approximate tune-up, you can start tweaking the tested values based on your real live results.
    MattW likes this.

Recent Updates

  1. SSL Server Redirect
  2. CloudFlare Real IP Adresses
  3. Geo Ban

Recent Reviews

  1. ehsan
    Excellent tutorial, nginx indeed is a powerful backend.