nginx

Web & Teknik

Här kommer lite råd om hur man enkelt kan ställa in sin nginx server. Dessa inställningar funkar för de allra flesta vanliga applikationer.

nginx.conf

user  nginx;
# kolla hur många kärnor du har men ett värde mellan 1-4 funkar för de flesta. Vet du inte skriv 1.
worker_processes  4;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

# kolla hur många kärnor du har.
worker_rlimit_nofile 8192; 

events {
# 1024 x antalet kärnor. Men det ska i de flesta fall inte behövas fler än nedan.
    worker_connections  4096; 
## epoll is preferred on 2.6 Linux
    ## kernels. Cf. http://www.kegel.com/c10k.html#nb.epoll
    use epoll;
    ## Accept as many connections as possible.
    multi_accept on;
}

 

http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

#    keepalive_timeout  65;
## Timeouts.
    client_body_timeout             60;
    client_header_timeout           60;
    keepalive_timeout            10 10;
    send_timeout                    60;

    ## Reset lingering timed out connections. Deflect DDoS.
    reset_timedout_connection on;


## TCP options.
    tcp_nodelay on;
    ## Optimization of socket handling when using sendfile.
    tcp_nopush on;

    ## Compression.
    gzip              on;
    gzip_buffers      16 8k;
    gzip_comp_level   1;
    gzip_http_version 1.1;
    gzip_min_length   10;
    gzip_types        text/plain text/css application/x-javascript text/xml application/xml application/xml+rss application/javascript text/javascript image/x-icon application/vnd.ms-fontobject font/opentype application/x-font-ttf;
    gzip_vary         on;
    gzip_proxied      any; # Compression for all requests.
    ## No need for regexps. See
    ## http://wiki.nginx.org/NginxHttpGzipModule#gzip_disable
    gzip_disable "msie6";

    ## Serve already compressed files directly, bypassing on-the-fly
    ## compression.
    gzip_static on;

    ## Enable clickjacking protection in modern browsers. Available in
    ## IE8 also. See
    ## https://developer.mozilla.org/en/The_X-FRAME-OPTIONS_response_header
    add_header X-Frame-Options sameorigin;

det är lika bra att ställa in fastcgicahce och proxycache även om man inte till en början tänker använd det.

# Proxy Cache nedan
    proxy_cache_path  /var/cache/nginx/proxy  levels=1:2    keys_zone=STATIC:10m inactive=24h  max_size=1g;
#Fastcgi cache nedan
fastcgi_cache_path /var/cache/nginx/fastcgi levels=1:2 keys_zone=PHPCACHE:100m inactive=60m;

fastcgi_cache_key "$scheme$request_method$host$request_uri";

## Include blacklist for bad bot and referer blocking.
    include blacklist.conf;
# inkludera givetvis sajter tex enligt nedan
    include /etc/nginx/conf.d/*.conf;
}