Nginx

来自开放百科 - 灰狐
跳转到: 导航, 搜索
Wikipedia-35x35.png 您可以在Wikipedia上了解到此条目的英文信息 Nginx Thanks, Wikipedia.
Nginx-logo.png

Nginx

目录

简介

nginx [engine x] is a HTTP server and mail proxy server.

Nginx is #8 on January 2008 Netcraft survey with more than 842,000 domains using it! (8x since last year) (05 Jan 2008)

新闻

功能

指南

./configure
make
sudo make install

一个参考

./configure \
 --prefix=/usr \
 --sbin-path=/usr/sbin/nginx \
 --conf-path=/etc/nginx/nginx.conf \
 --error-log-path=/var/log/nginx/error.log \
 --pid-path=/var/run/nginx/nginx.pid  \
 --lock-path=/var/lock/nginx.lock \
 --user=nginx \
 --group=nginx \
 --with-http_ssl_module \
 --with-http_flv_module \
 --with-http_gzip_static_module \
 --http-log-path=/var/log/nginx/access.log \
 --http-client-body-temp-path=/var/tmp/nginx/client/ \
 --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
 --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ 

more detail: http://wiki.nginx.org/NginxModules

可能的问题: ./configure: error: the HTTP rewrite module requires the PCRE library.

apt-get install libpcre3-dev

也可能需要 apt-get install zlib1g zlib1g-dev ./configure: error: the HTTP cache module requires md5 functions from OpenSSL library.

apt-get install libssl-dev

Start

# /usr/local/nginx/sbin/nginx // 配置文件: usr/local/nginx/conf/nginx.conf

reload the configuration with

kill -HUP `cat /usr/local/nginx/logs/nginx.pid`

列出目录

autoindex on;
autoindex_exact_size off;
autoindex_localtime on;

分发版

Java

Nginx作为Jetty, Apache Tomcat的前端

Python

PHP

  • 安装PHP-FPM
  • 配置nginx使其支持PHP

修改 /usr/local/etc/nginx/nginx.conf

修改 /usr/local/etc/nginx/fastcgi_params

修改 //usr/local/etc/php-fpm.conf

Ruby

Markdown

配置 Nginx 支持 Markdown 格式

为什么选择Nginx

Nginx 是一个很牛的高性能Web和反向代理服务器, 它具有有很多非常优越的特性:

  • 在高连接并发的情况下,Nginx是Apache服务器不错的替代品: Nginx在美国是做虚拟主机生意的老板们经常选择的软件平台之一. 能够支持高达 50,000 个并发连接数的响应, 感谢Nginx为我们选择了 epoll and kqueue作为开发模型. Nginx使用了最新的epoll(Linux 2.6内核)和kqueue(FreeBSD)网络I/O模型,而Apache则使用的是传统的Select模型. 目前Linux下能够承受高并发访问的Squid、Memcached都采用的是epoll网络I/O模型.
  • Nginx作为负载均衡服务器: Nginx 既可以在内部直接支持 Rails 和 PHP 程序对外进行服务, 也可以支持作为 HTTP代理服务器对外进行服务. Nginx采用C进行编写, 不论是系统资源开销还是CPU使用效率都比 Perlbal 要好很多.
  • 作为邮件代理服务器: Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器), Last.fm 描述了成功并且美妙的使用经验.
  • Nginx 是一个 安装非常的简单 , 配置文件 非常简洁(还能够支持perl语法), Bugs非常少的服务器: Nginx 启动特别容易, 并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动. 你还能够 不间断服务的情况下进行软件版本的升级.

参考:

Hash表

Ngnix使用hash表来协助完成请求的快速处理。

考虑到保存键及其值的hash表存储单元的大小不至于超出设定参数(hash bucket size), 在启动和每次重新配置时,Nginx为hash表选择尽可能小的尺寸。

直到hash表超过参数(hash max size)的大小才重新进行选择. 对于大多数hash表都有指令来修改这些参数。例如,保存服务器名字的hash表是由指令 server_names_hash_max_size 和 server_names_hash_bucket_size所控制的。参数hash bucket size总是等于hash表的大小,并且是一路处理器缓存大小的倍数。在减少了在内存中的存取次数后,使在处理器中加速查找hash表键值成为可能。如果 hash bucket size等于一路处理器缓存的大小,那么在查找键的时候,最坏的情况下在内存中查找的次数为2。第一次是确定存储单元的地址,第二次是在存储单元中查找键值。因此,如果Nginx给出需要增大 hash max size 或 hash bucket size的提示,那么首要的是增大前一个参数的大小.

事件模型

