您的位置:首页 > 运维架构 > Nginx

nginx的安装及虚拟主机配置

2012-09-01 14:57 537 查看
一.nginx简介

Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。

二.nginx的优点

Nginx 可以在大多数 Unix like OS 上编译运行,并有 Windows 移植版。目前 Nginx 的1.0.9稳定版已经于2011年11月1日发布,开发版本已完成的为1.3.0,如果新建站点,建议使用最新稳定版作为生产版本,已有站点升级急迫性不高。 Nginx 的源代码使用 2-clause BSD-like license。

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

在高连接并发的情况下,Nginx是Apache服务器不错的替代品:Nginx在美国是做虚拟主机生意的老板们经常选择的软件平台之一。能够支持高达 50,000 个并发连接数的响应,感谢Nginx为我们选择了 epoll and kqueue作为开发模型。

Nginx作为负载均衡服务器:Nginx 既可以在内部直接支持 Rails 和 PHP 程序对外进行服务,也可以支持作为 HTTP代理服务器对外进行服务。Nginx采用C进行编写,不论是系统资源开销还是CPU使用效率都比 Perlbal 要好很多。

作为邮件代理服务器:Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last. fm 描述了成功并且美妙的使用经验。

Nginx 是一个安装非常的简单,配置文件非常简洁(还能够支持perl语法),Bugs非常少的服务器:Nginx 启动特别容易,并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动。你还能够不间断服务的情况下进行软件版本的升级。

三.nginx结构与扩展

一个主进程和多个工作进程。工作进程是单线程的,且不需要特殊授权即可运行;

kqueue (FreeBSD 4.1+), epoll (Linux 2.6+), rt signals (Linux 2.2.19+), /dev/poll (Solaris 7 11/99+), select, 以及 poll 支持;

kqueue支持的不同功能包括 EV_CLEAR, EV_DISABLE (临时禁止事件), NOTE_LOWAT, EV_EOF, 有效数据的数目,错误代码;

sendfile (FreeBSD 3.1+), sendfile (Linux 2.2+), sendfile64 (Linux 2.4.21+), 和 sendfilev (Solaris 8 7/01+) 支持;

输入过滤 (FreeBSD 4.1+) 以及 TCP_DEFER_ACCEPT (Linux 2.4+) 支持;

10,000 非活动的 HTTP keep-alive 连接仅需要 2.5M 内存

最小化的数据拷贝操作;

其他HTTP功能:

基于IP 和名称的虚拟主机服务;

Memcached 的 GET 接口;

支持 keep-alive 和管道连接;

灵活简单的配置;

重新配置和在线升级而无须中断客户的工作进程;

可定制的访问日志,日志写入缓存,以及快捷的日志回卷;

4xx-5xx 错误代码重定向;

基于 PCRE 的 rewrite 重写模块

基于客户端IP 地址和 HTTP 基本认证的访问控制;

PUT, DELETE, 和 MKCOL 方法;

支持 FLV (Flash 视频);

带宽限制;

四.源码编译的一些参数

configure 脚本确定系统所具有一些特性,特别是 nginx 用来处理连接的方法。然后,它创建 Makefile 文件。

configure 支持下面的选项:

--prefix=<path> - Nginx安装路径。如果没有指定,默认为 /usr/local/nginx。

--sbin-path=<path> - Nginx可执行文件安装路径。只能安装时指定,如果没有指定,默认为<prefix>/sbin/nginx。

--conf-path=<path> - 在没有给定-c选项下默认的nginx.conf的路径。如果没有指定,默认为<prefix>/conf/nginx.conf。

--pid-path=<path> - 在nginx.conf中没有指定pid指令的情况下,默认的nginx.pid的路径。如果没有指定,默认为 <prefix>/logs/nginx.pid。

--lock-path=<path> - nginx.lock文件的路径。

--error-log-path=<path> - 在nginx.conf中没有指定error_log指令的情况下,默认的错误日志的路径。如果没有指定,默认为 <prefix>/logs/error.log。

--http-log-path=<path> - 在nginx.conf中没有指定access_log指令的情况下,默认的访问日志的路径。如果没有指定,默认为 <prefix>/logs/access.log。

