Configure nginx as front-end web server

I’ve been using lighttpd for a long time, it’s faster and stable. But 1.4.x series of lighttpd lacks some features I need: powerful proxy mod, dav_svn mod, real IP extract (as mod_extract_forwarded in apache).

Most of the bandwidth of feuvan.net is contributed to a web game proxy using proxy mod of lighttpd 1.4.13 : http://chaoswar.feuvan.net (official: http://alpha.chaoswar.cn:8080 ),  the lighttpd causes many TIME_WAIT connections in high load condition. Therefore, I want to use a powerful proxy like squid to do the proxy work. nginx ( Engine X ) is my selection. Squid is too heavy ;-).

It’s easy to install in debian, just type aptitude install nginx as root. Then I changed lighttpd listening port to localhost:81, and configure nginx to run on 0.0.0.0:80.

nginx.conf:

user www-data;
worker_processes  1;

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

events {
    worker_connections  4096;
    use epoll;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    access_log  /var/log/nginx/access.log;

    sendfile        on;
    tcp_nopush     on;

    keepalive_timeout  35;
    tcp_nodelay        on;

    gzip  on;

    server {
        listen       80;

        location / {
            proxy_pass http://127.0.0.1:81;
            include         /etc/nginx/proxy.conf;
        }

    }

    upstream chaoswar_cn {
        server alpha.chaoswar.cn:8080;
    }
    server {
        listen          80;
        server_name     chaoswar.feuvan.net;

        location / {
            proxy_pass http://chaoswar_cn;
            include         /etc/nginx/proxy.conf;
        }
    }
}

proxy.conf:

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   30;
proxy_send_timeout      30;
proxy_read_timeout      30;
proxy_buffer_size       4k;
proxy_buffers           4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

The most content of configure files are copied from nginx wiki, it’s easy to read. And may be I can do more optimizations like static content (picuture) cache ( expire 30d?). But the main purpose of this article is to show you a general whole picture of the proxy function of Nginx.

BTW, nginx is not just a light-weight squid like proxy server! It’s also a HTTP/MAIL Server.

Anyway, I’m still waiting for 1.5.0 release of lighttpd.

See also: nginx, nginx wiki, lighttpd 1.4.x mod_proxy, lighttpd 1.5.x mod_proxy_core

Nginx vs Lighttpd for a small VPS

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
This entry was posted in Default and tagged , . Bookmark the permalink.

2 Responses to Configure nginx as front-end web server

  1. haohao says:

    我用 nginx lighttpd 也建立和你一样的环境,但是奇怪的是,通过nginx 读取的网页都无法显示图片,请问你有没有碰到类似问题,是如何解决的. 谢谢~~~

  2. feuvan says:

    看看 mime.types

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by WP Hashcash