الجمعة، 17 سبتمبر 2010

Setup Nginx as HTTP Deamon with vhosts

Posted Image


nginx [engine x] is a HTTP server and mail proxy server written by me (Igor Sysoev).

Nginx vs Apache2

In this Setup, Nginx is used for replace apache.

Installing by Portsnap
cd /usr/ports/www/nginx-devel/
make install clean


Select this Options
HTTP_MODULE 
HTTP_CACHE_MODULE
HTTP_REWRITE_MODULE
HTTP_SSL_MODULE
HTTP_STATUS_MODULE


Create vhost directory:
mkdir /usr/local/etc/nginx/vhosts


Our config files have been setup to help keep our server secure and prevent people from "snooping" around the server to find exploits and holes. This is just part of a multilayer security setup and will not prevent people from hacking your server. These confg files are rather self explanatory feel free to change as needed.

/usr/local/etc/nginx.conf
user     www www;
worker_processes   8;
pid     /var/run/nginx.pid;
events {
 worker_connections  8192;
}
http {
 include    mime.types;
 default_type   application/octet-stream;
 ## Log Cache
 open_log_file_cache max=1000 inactive=20s min_uses=2 valid=1m;
 ## Log Format
 log_format   main '$remote_addr - $remote_user [$time_local] $request '
      '"$status" $body_bytes_sent "$http_referer" '
      '"$http_user_agent" "$http_x_forwarded_for"';
 ## Logs
 #access_log /var/log/nginx-access.log main;
 #access_log off;
 error_log  /var/log/nginx-error.log debug gzip buffer=32k;
 ## Compression
 gzip   on;
 gzip_min_length 1100;
 gzip_buffers  16 8k;
 gzip_http_version 1.0;
 gzip_comp_level 1;
 gzip_proxied  any;
 gzip_types  text/plain text/css text/javascript text/xml text/php
    application/x-javascript application/xml application/xml+rss;
 ## Size Limits
 client_body_buffer_size   8k;
 client_header_buffer_size 256k;
 client_max_body_size      10m;
 large_client_header_buffers 256 256k;
 ## Timeouts
 client_body_timeout   5;
 client_header_timeout 5;
 keepalive_timeout     75 20;
 send_timeout          5;
 ## General Options
 ignore_invalid_headers   on;
 limit_zone gulag $binary_remote_addr 1m;
 recursive_error_pages    on;
 sendfile                 on;
 server_name_in_redirect off;
 server_tokens           off;
 ## TCP options  
 tcp_nodelay on;
 tcp_nopush  on;
 ## Run all config files in our vhost directory
 include /usr/local/etc/nginx/vhosts/*.conf;
}


Now create a vHost in /usr/local/etc/nginx/vhosts directory.
You must replace the domain rv89.eu to your domain.
server {
 listen  *:80;
 server_name rv89.eu;
 root          /usr/local/www/rv89.eu;
 #access_log /var/log/rv89.eu.access.log main;
 error_log /var/log/rv89.eu.error.log debug;
 #error_page 404 = /error/404.html;
 #error_page 502 503 504 = /error/50x_error.html;
 #location    /error/50x_error.html {
 #    internal;
 #}
 ## directory and index file
 location / {
 root   /usr/local/www/rv89.eu;
 autoindex off;
 index   index.php index.html index.htm;
 }
 ## php-fpm config
 location ~ \.php$ {
  fastcgi_index index.php;
  fastcgi_pass 127.0.0.1:9000;
  include    fastcgi_params;
  fastcgi_param CONTENT_TYPE $content_type;
  fastcgi_param CONTENT_LENGTH $content_length;
  fastcgi_param SCRIPT_NAME $fastcgi_script_name;
  fastcgi_param SCRIPT_FILENAME /usr/local/www/rv89.eu$fastcgi_script_name;
  fastcgi_param REQUEST_URI $request_uri;
  fastcgi_param DOCUMENT_URI $document_uri;
  fastcgi_intercept_errors on;
 }
 ## Cache of images
 location ~* \.(jpg|jpeg|gif|css|png|js|ico)$ {
  root          /usr/local/www/rv89.eu;
  access_log off;
  expires 30d;
  break;
 }
 ## Serve an empty 1x1 gif _OR_ an error 204 (No Content) for favicon.ico
 location = /favicon.ico {
 #empty_gif;
  return 204;
 }
 ## If the file exists as a static file serve it directly without running all the other rewite tests on it
 if (-f $request_filename) { 
  break; 
 }
 ## Only allow GET and HEAD request methods
      if ($request_method !~ ^(GET|HEAD)$ ) {
         return 444;
      }
}


Now edit the file /etc/rc.conf

And add:
nginx_enable="YES"


Run
rehash


Start The WebServer Nginx
/usr/local/etc/rc.d/nginx start


You have install your server. Next guide will be dedicate to installing PHP on Nginx

ليست هناك تعليقات:

إرسال تعليق