--user=<user> - 在nginx.conf中没有指定user指令的情况下,默认的nginx使用的用户。如果没有指定,默认为 nobody。

--group=<group> - 在nginx.conf中没有指定user指令的情况下,默认的nginx使用的组。如果没有指定,默认为 nobody。

--builddir=DIR - 指定编译的目录

--with-rtsig_module - 启用 rtsig 模块

--with-select_module --without-select_module - Whether or not to enable the select module. This module is enabled by default if a more suitable method such as kqueue, epoll, rtsig or /dev/poll is not discovered by configure.

//允许或不允许开启SELECT模式,如果 configure 没有找到更合适的模式,比如:kqueue(sun os),epoll (linux kenel 2.6+), rtsig(实时信号)或者/dev/poll(一种类似select的模式,底层实现与SELECT基本相 同,都是采用轮训方法) SELECT模式将是默认安装模式

--with-poll_module --without-poll_module - Whether or not to enable the poll module. This module is enabled by default if a more suitable method such as kqueue, epoll, rtsig or /dev/poll is not discovered by configure.

--with-http_ssl_module - Enable ngx_http_ssl_module. Enables SSL support and the ability to handle HTTPS requests. Requires OpenSSL. On Debian, this is libssl-dev.

//开启HTTP SSL模块,使NGINX可以支持HTTPS请求。这个模块需要已经安装了OPENSSL,在DEBIAN上是libssl

--with-http_realip_module - 启用 ngx_http_realip_module

--with-http_addition_module - 启用 ngx_http_addition_module

--with-http_sub_module - 启用 ngx_http_sub_module

--with-http_dav_module - 启用 ngx_http_dav_module

--with-http_flv_module - 启用 ngx_http_flv_module

--with-http_stub_status_module - 启用 "server status" 页

--without-http_charset_module - 禁用 ngx_http_charset_module

--without-http_gzip_module - 禁用 ngx_http_gzip_module. 如果启用,需要 zlib 。

--without-http_ssi_module - 禁用 ngx_http_ssi_module

--without-http_userid_module - 禁用 ngx_http_userid_module

--without-http_access_module - 禁用 ngx_http_access_module

--without-http_auth_basic_module - 禁用 ngx_http_auth_basic_module

--without-http_autoindex_module - 禁用 ngx_http_autoindex_module

--without-http_geo_module - 禁用 ngx_http_geo_module

--without-http_map_module - 禁用 ngx_http_map_module

--without-http_referer_module - 禁用 ngx_http_referer_module

--without-http_rewrite_module - 禁用 ngx_http_rewrite_module. 如果启用需要 PCRE 。

--without-http_proxy_module - 禁用 ngx_http_proxy_module

--without-http_fastcgi_module - 禁用 ngx_http_fastcgi_module

--without-http_memcached_module - 禁用 ngx_http_memcached_module

--without-http_limit_zone_module - 禁用 ngx_http_limit_zone_module

--without-http_empty_gif_module - 禁用 ngx_http_empty_gif_module

--without-http_browser_module - 禁用 ngx_http_browser_module

--without-http_upstream_ip_hash_module - 禁用 ngx_http_upstream_ip_hash_module

--with-http_perl_module - 启用 ngx_http_perl_module

--with-perl_modules_path=PATH - 指定 perl 模块的路径

--with-perl=PATH - 指定 perl 执行文件的路径

--http-log-path=PATH - Set path to the http access log

--http-client-body-temp-path=PATH - Set path to the http client request body temporary files

--http-proxy-temp-path=PATH - Set path to the http proxy temporary files

--http-fastcgi-temp-path=PATH - Set path to the http fastcgi temporary files

--without-http - 禁用 HTTP server

--with-mail - 启用 IMAP4/POP3/SMTP 代理模块

--with-mail_ssl_module - 启用 ngx_mail_ssl_module

--with-cc=PATH - 指定 C 编译器的路径

--with-cpp=PATH - 指定 C 预处理器的路径

--with-cc-opt=OPTIONS - Additional parameters which will be added to the variable CFLAGS. With the use of the system library PCRE in FreeBSD, it is necessary to indicate --with-cc-opt="-I /usr/local/include". If we are using select() and it is necessary to increase the number of file descriptors, then this also can be assigned here: --with-cc-opt="-D FD_SETSIZE=2048".

