Tuesday, 11 November 2014

Nginx configuration for YII

1. Create General Nginx configuration 

2.  Config :


/etc/nginx/nginx.conf



user  nginx;
worker_processes  4;
worker_rlimit_nofile 100000;

pid        /var/run/nginx.pid;

events {
    worker_connections  50000;
    use epoll;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
  keepalive_requests 0;
  log_format  main  '"$time_iso8601","$remote_addr","$host","$request","$status",'
      '"$bytes_sent","$http_user_agent","$http_range",'
      '"$sent_http_accept_ranges","$sent_http_content_range",'
      '"$http_referer","$http_x_forwarded_for","$geoip_country_code"';
           
  geoip_country /usr/share/GeoIP/GeoIP.dat;

    access_log  /var/log/nginx/nginx-access.log  main  buffer=128k;
  error_log   /var/log/nginx/nginx-error.log;
  
  proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=defender-cache:1200m max_size=500000m inactive=600m;
 
    sendfile                on;
    tcp_nopush              on;
    tcp_nodelay             on;
    server_tokens           off;    

 ## Caching files
  open_file_cache         max=200000 inactive=20s;
  open_file_cache_valid      30s;
  open_file_cache_min_uses     2;
  open_file_cache_errors      on;

 ## Size Limits
  client_body_buffer_size         640k;
  client_header_buffer_size       640k;
  client_max_body_size            8G;
  large_client_header_buffers    6 6k;
  connection_pool_size           2048;
  request_pool_size               16k;
  server_names_hash_bucket_size   256;
  variables_hash_bucket_size    512;
  fastcgi_buffers         8192 128k;  
    proxy_buffers          8 512k;
    proxy_buffer_size        512k;
  fastcgi_buffer_size       128k;
  fastcgi_busy_buffers_size    256k;
  fastcgi_temp_file_write_size   256k;
  fastcgi_intercept_errors     off;

 ## Timeouts
  client_body_timeout             10s;
  client_header_timeout           10s;
  keepalive_timeout               10s;
  send_timeout                    10s;
  fastcgi_send_timeout            10s;
  fastcgi_read_timeout            10s;
  fastcgi_connect_timeout         10s;

## Compression
  gzip                     on;
  gzip_buffers             16 8k;
  gzip_comp_level          9;
  gzip_http_version        1.1;
  gzip_min_length          10;
  gzip_types               text/plain text/css application/javascript image/png image/gif image/jpeg application/x-javascrip text/xml application/xml;
  gzip_vary                on;
  gzip_static              on;
  gzip_proxied             any;
  gzip_disable             "MSIE [1-6]\.";

  
    include /etc/nginx/conf.d/*.conf;

  index  index.php index.html index.htm;
    
  include /etc/nginx/sites-enabled/*.conf;
}

/etc/nginx/sites-available/YOUR_APP.conf


server {
    listen       80;
 server_name  www.example.com;
 rewrite ^(.*)$ $scheme://example.com$1;
}

server {
    listen       80;
    server_name  example.com;
    root   /var/www/example.com/www/;
    index  index.php;
    access_log   /var/log/nginx/example.com.access.log main;
    access_log   /var/log/nginx/example.com.error.log;

    location / {
  #auth_basic "Access";
        #auth_basic_user_file /etc/nginx/.htpasswd;
        try_files $uri $uri/ /index.php?$args;
    }
    #deny access to protected directories
    location ~ ^/(backup|protected|framework|themes/\w+/views) { deny all; }

    location ~ /themes/\w+/views {deny all; access_log off; log_not_found off; }

    # prevent nginx from serving dotfiles (.htaccess, .svn, .git, etc.)
    location ~ /\. { deny all; access_log off; log_not_found off; }

    #location = /favicon.ico { log_not_found off; access_log off; }

    #avoid processing of calls to unexisting static files by yii
    location ~ (js|css|png|jpg|gif|ico|pdf|zip|rar)$ { try_files $uri =404;}
 location ~* \.(ico|css|js|gif|jpe?g|png|swf)$ {expires 30d; access_log off; }
    location ~ \.php {
        fastcgi_split_path_info  ^(.+\.php)(.*)$;

        fastcgi_param  PATH_INFO        $fastcgi_path_info;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include fastcgi_params;

        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;

        fastcgi_read_timeout 600s;
        fastcgi_send_timeout 600s;
    }
}


No comments:

Post a Comment