Nginx 使用总结

Nginx是俄罗斯人编写的轻量级的HTTP服务器,是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器。自Nginx 发布以来已经因为它的稳定性、丰富的功能集、全面的示例配置文件和低系统资源消耗而闻名。Nginx专为性能优化而开发,性能是其最重要的考量。它支持内核Poll模型,能经受高负载的考验,有报告表明能支持高达 50000个并发连接数。Nginx具有很高的稳定性,其它HTTP服务器当遇到访问的峰值,或者有人恶意发起慢速连接时,也很可能会导致服务器物理内存耗尽,频繁交换,失去响应,只能重启服务器。例如当前Apache一旦上到200个以上进程,web响应速度就明显非常缓慢了。而Nginx采取了分阶段资源分配技术,使得它的CPU与内存占用率非常低。Nginx官方表示保持10000个没有活动的连接,它只占2.5M内存,所以类似DOS这样的攻击对Nginx来说基本上是毫无用处的。就稳定性而言,nginxlighttpd更胜一筹。Nginx支持热部署,它的启动特别容易, 并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动。你还能够在不间断服务的情况下,对软件版本进行进行升级。Nginx采用master-slave模型,能够充分利用SMP的优势,且能够减少工作进程在磁盘I/O的阻塞延迟。Nginx代码质量非常高,代码很规范,模块扩展也很容易。

当然,nginx还很年轻,多多少少存在一些问题,比如,Nginx是俄罗斯人创建,目前文档方面还不是很完善.因为文档大多是俄语,所以文档方面这也是个障碍.尽管Nignx的模块比较多,但它们还不够完善。对脚本的支持力度不够。这些问题,Nginx的作者和社区都在努力解决,我们有理由相信nginx将继续以高速的增长率来分享轻量级HTTP服务器市场,会有一个更美好的未来。

安装软件选择说明

软件

版本号

pcre

8.01

nginx

0.8.53

 

1)         安装步骤,参考如下

创建用户组及用户(若使用默认账户进行安装本步可跳过)

cd /usr/sbin

groupadd www

useradd –g www www

安装pcre,把需要安装的程序包上传到/opt文件夹

cd /opt
tar zxvf pcre-8.01.tar.gz
cd pcre-8.01
./configure
make

make install

安装nginx

cd /opt
tar zxvf nginx-0.8.53.tar.gz
cd nginx-0.8.53

./configure --user=www --group=www --prefix=/opt/nginx --without-http-cache

make

make install

 

2)         Nginx管理

启动

cd /opt/nginx/sbin

./nginx

检查配置文件是否正确,若输出如下内容说明文件配置正确

cd /opt/nginx/sbin

./nginx-t

the configuration file /opt/nginx/conf/nginx.conf syntax is ok

configuration file /opt/nginx/conf/nginx.conf test is successful

 

执行如下命令系统会自动加载修改后的配置信息,使系统在不间断服务的情况下重载服务,这是nginx非常优秀的地方,只有0.8.x版本以后支持如下命令

cd /opt/nginx/sbin

./ nginx -s reload

 

3)         设置开机自启服务

/etc/rc.local文件中加入nginx启动命令,

/opt/nginx/sbin/nginx

 

      

1)         Nginx配置文件(nginx.conf)说明

 

user www www;

#指定使用的用户,默认为匿名用户

worker_processes 1;

#开启的进程数,一般设置1-5,可参考系统cpu个数

#error_log logs/error.log;

#error_log logs/error.log notice;

#error_log logs/error.log info;

#定义错误日志,以及记录的日志等级

#pid logs/nginx.pid;

#定义pid文件位置

 

events {

# use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];

use epoll; #使用epolllinux2.6的高性能方式)

#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以后的系统。可是从Linux内核版本2.6.6-mm2开始,这个参数就不再使用了.

#/dev/poll - 高效的方法,使用于 Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ Tru64 UNIX 5.1A+.

#eventport - 高效的方法,使用于 Solaris 10. 为了防止出现内核崩溃的问题, 有必要安装 这个 安全补丁。

worker_connections 1024;

worker_connections 51200; #每个进程最大连接数(最大连接=连接数x进程数)

}

 

http {

include 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 logs/access.log main;

sendfile on;

#开启高效文件传输模式

#tcp_nopush on;

#该选项用于防止网络阻塞

#keepalive_timeout 0;

keepalive_timeout 65;

##长链接超时时间

#gzip on;

#打开gzip压缩

#fastcgi_connect_timeout 300;

#fastcgi_send_timeout 300;

#fastcgi_read_timeout 300;

#fastcgi_buffer_size 128k;

#fastcgi_buffers 4 256k;

#fastcgi_busy_buffers_size 256k;

#fastcgi_temp_file_write_size 256k;

#fastcgi_temp_path /dev/shm;

#fastcgi连接超时时间和缓存

 

server {

listen 80;

server_name localhost;

#主机名

#charset koi8-r;

#默认字符编码 charset gb2312

#access_log logs/host.access.log main;

location / {

pass路径匹配 能够匹配路径中带“/”的,不过需要注意的是如果之后也有相关“/”匹配项并且使用比此处更精确匹配符,则之后表达式优先

root html;

index index.html index.htm;

}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html

error_page 500 502 503 504 /50x.html;

location = /50x.html {

#精确的匹配,并且不再向下匹配

root html;

}

 

#location ~ /.php$ {

#正则表达式匹配 一旦匹配则不再向下匹配

# proxy_pass http://127.0.0.1;

#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

#

#location ~ /.php$ {

# root html;

# fastcgi_pass 127.0.0.1:9000;

#指定fastcgi的地址端口

# fastcgi_index index.php;

# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

# include fastcgi_params;

#}

# deny access to .htaccess files, if Apache's document root

# concurs with nginx's one

#

#location ~ //.ht {

# deny all;

#不允许访问以.ht开头的文件

#}

}

 

# 可以配置多个虚拟主机的情况,实现分组负载均衡

#server {

# listen 8000;

# listen somename:8080;

# server_name somename alias another.alias;

# location / {

# root html;

# index index.html index.htm;

# }

#}

#以上在配置虚拟主机

# HTTPS server

#server {

# listen 443;

# server_name localhost;

# ssl on;

# ssl_certificate cert.pem;

# ssl_certificate_key cert.key;

# ssl_session_timeout 5m;

# ssl_protocols SSLv2 SSLv3 TLSv1;

# ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;

# ssl_prefer_server_ciphers on;

#以上为ssl 配置

 

# location / {

# root html;

# index index.html index.htm;

# }

#}

}

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值