User Tools

Site Tools


linux:applications:nginx

Nginx - web server

nginx - Robust, small and high performance http and reverse proxy server Nginx [engine x] is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server written by Igor Sysoev.

Installation

Download and install the RPM package:

Make sure you replace the nginx package with the newest available for your platform on http://download.fedora.redhat.com/
wget http://download.fedora.redhat.com/pub/epel/5/i386/nginx-0.6.35-2.el5.i386.rpm
rpm -ivh nginx-0.6.35-2.el5.i386.rpm

Configure you server accordingly. You can find many sample configurations on Nginx Wiki

vim /etc/nginx/nginx.conf
You can find the default configuration in /etc/nginx/nginx.conf.default

Enable the nginx service via chkconfig and start the server (check you configuration first):

chkconfig nginx on
service nginx start

Check the nginx error log in case of problems:

tail /var/log/nginx/error.log

Configuration examples

One static site and two proxies

#----------------------------------------------------------------------
# Events Module
#
#   http://wiki.codemongers.com/NginxEventsModule
#
#----------------------------------------------------------------------
 
events {
    worker_connections  1024;
}
 
 
#----------------------------------------------------------------------
# HTTP Core Module
#
#   http://wiki.codemongers.com/NginxHttpCoreModule
#
#----------------------------------------------------------------------
 
http {
    include       /etc/nginx/mime.types;
    include      /etc/nginx/proxy.conf;
    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  0;
    keepalive_timeout  5;
 
    gzip  on;
 
    # Load config files from the /etc/nginx/conf.d directory
    include /etc/nginx/conf.d/*.conf;
 
    #
    # The default server
    #
    server {
        listen       80;
        server_name  _ example.com;
 
        #charset koi8-r;
 
        #access_log  logs/host.access.log  main;
 
        location / {
            root   /srv/www/admin/;
            index  index.html index.htm;
        }
 
        # static content for phpPgAdmin directly server trough nginx
        # everything else is passed as proxy to Apache
        location /phppgadmin/ {
            location ~* ^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|js)$ {
                root /srv/www/admin/;
                expires 1m;
            }
            proxy_pass      http://127.0.0.1:8080;
        }
 
        # proxy to Apache for phpMyAdmin
        location /phpmyadmin/ {
            proxy_pass      http://127.0.0.1:8080;
        }
 
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
            deny  all;
        }
    }
}

Server every thing but php true nginx

events {
    worker_connections  1024;
}


#----------------------------------------------------------------------
# HTTP Core Module
#
#   http://wiki.codemongers.com/NginxHttpCoreModule
#
#----------------------------------------------------------------------

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  0;
    keepalive_timeout  5;

    gzip  on;

    # Load config files from the /etc/nginx/conf.d directory
    include /etc/nginx/conf.d/*.conf;

    #
    # The default server
    #
    server {
        listen       8000;
        server_name  example.com;
        access_log  /var/log/nginx/example.com.access.log  main;

        # serve static files
        location / {
          root    /mnt/nas/srv/www/example.com/htdocs/;
          expires 1d;
        }

        # serve dynamic php files with Apache
        location ~* ^.+.(php|php5)$ {
          proxy_pass      http://127.0.0.1:80;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #        proxy_pass   http://127.0.0.1:80;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
            deny  all;
        }
    }
}

Proxy config

proxy.conf example included with “include /etc/nginx/proxy.conf;” (see above)

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_buffers           32 4k;
/srv/wiki.niwos.com/data/pages/linux/applications/nginx.txt · Last modified: 2009/08/15 19:07 (external edit)