Nginx之开门见山
目录:
一、简介
二、安装
三、服务控制
四、配置文件说明
五、应用实例
一、简介
Nginx是一款开放源代码的高性能HTTP服务器、反向代理服务器,同时支持IMAP/POP3代理服务,这是一款自由的软件。
Igor Ysyoev在2002年开始开发该软件,于2004年发布第一个公开版本。
Nginx以高性能、高可用、丰富的功能模块、简单明了的配置文档以及低资源占用而著称。Nginx采用最新的网络I/O模型,支持高达50 000个并发连接。
近年来Nginx在国内取得了突飞猛进的发展,很多门户网站开始提供Nginx解决方案。
二、安装
Nginx软件可以到官网下载:http://nginx.org/en/download.html,目前最新的稳定版为1.2.2。本文运行环境为Nginx-1.2.2,CentOS6.2。
1. 二进制包安装:
(nginx for CentOS/Red Hat):
[root@centos6 ~]# cat > /etc/yum.repos.d/nginx.repo <<EOF
- >[nginx]
- >name=nginx rpm
- >baseurl=http://nginx.org/packages/centos/6/x86_64/
- >gpgcheck=0
- >enabled=1
- >EOF
[root@centos6 ~]# yum clean all
[root@centos6 ~]# yum install nginx-1.2.2
注意:http://nginx.org/packages/centos/6/x86_64/,路径需要根据你自己的实际系统版本与架构填写。 |
(nginx for Debain/Ubuntu):
jacob@ubuntu ~$ sudo cat >> /etc/apt/sources.list <<EOF
- >deb http://nginx.org/packages/ubuntu/ lucid nginx
- >deb-src http://nginx.org/packages/ubuntu/ lucid nginx
- >EOF
jacob@ubuntu ~$ sudo atp-get update
jacob@ubuntu ~$ sudo apt-get install nginx
2. 源码安装:
[root@centos6 ~]# wget http://nginx.org/download/nginx-1.2.2.tar.gz
[root@centos6 ~]# tar -xzf nginx-1.2.2.tar.gz -C /usr/src
[root@centos6 ~]# cd /usr/src/nginx-1.2.2
[root@centos6 ~]# ./configure --prefix=/usr/local/nginx \
> --with-ipv6 \
> --with-http_ssl_module \
> --with-http_realip_module \
> --with-http_addition_module \
> --with-http_image_filter_module \
> --with-http_dav_module \
> --with-http_flv_module \
> --with-http_mp4_module \
> --with-http_gzip_static_module \
> --with-http_perl_module \
> --with-mail \
> --with-mail_ssl_module
注意:
configure执行时会依赖与一些其他程序,如会出现报错信息(./configure: error: C compiler gcc is not found)可根据提示安装相应的软件包即可。 常见的软件包有(gcc,pcre,pcre-devel,openssl,openssl-devel,gd,gd-devel,perl- 5.10.1-127,perl-ExtUtils),其中gd-devel在CentOS6.2光盘中没有,需要从互联网安装。 configure的选项可以根据的自己需求输入,最简单的就是./configure不添加任何选项,所有设置均为默认。 |
[root@centos6 ~]# make
[root@centos6 ~]# make install
3.进阶:
Nginx核心模块包括主模块、事件模块,以下这些模块将被configure自动编译,除非configure时指定相应的--without。
模块名称 描述 禁用选项
Core 控制端口,错误页面别名等核心功能 --without-http
Access 基于IP的访问控制 --without-http_access_module
Auth Basic HTTP用户认证模块 --without-http_auth_basic_module
Auto Index 自动目录索引 --without-http_autoindex_module
Browser 描述用户代理 --without-http_charset_module
Charset 重新编码网页 --without-http_charset_module
Empty GIF 内存中存放一个图片 --without-http_empty_gif_module
FastCGI FastCGI支持 --without-http_fastcgi_module
Geo 支持IP变量设置 --without-http_geo_module
Gzip Gzip压缩 --without-http_gzip_module
Headers 设置http响应的头部信息
Index 首页
Limit Requests 限制客户端连接频率 --without-http_limit_req_module
Limit Conn 会话的并发连接 --without-http_limit_conn_module
Log 自定义日志
Map 设置变量 --without-http_map_module
Memcached Memcache支持 --without-http_memcached_module
Referer 基于Referer头部信息过滤 --without-http_referer_module
Rewrite 使用正则表达式重写请求 --without-http_rewrite_module
SCGI 支持SGCI协议 --without-http_scgi_module
Upstream 负载均衡 --without-http_upstream_ip_hash_module
接下来的这些模块属于附加模块,编译软件时需要通过configure指定开启:
模块名称 描述 开启选项
Embedded Perl 支持Perl --with-http_perl_module
FLV 支持flash视频 --with-http_flv_module
GeoIP 通过IP变量实现负载均衡 --with-http_geoip_module
Google Perftools 支持谷歌的性能优化工具 --with-google_perftools_module
Gzip Precompression 压缩静态文件 --with-http_gzip_static_module
Image Filter 转换图形的过滤器 --with-http_image_filter_module
MP4 支持MP4 --with-http_mp4_module
Real IP 使用Nginx作为后端服务器 --with-http_realip_module
Secure Link 使用密钥保护页面 --with-http_secure_link_module
SSL 支持HTTPS/SSL --with-http_ssl_module
Stub Status 查看服务器状态 --with-http_stub_status_module
WebDAV 支持WebDAV --with-http_dav_module
邮件模块:
模块名称 描述 启用/禁用选项
Core 邮件代理功能 --with-mail
--without-mail_pop3_module
--without-mail_imap_module
--without-mail_smtp_module
Auth 邮件认证
Proxy 邮件代理
SSL 支持SSL/TLS邮件协议 --with-mail_ssl_module
第三方模块:当需要添加第三方模块时,同样需要在configure时指定需要添加的模块,格式如下。
./configure --add-module=/path/module1 \
--add-module=/path/module2
... ...
在此介绍的模块仅为Nginx常用模块及描述,具体使用方法可期待后期文章... ...
三、控制Nginx服务
1. 启动服务:
[root@centos6 ~]# /usr/local/nginx/sbin/nginx
[root@centos6 ~]# ps aux | grep nginx //查看Nginx进程信息
2. 停止服务:
[root@centos6 ~]# kill -QUIT `cat /usr/local/nginx/logs/nginx.pid`
我们可以通过传输信号给主进程ID号来管理Nginx服务,Nginx主进程ID号默认存储在/usr/local/nginx/logs/nginx.pid文件中,
该文件名可以通过configure修改,或者在Nginx主配置文件中通过pid指定。
Nginx支持以下管理信号:
信号 描述
TERM,INT 快速关闭
QUIT 优雅关闭
HUP 重新加载配置文件(等同于重启)
USR1 重启加载日志文件
USR2 升级Nginx程序
WINCH 优雅地关闭子工作进程
为了重新读取配置文件,HUP信号必须发送给Nginx主进程。
主进程首先检查配置文件语法是否有效,再尝试应用新的配置,这里会重新打开日志文件并建立新的监听套接字,如果这个过程失败Nginx可以回滚使用就配置文件继续工作。
如果成功Nginx会启动新的工作子进程,并发送消息给旧的工作进程(QUIT信号),这样旧的进程停止监听服务但会继续为当前正在进行的连接服务,
当所有的就客户连接终止后,就工作进程被关闭。
3. Nginx本身并未附带启动脚本,本着长久使用的便利性,建议将对Nginx的控制写入脚本。篇幅有些,下一篇博客文章会专门讲解这样的启动脚本。
四、基本配置
以下为Nginx典型主配置文档:/usr/local/nginx/conf/nginx
- #user nobody; //设置用户与组
- worker_processes 1; //启动进程数
- error_log logs/error.log; //错误日志
- pid logs/nginx.pid; //Nginx进程号
- events {
- worker_connections 1024; //每个进程的连接数
- }
- 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"';
- //以上被注释的log_format可以定义自己的日志格式
- #access_log logs/access.log main; //使用log_format定义的main日志格式产生access.log日志文件,注这里默认是被注释的
- sendfile on; //是否调用sendfile()进行数据的拷贝,sendfile()拷贝数据是在内核级别完成,所以会比一般的read,write更高效
- #tcp_nopush on; //开启后服务器的响应头部信息产生独立的数据包发送,即一个响应头信息一个包
- #keepalive_timeout 0; //保持连接的超时时间
- keepalive_timeout 65;
- #gzip on; //是否启用压缩功能,将页面压缩后传输更节省流量
- server { //定义虚拟主机
- listen 80; //服务器监听的端口
- server_name www.jacob.com; //访问域名
- #charset koi8-r; //页面编码,如果网页的编码与charset不同将被自动转码(转码后可能显示为乱码)
- #access_log logs/host.access.log main; //设置服务器www.jacob.com的访问日志
- location / { //匹配URL地址
- root html; //网页根路径,如果Nginx安装在/usr/local/nginx目录下,则网页在/usr/local/nginx/html目录下
- index index.html index.htm; //默认首页
- }
- #error_page 404 /404.html; //错误页面
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- # proxy the PHP scripts to Apache listening on 127.0.0.1:80
- #
- #location ~ \.php$ {
- # proxy_pass http://127.0.0.1;
- #} //以上注释行用来设置代理,当客户端访问php页面时,自动将请求转发给后端服务器
- # 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_index index.php;
- # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
- # include fastcgi_params;
- #} //以上注释行用来设置代理,当客户端访问php页面时,自动将请求转发给后端服务器
- # deny access to .htaccess files, if Apache's document root
- # concurs with nginx's one
- #
- #location ~ /\.ht {
- # deny all; //拒绝所有人访问.ht页面
- #}
- }
- # another virtual host using mix of IP-, name-, and port-based configuration
- #
- #server { //定义虚拟主机(此处设置与上面一个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
- # ssl_certificate cert.pem; //指定证书文件
- # ssl_certificate_key cert.key; //指定私钥文件
- # ssl_session_timeout 5m;
- # ssl_protocols SSLv2 SSLv3 TLSv1;
- # ssl_ciphers HIGH:!aNULL:!MD5;
- # ssl_prefer_server_ciphers on;
- # location / {
- # root html;
- # index index.html index.htm;
- # }
- #}
- }
五、示例(虚拟主机、HTTPS)
注意:实现SSL需要configure时添加--with-http_ssl_module模块
1. 修改配置文件部分内容如下:
- http {
- include mime.types;
- default_type application/octet-stream;
- sendfile on;
- keepalive_timeout 65;
- gzip on;
- server {
- listen 80;
- server_name www.demo.com;
- charset utf-8;
- access_log logs/demo.access.log;
- location / {
- root demo;
- index index.html index.htm;
- }
- }
- # HTTPS server
- server {
- listen 443;
- server_name www.test.comm;
- ssl on;
- ssl_certificate cert.cer; //证书路径同nginx.conf在一个目录
- ssl_certificate_key test.pem; //私钥路径同nginx.conf在一个目录
- ssl_session_timeout 5m;
- ssl_protocols SSLv2 SSLv3 TLSv1;
- ssl_ciphers HIGH:!aNULL:!MD5;
- ssl_prefer_server_ciphers on;
- location / {
- root test;
- index index.html index.htm;
- }
- }
- }
2. 生成证书的方法如下:
[root@centos6 ~] openssl genrsa -out test.pem 2048 //生成私钥
[root@centos6 ~] openssl req -new -x509 -key test.pem -out cert.cer //根据私钥生成根证书
[root@centos6 ~] cp {test.pem,cert,cer} /usr/local/nginx/conf/ //拷贝证书
效果如图:
提示:关于密钥与证书的操作可以参考http://manual.blog.51cto.com/3300438/788691与http://manual.blog.51cto.com/3300438/788681两篇博文 |
2. 创建测试用网页
[root@centos6 ~] mkdir /usr/local/nginx{demo,test}
[root@centos6 ~] cat > /usr/local/nginx/demo/index.html <<EOF
> <html>
> <title>Hello The World</title>
> <body>
> This is a demo page!
> </body>
> </html>
> EOF
[root@centos6 ~] cat > /usr/local/nginx/test/index.html <<EOF
> <html>
> <title>Hello The World</title>
> <body>
> This is a demo page!
> </body>
> </html>
> EOF
3. 重启Nginx
[root@centos6 ~] kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
4. 客户端访问(需要DNS解析或hosts文件解析)
效果如图:
不早了,休息了各位!
本文转自丁丁历险51CTO博客,原文链接:http://blog.51cto.com/manual/948204 ,如需转载请自行联系原作者