Virtual Hosts with NGINX

 

To use NGINX for virtual hosts (preferably name based) you can use the following technique. Your main NGINX config file includes all the site which are enabled for NGINX, following the sites-available / sites-enabled approach found e.g. with Apache.

To achieve this, first you need to configure your NGINX for inclusion of the servers and enabling the proxy modules in you nginx.conf

 

#user  nobody;
worker_processes  1;
daemon off;
 
#error_log  var/log/nginx//error.log;
#error_log  var/log/nginx/error.log  notice;
#error_log  var/log/nginx/error.log  info;
 
pid        /var/run/nginx.pid;
 
 
events {
    worker_connections  1024;
}
 
 
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  var/log/nginx/access.log  main;
    client_max_body_size        50m;
    sendfile        on;
    tcp_nopush     on;
    
    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay on;
 
#    gzip  on;
#    gzip_comp_level 1; gzip_proxied any;
#    gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    server {
        listen 80;
        server_name _;
        return 444;
    }
 
    include /opt/local/etc/nginx/sites-enabled/*;
#    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_ name;
}
 
 
Once this is done you can then have your site config file (e.g. cognovis.conf) in your sites-enabled directory
 
####################
# cognovis server
#####################
server {
    listen 192.168.2.5:80;
    server_name  cognovis.de;
    server_name  www.cognovis.de;
 
    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header  X-Forwarded-For  $remote_addr;
    }
    
    location /stats/ {
        include         proxy.conf;
    }
        
    access_log  /opt/local/var/log/nginx/cognovis.access.log  main;
}
 
The proxy.conf we are using looks like this:
 
proxy_redirect     off;

proxy_set_header   Host             $host;

proxy_set_header   X-Real-IP        $remote_addr;

proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

client_max_body_size       10m;

client_body_buffer_size    128k;

proxy_connect_timeout      90;

proxy_send_timeout         90;

proxy_read_timeout         90;

proxy_buffer_size          4k;

proxy_buffers              4 32k;

proxy_busy_buffers_size    64k;

proxy_temp_file_write_size 64k;