Nginx支持如下处理连接的方法(I/O复用方法),这些方法可以通过use指令指定。

  • select - 标准方法。 如果当前平台没有更有效的方法,它是编译时默认的方法。你可以使用配置参数 --with-select_module 和 --without-select_module 来启用或禁用这个模块。
  • poll - 标准方法。 如果当前平台没有更有效的方法,它是编译时默认的方法。你可以使用配置参数 --with-poll_module 和 --without-poll_module 来启用或禁用这个模块。
  • kqueue - 高效的方法,使用于 FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X. 使用双处理器的MacOS X系统使用kqueue可能会造成内核崩溃。
  • epoll - 高效的方法,使用于Linux内核2.6版本及以后的系统。在某些发行版本中,如SuSE 8.2, 有让2.4版本的内核支持epoll的补丁。
  • rtsig - 可执行的实时信号,使用于Linux内核版本2.2.19以后的系统。默认情况下整个系统中不能出现大于1024个POSIX实时(排队)信号。这种情况对于高负载的服务器来说是低效的;所以有必要通过调节内核参数 /proc/sys/kernel/rtsig-max 来增加队列的大小。可是从Linux内核版本2.6.6-mm2开始, 这个参数就不再使用了,并且对于每个进程有一个独立的信号队列,这个队列的大小可以用 RLIMIT_SIGPENDING 参数调节。当这个队列过于拥塞,nginx就放弃它并且开始使用 poll 方法来处理连接直到恢复正常。
  • /dev/poll - 高效的方法,使用于 Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+.
  • eventport - 高效的方法,使用于 Solaris 10. 为了防止出现内核崩溃的问题, 有必要安装 这个 安全补丁。

Basic HTTP features

  • Handling of static files, index files, and autoindexing; open file descriptor cache;
  • Accelerated reverse proxying without caching, simple load balancing and fault tolerance;
  • Accelerated support without caching of remote FastCGI servers, simple load balancing and fault tolerance;
  • Modular architecture. Filters include gzipping, byte ranges, chunked responses, and SSI. Multiple SSI inclusions within a single page can be processed in parallel if they are handled by FastCGI or proxied servers.
  • SSL and TLS SNI support.

Mail proxy server features

  • User redirection to IMAP/POP3 backend using an external HTTP authentication server;
  • User authentication using an external HTTP authentication server and connection redirection to internal SMTP backend;
  • Authentication methods:
         o POP3: USER/PASS, APOP, AUTH LOGIN PLAIN CRAM-MD5;
         o IMAP: LOGIN, AUTH LOGIN PLAIN CRAM-MD5;
         o SMTP: AUTH LOGIN PLAIN CRAM-MD5;
  • SSL support;
  • STARTTLS and STLS support.

Other HTTP features

  • name- and IP-based virtual servers;
  • keep-alive and pipelined connections support;
  • flexible configuration;
  • reconfiguration and online upgrade without interruption of the client processing;
  • access log formats, bufferred writing, and quick log rotation;
  • 4xx-5xx error codes redirection;
  • rewrite module;
  • access control based on client IP address and HTTP Basic authentication;
  • PUT, DELETE, MKCOL, COPY and MOVE methods;
  • FLV streaming;
  • speed limitation;
  • limitation of simultaneous connections from one address.

Nginx + Jetty

Nginx + GlassFish

upstream glassfish {
       server 127.0.0.1:3000; 
   }

Modules

需要 ngx_http_autoindex_module 模块

目录浏览

location  /  {
    autoindex  on;
}

站点

server  
{        
listen 80;
server_name blog.huihoo.com;
index index.htm index.html index.php;
root /var/www/blog.huihoo.com;
       location ~ \.php$ {
               fastcgi_split_path_info ^(.+\.php)(/.+)$;
               # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
               # With php5-cgi alone:
               #fastcgi_pass 127.0.0.1:9000;
               # With php5-fpm:
               fastcgi_pass unix:/var/run/php5-fpm.sock;
               fastcgi_index index.php;
               include fastcgi_params;
               client_max_body_size 30m;
       }
}

多站点

 server { # php/fastcgi
   listen       80;
   server_name  domain1.com www.domain1.com;
   access_log   logs/domain1.access.log  main;
   root         html;
   location ~ \.php$ {
     fastcgi_pass   127.0.0.1:1025;
   }
 }
 server { # simple reverse-proxy
   listen       80;
   server_name  domain2.com www.domain2.com;
   access_log   logs/domain2.access.log  main;
   # serve static files
   location ~ ^/(images|javascript|js|css|flash|media|static)/  {
     root    /var/www/virtual/big.server.com/htdocs;
     expires 30d;
   }

更完整的Nginx配置信息

Rewrite

Discuz!在Nginx下的Rewrite

rewrite ^/archiver/((fid|tid)-[\w\-]+\.html)$ /archiver/index.php?$1 last;
rewrite ^/forum-([0-9]+)-([0-9]+)\.html$ /forumdisplay.php?fid=$1&page=$2 last;
rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /viewthread.php?tid=$1&extra=page%3D$3&page=$2 last;
rewrite ^/space-(username|uid)-(.+)\.html$ /space.php?$1=$2 last;
rewrite ^/tag-(.+)\.html$ /tag.php?name=$1 last;
break;

技巧

没有html后缀,也能正常显示,如:http://docs.huihoo.com/go/golang.org/doc/go1.6 且类似 Makefile .bat .bash .rc .s .go 的文件都能显示。

location / {     
add_header  Content-Type: 'text/html; charset=utf-8';

但会导致一些浏览器无法打开 docs.huihoo.com 网页会直接下载而不会被浏览,已暂时取消 add_header 设置。

Module ngx_http_headers_module

http://book.huihoo.com/practical-postgresql/ppbook/ 的问题还没解决

支持在线播放 avi, wmv

 video/x-ms-wmv                        wmv;
 video/x-msvideo                       avi;

图集

Nginx架构

链接

分享您的观点
个人工具
名字空间

变换
操作
导航
工具箱