nginx安装与配置
Nginx ("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过四年了。Igor 将源代码以类BSD许可证的形式发布。
Nginx 超越 Apache 的高性能和稳定性,使得国内使用 Nginx 作为 Web 服务器的网站也越来越多,其中包括新浪天涯、网易等门户网站。
nginx中文文档:http://wiki.nginx.org/Chs
安装NGINX:
1、安装Nginx所需的pcre库:
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.12.tar.gz
tar zxvf pcre-8.12.tar.gz
cd pcre-8.12/
./configure && make && make install
下载nginx,当前时间2011/3/20 13:45最新稳定版本为0.8.54
wget http://nginx.org/download/nginx-0.8.54.tar.gz
nginx缓存模块:
http://labs.frickle.com/files/ngx_cache_purge-1.2.tar.gz
./configure --prefix=/usr/local/nginx --without-http_autoindex_module --without-http_geo_module --without-http_map_module --without-http_browser_module --with-http_stub_status_module --add-module=/root/ngx_cache_purge-1.2
make && make install
以下是对./configure --help 中可关闭模块的分析.
--without-select_module
一种文件读取模式,低效的轮询模式,可关闭
--without-poll_module
一种文件读取模式,低效的轮询模式,可关闭
--without-http_charset_module
定义文件编码格式,通常格式由文件本身来设定即可, 可关闭
--without-http_gzip_module
压缩传输, 该方案对HTML,CSS,JS等可压缩文件有效,对图片传输无帮助, 可关闭
--without-http_ssi_module
SSI是英文Server Side Includes的缩写,翻译成中文就是服务器端包含的意思。从技术角度上说,SSI就是在HTML文件中,可以通过注释行调用的命令或指针。SSI具有强大的功能,只要使用一条简单的SSI命令就可以实现整个网站的内容更新,时间和日期的动态显示,以及执行shell和CGI脚本程序等复杂的功能。可关闭
--without-http_userid_module
在无法使用cookies的地方实现用户标识,用于链接中自动增加sessionID等.可关闭
--without-http_auth_basic_module
一种服务器用户验证方案,经常被用来锁定 stub_status 记录
location /nginx_status {
stub_status on;
access_log off;
auth_basic “nginx_status”;
auth_basic_user_file conf/htpasswd;
}
其中htpasswd,可用apache htpasswd工具生成或者用在线生成工具生成
http://www.4webhelp.net/us/password.php
--without-http_autoindex_module
自动生成文件列表,生产服务器上通常是关闭的.
--without-http_geo_module
根据来访IP判断用户访问, 类似智能解析,智能网段负载均衡等, 可关闭.
网上例子: http://deidara.blog.51cto.com/400447/196276
--without-http_map_module
条件赋值模块,用于复杂条件的判断, 可关闭
--without-http_proxy_module
反向代理模块, 可关闭
--without-http_fastcgi_module
用于处理fastcgi,动态文件部分,可关闭
--without-http_memcached_module
memcached缓存模块, 通常用于服务器间共享数据, 方便但效率不高, 可关闭
--without-http_limit_zone_module
并发控制, 通常用在下载服务器控制, 可关闭
--without-http_limit_req_module
带宽控制, 通常用在下载服务器控制, 可关闭
--without-http_empty_gif_module
强制返回一个在内存中的1*1的空GIF图, 通常用于访问统计/日志记录等, 可关闭
--without-http_browser_module
根据访问者浏览器进行基本判断, 可关闭
--without-http_upstream_ip_hash_module
负载均衡模块,可关闭
ngx_http_access_module
访问控制模块, 通常必开, 图片服务器适用
ngx_http_referer_module
referer相关处理, 通常用于访问控制, 图片服务器适用
ngx_http_rewrite_module
地址重定向, 通常用于访问控制, 图片服务器适用
管理nginx:
启动:/usr/local/nginx/sbin/nginx
测试:/usr/local/nginx/sbin/nginx -t
重载:/usr/local/nginx/sbin/nginx -s reload
nginx配置
http {
缓存配置:
#注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区
proxy_temp_path /tmp/proxy_temp;
#设置Web缓存区名称为cache_one,内存缓存空间大小为2000MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
proxy_cache_path /tmp/proxy_cache levels=1:2 keys_zone=cache_one:2000m inactive=1d max_size=30g;
#upstream要放server前面
#集群节点配置
upstream cluster_tomcat {
ip_hash;
server 127.0.0.1:8080 ;
server 127.0.0.1:9090 ;
server 192.168.1.6:8080 ;
server 192.168.1.6:9090 ;
}
#虚拟主机配置
server {
listen 80;
server_name *.host.com;
access_log logs/search.access.log;
root /home/tomcat/search/webroot;
index index.html index.htm;
location / {
root /home/tomcat/search/webroot;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.(do|htm)$ {
#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
#对不同的HTTP状态码设置不同的缓存时间
proxy_cache_valid 200 304 12h;
#以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
proxy_cache_key $host$uri$is_args$args;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://cluster_tomcat;
expires 1d;
}
#用于清除缓存,假设一个URL为http://192.168.8.42/test.txt,通过访问http://192.168.8.42/purge/test.txt就可以清除该URL的缓存。
location ~ /purge(/.*) {
#设置只允许指定的IP或IP段才可以清除URL缓存。
allow 127.0.0.1;
allow 192.168.0.0/16;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
}
location ~ .*\.(js|css)?$ {
expires 1h;
}
}
}
Nginx 超越 Apache 的高性能和稳定性,使得国内使用 Nginx 作为 Web 服务器的网站也越来越多,其中包括新浪天涯、网易等门户网站。
nginx中文文档:http://wiki.nginx.org/Chs
安装NGINX:
1、安装Nginx所需的pcre库:
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.12.tar.gz
tar zxvf pcre-8.12.tar.gz
cd pcre-8.12/
./configure && make && make install
下载nginx,当前时间2011/3/20 13:45最新稳定版本为0.8.54
wget http://nginx.org/download/nginx-0.8.54.tar.gz
nginx缓存模块:
http://labs.frickle.com/files/ngx_cache_purge-1.2.tar.gz
./configure --prefix=/usr/local/nginx --without-http_autoindex_module --without-http_geo_module --without-http_map_module --without-http_browser_module --with-http_stub_status_module --add-module=/root/ngx_cache_purge-1.2
make && make install
以下是对./configure --help 中可关闭模块的分析.
--without-select_module
一种文件读取模式,低效的轮询模式,可关闭
--without-poll_module
一种文件读取模式,低效的轮询模式,可关闭
--without-http_charset_module
定义文件编码格式,通常格式由文件本身来设定即可, 可关闭
--without-http_gzip_module
压缩传输, 该方案对HTML,CSS,JS等可压缩文件有效,对图片传输无帮助, 可关闭
--without-http_ssi_module
SSI是英文Server Side Includes的缩写,翻译成中文就是服务器端包含的意思。从技术角度上说,SSI就是在HTML文件中,可以通过注释行调用的命令或指针。SSI具有强大的功能,只要使用一条简单的SSI命令就可以实现整个网站的内容更新,时间和日期的动态显示,以及执行shell和CGI脚本程序等复杂的功能。可关闭
--without-http_userid_module
在无法使用cookies的地方实现用户标识,用于链接中自动增加sessionID等.可关闭
--without-http_auth_basic_module
一种服务器用户验证方案,经常被用来锁定 stub_status 记录
location /nginx_status {
stub_status on;
access_log off;
auth_basic “nginx_status”;
auth_basic_user_file conf/htpasswd;
}
其中htpasswd,可用apache htpasswd工具生成或者用在线生成工具生成
http://www.4webhelp.net/us/password.php
--without-http_autoindex_module
自动生成文件列表,生产服务器上通常是关闭的.
--without-http_geo_module
根据来访IP判断用户访问, 类似智能解析,智能网段负载均衡等, 可关闭.
网上例子: http://deidara.blog.51cto.com/400447/196276
--without-http_map_module
条件赋值模块,用于复杂条件的判断, 可关闭
--without-http_proxy_module
反向代理模块, 可关闭
--without-http_fastcgi_module
用于处理fastcgi,动态文件部分,可关闭
--without-http_memcached_module
memcached缓存模块, 通常用于服务器间共享数据, 方便但效率不高, 可关闭
--without-http_limit_zone_module
并发控制, 通常用在下载服务器控制, 可关闭
--without-http_limit_req_module
带宽控制, 通常用在下载服务器控制, 可关闭
--without-http_empty_gif_module
强制返回一个在内存中的1*1的空GIF图, 通常用于访问统计/日志记录等, 可关闭
--without-http_browser_module
根据访问者浏览器进行基本判断, 可关闭
--without-http_upstream_ip_hash_module
负载均衡模块,可关闭
ngx_http_access_module
访问控制模块, 通常必开, 图片服务器适用
ngx_http_referer_module
referer相关处理, 通常用于访问控制, 图片服务器适用
ngx_http_rewrite_module
地址重定向, 通常用于访问控制, 图片服务器适用
管理nginx:
启动:/usr/local/nginx/sbin/nginx
测试:/usr/local/nginx/sbin/nginx -t
重载:/usr/local/nginx/sbin/nginx -s reload
nginx配置
http {
缓存配置:
#注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区
proxy_temp_path /tmp/proxy_temp;
#设置Web缓存区名称为cache_one,内存缓存空间大小为2000MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
proxy_cache_path /tmp/proxy_cache levels=1:2 keys_zone=cache_one:2000m inactive=1d max_size=30g;
#upstream要放server前面
#集群节点配置
upstream cluster_tomcat {
ip_hash;
server 127.0.0.1:8080 ;
server 127.0.0.1:9090 ;
server 192.168.1.6:8080 ;
server 192.168.1.6:9090 ;
}
#虚拟主机配置
server {
listen 80;
server_name *
access_log logs/search.access.log;
root /home/tomcat/search/webroot;
index index.html index.htm;
location / {
root /home/tomcat/search/webroot;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.(do|htm)$ {
#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
#对不同的HTTP状态码设置不同的缓存时间
proxy_cache_valid 200 304 12h;
#以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
proxy_cache_key $host$uri$is_args$args;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://cluster_tomcat
expires 1d;
}
#用于清除缓存,假设一个URL为http://192.168.8.42/test.txt
location ~ /purge(/.*) {
#设置只允许指定的IP或IP段才可以清除URL缓存。
allow 127.0.0.1;
allow 192.168.0.0/16;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
}
location ~ .*\.(js|css)?$ {
expires 1h;
}
}
}

浙公网安备 33010602011771号