--with-ld-opt=OPTIONS - Additional parameters passed to the linker. With the use of the system library PCRE in FreeBSD, it is necessary to indicate --with-ld-opt="-L /usr/local/lib".

--with-cpu-opt=CPU - 为特定的 CPU 编译,有效的值包括:pentium, pentiumpro, pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64

--without-pcre - 禁止 PCRE 库的使用。同时也会禁止 HTTP rewrite 模块。在 "location" 配置指令中的正则表达式也需要 PCRE 。

--with-pcre=DIR - 指定 PCRE 库的源代码的路径。

--with-pcre-opt=OPTIONS - Set additional options for PCRE building.

--with-md5=DIR - Set path to md5 library sources.

--with-md5-opt=OPTIONS - Set additional options for md5 building.

--with-md5-asm - Use md5 assembler sources.

--with-sha1=DIR - Set path to sha1 library sources.

--with-sha1-opt=OPTIONS - Set additional options for sha1 building.

--with-sha1-asm - Use sha1 assembler sources.

--with-zlib=DIR - Set path to zlib library sources.

--with-zlib-opt=OPTIONS - Set additional options for zlib building.

--with-zlib-asm=CPU - Use zlib assembler sources optimized for specified CPU, valid values are: pentium, pentiumpro

--with-openssl=DIR - Set path to OpenSSL library sources

--with-openssl-opt=OPTIONS - Set additional options for OpenSSL building

--with-debug - 启用调试日志

--add-module=PATH - Add in a third-party module found in directory PATH

在不同版本间,选项可能会有些许变化,请总是使用 ./configure --help 命令来检查一下当前的选项列表。

五.案例简介

1.案例原理

源码安装nginx 提供web 服务

2.案例环境

vmware 8.0.0 虚拟机模拟软件。redhat 5.4 操作系统。

一块网卡ip地址为192.168.10.244/24

使用host only方式连接。

修改物理机地址解析文件“hosts” 如下图:





3.案例目的

源码安装nginx 提供web 服务

六.安装nginx服务器,进行安装nginx前的环境准备

