nginx
nginx详解
什么是nginx
Nginx (engine x) 是一个高性能的HTTP和反向代理服务,也是一个IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。
其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好。
nginx的安装配置详解
nginx的安装
# 安装nginx依赖包
yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel
# 下载nginx
$ wget http://nginx.org/download/nginx-1.8.0.tar.gz
# 解压缩
$ tar -zxvf nginx-1.8.0.tar.gz
# 配置
$ ./configure --prefix=/usr/local/nginx
# 编译并安装
$ make && make install
# 查看nginx的版本
$ /usr/local/nginx/sbin/nginx -v
# 校验nginx的配置是否正确
$ /usr/local/nginx/sbin/nginx -t
# 默认配置文件 /usr/local/nginx/conf/nginx.conf,-c 指定
$ /usr/local/nginx/sbin/nginx
$ /usr/local/nginx/sbin/nginx -s reload # 重新载入配置文件
# 重启Nginx,不会改变启动时指定的配置文件
$ /usr/local/nginx/sbin/nginx -s reopen
# 停止Nginx
$ /usr/local/nginx/sbin/nginx -s stop
安装ssl模块
# 进入nginx解压目录
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
# 编译不要make install,否则就覆盖安装了make完之后在objs目录下就生成会有一个nginx文件这个就是支持ssl的nginx程序
$ make
# 备份旧的nginx程序
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
# 把新的nginx程序覆盖旧的
cp objs/nginx /usr/local/nginx/sbin/nginx
nginx 配置文件
# nginx进程,一般设置为和cpu核数一样
worker_processes 1;
# 错误日志存放目录
# 全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
# 进程pid存放位置
#pid logs/nginx.pid;
# 一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致。
worker_rlimit_nofile 65535;
# nginx工作模式配置
events {
# 单个后台worker process进程的最大并发链接数
worker_connections 1024;
multi_accept on;
# epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
use epoll;
}
# http设置
http {
# 文件扩展名与文件类型映射表
include mime.types;
# 默认文件类型
default_type application/octet-stream;
# 开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
sendfile on;
# 长连接超时时间,单位是秒
keepalive_timeout 65;
# 存储访问记录的日志目录
access_log logs/access.log;
# 存储记录错误发生的日志目录
error_log logs/error.log;
# 设定mime类型,类型由mime.type文件定义
include mime.types;
server {
listen 80;
server_name *.xxx.com;
return 301 https://$server_name$request_uri;
}
# 虚拟主机的配置
server {
# 监听端口
listen 443 ssl;
# 域名可以有多个,用空格隔开
server_name *.xxx.com;
# 字符集
charset utf-8;
ssl on;
# 证书路径
ssl_certificate /xxx/yyy/fullchain.pem;
# 私钥路径
ssl_certificate_key /xxx/yyy/privkey.pem;
# 开启gzip压缩
gzip on;
# 连接超时时间
keepalive_timeout 65;
location / {
# 访问域名跟目录
root /xxx/xxx/;
# 载入其他配置文件
index index.html;
if ($host ~ ^(api)\.rllin\.cn$){
proxy_pass http://0.0.0.0:9901;
}
if ($host ~ ^(dev)\.rllin\.cn$){
proxy_pass http://0.0.0.0:9902;
}
}
# 错误页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# 其他配置文件位置
include /usr/local/nginx/conf.d/*.xxx.conf;
}