【php精品源码栏目提醒】:网学会员鉴于大家对php精品源码十分关注,论文会员在此为大家搜集整理了“nginx安装与配置 - 其它资料”一文,供大家参考学习
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 可以在大多数 Unix like OS 上编译运行,并有 Windows 移植版。
目前 Nginx 的1.0.9 稳定版已经于 2011 年 11 月 1 日发布开发版本已完成的为 1.3.0,如果新建站点,建议使用最新稳定版作为生产版本,已有站点升级急迫性不高。
Nginx 的源代码使用 2-clauseBSD-like license。
Nginx 是一个很牛的高性能 Web 和反向代理服务器,它具有很多非常优越的特性:在高连接并发的情况下,Nginx 是 Apache 服务器不错的替代品:Nginx 在美国是做虚拟主机生意的老板们经常选择的软件平台之一。
能够支持高达 50000 个并发连接数的响应,感谢 Nginx 为我们选择了 epoll and kqueue 作为开发模型。
Nginx 作为负载均衡服务器:Nginx 既可以在内部直接支持 Rails 和
PHP 程序对外进行服务,也可以支持作为 HTTP 代理服务器对外进行服务。
Nginx 采用 C 进行编写,不论是系统资源开销还是 CPU 使用效率都比 Perlbal 要好很多。
作为邮件代理服务器: Nginx 同时也是一个非常优秀的邮件代理服务器 (最早开发这个产品的目的之一也是作为邮件代理服务器) ,Last. fm 描述了成功并且美妙的使用经验。
Nginx 是一个安装非常的简单,配置文件非常简洁(还能够支持 perl 语法) ,Bugs 非常少的服务器:Nginx 启动特别容易,并且几乎可以做到 724 不间断运行,即使运行数个月也不需要重新启动。
你还能够不间断服务的情况下进行软件版本的升级。
安装tar -zxvf nginx-0.8.33.tar.gzcd nginx-0.8.33./configure --prefix/usr/local/nginx(但请把—prefix 加上。
这里以安装 supersparrow-0.0.0 为例,我们打算把它安装到目录/usr/local/supersparrow 于 是 在 supersparrow-0.0.0 目 录 执 行 带 选 项 的 脚 本 ./configure--prefix/usr/local/supersparrow执行成功后再编译、安装(make,make install) ;安装完成将自动生成目录 supersparrow而且该软件所有的文件都被复制到这个目录。
为什么要指定这个安装目录?是为了以后的维护方便,如果没有用这个选项,安装过程结束后,该软件所需的软件被复制到不同的系统目录下, 很难弄清楚到底复制了那些文件、 都复制到哪里去了—基本上是一塌糊涂。
)安装 Nginx 时报错./configure: error: the HTTP rewrite module requires the PCRE library.安装 pcre-devel 解决问题yum -y install pcre-devel错误提示:./configure: error: the HTTP cache module requires md5 functionsfrom OpenSSL library. You can either disable the module by using--without-http-cache option or install the OpenSSL library into the systemor build the OpenSSL library statically from the source with nginx by using--with-http_ssl_module --with-openssl options.解决办法:yum -y install openssl openssl-devel总结:yum -y install pcre-devel openssl openssl-devel./configure --prefix/usr/local/nginxmakemake install一切搞定nginx 这个轻量级、高性能的 web server 主要可以干两件事情:〉直接作为 http server代替 apache,对
PHP 需要 FastCGI 处理器支持;〉另外一个功能就是作为反向代理服务器实现负载均衡以下我们就来举例说明如何使用 nginx 实现负载均衡。
因为 nginx 在处理并发方面的优势,现在这个应用非常常见。
当然了 Apache 的 mod_proxy 和 mod_cache 结合使用也可以实现对多台 app server 的反向代理和负载均衡,但是在并发处理方面 apache 还是没有 nginx 擅长。
反向代理和负载均衡1环境:a. 我们本地是 Windows 系统,然后使用 VirutalBox 安装一个虚拟的 Linux 系统。
在本地的 Windows 系统上分别安装 nginx侦听 8080 端口和 apache侦听 80 端口。
在虚拟的 Linux 系统上安装 apache侦听 80 端口。
这样我们相当于拥有了 1 台 nginx 在前端作为反向代理服务器;后面有 2 台 apache 作为应用程序服务器可以看作是小型的 server cluster。
;b. nginx 用来作为反向代理服务器,放置到两台 apache 之前,作为用户访问的入口;nginx 仅仅处理静态页面,动态的页面
php 请求统统都交付给后台的两台 apache 来处理。
也就是说,可以把我们网站的静态页面或者文件放置到 nginx 的目录下;动态的页面和数据库访问都保留到后台的 apache 服务器上。
c. 如下介绍两种方法实现 server cluster 的负载均衡。
我们假设前端 nginx为 127.0.0.1:80仅仅包含一个静态页面 index.html;后台的两个 apache 服务器分别为 localhost:80 和 158.37.70.143:80,一台根目录放置phpMyAdmin 文件夹和 test.
php里面测试代码为 print “server1“,另一台根目录仅仅放置一个 test.
php里面测试代码为 print “server2“。
2针对不同请求 的负载均衡:a. 在最简单地构建反向代理的时候 nginx 仅仅处理静态不处理动态内容,动态内容交给后台的 apache server 来处理,我们具体的设置为:在 nginx.conf 中修改:复制代码 代码如下:目录地址:/usr/local/nginx/conflocation .
php proxy_pass 158.37.70.143:80 〉 这样当客户端访问 localhost:8080/index.html 的时候,前端的 nginx 会自动进行响应;〉当用户访问 localhost:8080/test.
php 的时候这个时候 nginx 目录下根本就没有该文件,但是通过上面的设置 location .
php表示正则表达式匹配以.
php 结尾的文件,详情参看location 是如何定义和匹配的 http://wiki.nginx.org/NginxHttpCoreModule ,nginx 服务器会自动 pass 给 158.37.70.143 的 apache 服务器了。
该服务器下的 test.
php 就会被自动解析,然后将 html 的结果页面返回给 nginx,然后 nginx 进行显示如果 nginx 使用 memcached 模块或者 squid 还可以支持缓存,输出结果为打印 server2。
如上是最为简单的使用 nginx 做为反向代理服务器的例子;b. 我们现在对如上例子进行扩展,使其支持如上的两台服务器。
我们设置 nginx.conf 的 server 模块部分,将对应部分修改为:复制代码 代码如下:location /phpMyAdmin/ proxy_pass 127.0.0.1:80 location .
php proxy_pass 158.37.70.143:80 上面第一个部分 location /phpMyAdmin/, 表示不使用正则表达式匹配,而是直接匹配,也就是如果客户端访问的 URL 是以 http://localhost:8080/phpMyAdmin/ 开头的话本地的nginx 目录下根本没有 phpMyAdmin 目录,nginx 会自动 pass 到 127.0.0.1:80 的 Apache 服务器,该服务器对 phpMyAdmin 目录下的页面进行解析,然后将结果发送给 nginx,后者显示;如果客户端访问 URL 是 http://localhost/test.
php 的话,则会被 pass 到 158.37.70.143:80 的apache 进行处理。
因此综上,我们实现了针对不同请求的负载均衡。
〉如果用户访问静态页面 index.html,最前端的 nginx 直接进行响应;〉如果用户访问 test.
php 页面的话,158.37.70.143:80 的 Apache 进行响应;〉如果用户访问目录 phpMyAdmin 下的页面的话,127.0.0.1:80 的 Apache 进行响应;3访问同一页面 的负载均衡:即用户访问 http://localhost:8080/test.
php 这个同一页面的时候,我们实现两台服务器的负载均衡 实际情况中, 这两个服务器上的数据要求同步一致, 这里我们分别定义了打印 server1和 server2 是为了进行辨认区别。
a. 现在我们的情况是在 windows 下 nginx 是 localhost 侦听 8080 端口;两台 apache,一台是 127.0.0.1:80包含 test.
php 页面但是打印 server1,另一台是虚拟机的158.37.70.143:80包含 test.
php 页面但是打印 server2。
b. 因此重新配置 nginx.conf 为:〉首先在 nginx 的配置文件 nginx.conf 的 http 模块中添加,服务器集群 server cluster我们这里是两台的定义:复制代码 代码如下:upstream myCluster server 127.0.0.1:80 server 158.37.70.143:80 表示这个 server cluster 包含 2 台服务器〉然后在 server 模块中定义,负载均衡:复制代码 代码如下:location .
php proxy_pass http://myCluster 这里的名字和上面的 cluster 的名字相同proxy_redirect offproxy_set_header Host hostproxy_set_header X-Real-IP remote_addrproxy_set_header X-Forwarded-For proxy_add_x_forwarded_for这样的话,如果访问 http://localhost:8080/test.
php 页面的话,nginx 目录下根本没有该文件,但是它会自动将其 pass 到 myCluster 定义的服务区机群中,分别由 127.0.0.1:80或者158.37.70.143:80来做处理。
上面在定义 upstream 的时候每个 server 之后没有定义权重,表示两者均衡;如果希望某个更多响应的话例如:复制代码 代码如下:upstream myCluster server 127.0.0.1:80 weight5server 158.37.70.143:80 这样表示 5/6 的几率访问第一个 server1/6 访问第二个。
另外还可以定义 max_fails 和fail_timeout 等参数。
http://wiki.nginx.org/NginxHttpUpstreamModule综上, 我们使用 nginx 的反向代理服务器 reverse proxy server 的功能,将其布置到多台 apacheserver 的前端。
nginx 仅仅用来处理静态页面响应和动态请求的代理 pass,后台的 apache server 作为 appserver 来对前台 pass 过来的动态页面进行处理并返回给 nginx。
通过以上的架构,我们可以实现 nginx 和多台 apache 构成的机群 cluster 的负载均衡。
两种均衡:1可以在 nginx 中定义访问不同的内容,代理到不同的后台 server; 如上例子中的访问phpMyAdmin 目录代理到第一台 server 上;访问 test.
php 代理到第二台 server 上;2可以在 nginx 中定义访问同一页面, 均衡 当然如果服务器性能不同可以定义权重来均衡地代理到不同的后台 server 上。
如上的例子访问 test.
php 页面,会均衡地代理到 server1 或者 server2 上。
实际应用中,server1 和 server2 上分别保留相同的 app 程序和数据,需要考虑两者的数据同步。
启动及重启启动:cd /usr/local/nginx/sbin./niginx停止: niginx -s stop配置文件说明:vim /usr/local/nginx/conf/nginx.confuser nginx 用户使用 nginx worker_processes 1 程序启动后,初始化的进程数;内核多的话,可根据内核数量设置。
error_log logs/error.log error_log logs/error.log notice error_log logs/error.log info 设置错误日志的记录级别pid logs/nginx.pid 重启关闭 nginx 时,需要知道 pid 文件。
worker_rlimit_nofile 51000 events worker_connections 50000 允许的连接数,我这里设置 50000 个。
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 30 gzip on 允许 gzip 压缩。
gzip_buffers 32 4k gzip_comp_level 1 gzip_min_length 1024 gzip_types text/html text/css application/xml server listen 80 server_name localhost charset koi8-r access_log logs/host.access.log main location / root html 目录 index index.html index.htm index 类型 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 这里是关于转发的部分 proxy the
PHP scripts to Apache listening on 127.0.0.1:80 location .
php proxy_pass http://127.0.0.1 fastcgi 的部分 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 /scriptsfastcgi_script_name include fastcgi_params deny access to .htaccess files if Apaches document root concurs with nginxs one location /.ht deny all another virtual host using mix of IP- name- and port-based configuration 虚拟主机的部分 现在就配置一台主机改了以下内容: server listen 80 server_name svr3.labexam.com 10.1.1.30 charset koi8-r access_log logs/w1.access.log combined location / root /var/www/w1 index index.html index.htm 看了下日志 rootsvr3 nginx tail /usr/local/nginx/logs/w1.access.log 10.1.1.8 - - 15/Aug/2011:02:21:50 0800 GET / HTTP/1.1 200 8 - Opera/9.80 WindowsNT 5.1 U zh-cn Presto/2.9.168 Version/11.50 10.1.1.8 - - 15/Aug/2011:02:21:50 0800 GET /favicon.ico HTTP/1.1 404 142http://svr3.labexam.com/ Opera/9.80 Windows NT 5.1 U zh-cn Presto/2.9.168Version/11.50 如何启动 nginx方 法 1 、 执 行 /usr/local/nginx/sbin/nginx -t 检 查 配 置 文 件 是 否 有 误 ! 或 是 直 接 执 行/usr/local/nginx/sbin/nginx如果有多个配置文件可以使用指定的配置文件启动: /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf nginx 的信号控制:TERMINT 快速关闭QUIT 从容关闭HUP 重启,重新加载配置文件USR1 重启打开日志,在切割日志时用途大USR2 平滑升级可执行程序WINCH 从容关闭进程
上一篇:
织梦建站详细教程
下一篇:
论“十二五”期间的行政体制改革