1.安装pcre和pcre-devel (yum安装参考/article/4548518.html

yum install pcre pcre-devl //用于url转发

2.升级时间触发器

①.将源码的压缩包加压到指定的“/usr/local/src” 下

[root@donghao244 ~]# tar -zxvf libevent-2.0.16-stable.tar.gz -C /usr/local/src/

[root@donghao244 libevent-2.0.16-stable]# make

[root@donghao244 libevent-2.0.16-stable]# make install

Installation directories:

--prefix=PREFIX install architecture-independent files in PREFIX

[/usr/local] //默认的安装主目录

[root@donghao244 libevent-2.0.16-stable]# ./configure

[root@donghao244 libevent-2.0.16-stable]# make

[root@donghao244 libevent-2.0.16-stable]# make install

七.安装nginx源码包

1.新建nginx账号及组

[root@donghao244 nginx-1.0.11]# groupadd -r nginx

[root@donghao244 nginx-1.0.11]# useradd -r -g nginx -s /bin/false -M nginx

2.检测预编译环境

[root@donghao244 nginx-1.0.11]# ./configure \

--prefix=/usr \ //Nginx安装路径。如果没有指定,默认为 /usr/local/nginx。

--sbin-path=/usr/sbin/nginx \ //Nginx可执行文件安装路径。只能安装时指定,如果没有指定,默认为<prefix>/sbin/nginx。

--conf-path=/etc/nginx/nginx.conf \ //在没有给定-c选项下默认的nginx.conf的路径。如果没有指定,默认为<prefix>/conf/nginx.conf。

--error-log-path=/var/log/ngix/error.log \ //在nginx.conf中没有指定error_log指令的情况下,默认的错误日志的路径。如果没有指定,默认为 <prefix>/logs/error.log。

--http-log-path=/var/log/ngix/access.log \ //在nginx.conf中没有指定access_log指令的情况下,默认的访问日志的路径。如果没有指定,默认为 <prefix>/logs/access.log。

--pid-path=/var/run/nginx/nginx.pid \ //

--lock-path=/var/lock/nginx.lock \ //nginx.lock文件的路径。

--user=nginx \ //在nginx.conf中没有指定user指令的情况下,默认的nginx使用的用户。如果没有指定,默认为 nobody。

--group=nginx \ //在nginx.conf中没有指定user指令的情况下,默认的nginx使用的用户。如果没有指定,默认为 nobody。

--with-http_ssl_module \ ////开启HTTP SSL模块,使NGINX可以支持HTTPS请求。这个模块需要已经安装了OPENSSL,在DEBIAN上是libssl

--with-http_flv_module \ //启用 ngx_http_flv_module

--with-http_mp4_module \ //启用 ngx_http_mp4_module

--with-http_stub_status_module \ //启用 "server status" 页

--with-http_gzip_static_module \ //启用ngx_http_gzip_module. 如果启用,需要 zlib 。

--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/ \ //临时文件

--with-pcre //添加url重定向

3.编译及安装

[root@donghao244 nginx-1.0.11]#make //编译

[root@donghao244 nginx-1.0.11]#make install //安装

安装后产生的信息

nginx path prefix: "/usr/local/nginx"

nginx binary file: "/usr/sbin/nginx"

nginx configuration prefix: "/etc/nginx"

nginx configuration file: "/etc/nginx/nginx.conf"

nginx pid file: "/var/run/nginx/nginx.pid"

nginx error log file: "/var/log/ngix/error.log"

nginx http access log file: "/var/log/ngix/access.log"

nginx http client request body temporary files: "/var/tmp/nginx/client/"

nginx http proxy temporary files: "/var/tmp/nginx/proxy/"

nginx http fastcgi temporary files: "/var/tmp/nginx/fcgi/"

nginx http uwsgi temporary files: "uwsgi_temp"

nginx http scgi temporary files: "scgi_temp"

4.启动 nginx并测试

[root@donghao244 bin]#nginx

测试nginx





安装成功!!!

八.基于主机头的虚拟主机

1.修改配置文件

[root@donghao244 nginx]# vim /etc/nginx/nginx.conf

配置如下:

35 server {

36 listen 80; //监听80 端口

37 server_name www.abc.com; //站点的名字

38 error_log /var/log/www-error.log; //错误日志

39 access_log /var/log/www-bbs-acess.log; //访问日志

45 location / { //指定根目录下的所有目录均可以访问

46 root html; //指定根目录的名字

47 index index.html index.htm; //站点默认打开的主页文件

119 server {

120 listen 80; //监听80 端口

121 server_name bbs.cj.com;//站点的名字

122 error_log /var/log/bbs-error.log;//错误日志

123 access_log /var/log/bbs-access.log;//访问日志

124 location /{//指定根目录下的所有目录均可以访问

125 root bbs;//指定根目录的名字

126 index index.html index.htm;//站点默认打开的主页文件

127 }

128 }

129 server {

130 listen 80;//监听80 端口

131 server_name phpwind.cj.com;//站点的名字

132 access_log /var/log/phpwind-access.log;//错误日志

133 error_log /var/log/phpwind-error.log;//访问日志

134 location /{指定根目录下的所有目录均可以访问

135 root phpwind;//指定根目录的名字

136 index index.html index.htm;//站点默认打开的主页文件

137 }

138 }

139 }

2.编辑创建站点主目录

[root@donghao244 bin]#cd /usr/local/nginx

[root@donghao244 nginx]#mkdir -p pps phpwind

[root@donghao244 nginx]# echo "bbs">>bbs/index.html

[root@donghao244 nginx]# echo "phpwind">>phpwind/index.html

[root@donghao244 nginx]# pkill -1 nginx

[root@donghao244 nginx]# nginx

九.测试如下

访问默认主站点





访问bbs站点





访问phpwind站点





这时简单的基于主机头的虚拟主机已经可以正常提供服务了。

嗯,韩宇数的对呀!!!
本文出自 “交流群“143420878”” 博客,请务必保留此出处http://donghao123.blog.51cto.com/4879169/979400
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: