Nginx专为性能优化而开发,其最大的优势就是它的稳定性和低系统资源消耗,以及对http并发链接的高处理能力,单台物理服务器可支持20000~50000个并发请求,正是如此,大量提供社交网络、新闻资讯、电子商务及虚拟主机等服务的企业纷纷选择Nginx来提供web服务,目前中国大陆使用nginx网站用户有:新浪、网易、腾讯,另外知名的微网志Plurk也使用nginx。php
Nginx是一个很牛的高性能Web和反向代理服务器,它具备有不少很是优越的特性:html
- 高并发链接:官方测试能支撑5万并发链接,在实际生产环境中跑到2,~3W并发链接。
- 内存消耗少:在3W并发链接下,开启的10个NGINX进程才消耗150M内存(15M*10=150M)
- 配置文件很是简单:风格跟程序同样通俗易懂。
- 成本低廉:Nginx做为开源软件,能够无偿使用,而购买F5 BIG-IP、NetScaler等硬件负载均衡交换机则须要十多万至几十万人民币。
- 支持rewrite重写规则:可以根据域名、URL的不一样,将HTTP请求分发到不一样的后端服务器群组。
内置的健康检查功能:若是Nginx Proxy后端的后台web服务器宕机了,不会影响前端访问。- 节省带宽:支持GZIP压缩,能够添加浏览器本地缓存的Header头。
- 稳定性高:用于反向代理,宕机的几率微乎其微。
啰嗦了一堆,只是想将Nginx的优势说出来,下面开始搭建Nginx网站服务器:前端
1、准备工做:nginx
2、开始搭建Nginx网站(挂载系统盘,安装所需的依赖包。):web
一、安装所需依赖包,均由系统盘提供:vim
[root@localhost yum.repos.d]# yum -y erase httpd #卸载系统自带httpd服务。 [root@localhost yum.repos.d]# yum -y install pcre-devel zlib-devel #搭建Nginx网站所需依赖包
二、编译安装及配置优化Nginx(其安装文件能够从官网 https://nginx.org/ 或我上面提供的连接地址下载使用):后端
[root@localhost media]# useradd -M -s /sbin/nologin nginx #建立系统用户 [root@localhost media]# tar zxf nginx-1.12.0.tar.gz -C /usr/src #解包 [root@localhost media]# cd /usr/src/nginx-1.12.0/ [root@localhost nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install #编译安装Nginx [root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #建立主程序的连接文件 [root@localhost ~]# vim /etc/init.d/nginx #编辑服务脚本 #!/bin/bash # chkconfig: - 99 20 PROG="/usr/local/nginx/sbin/nginx" PIDF="/usr/local/nginx/logs/nginx.pid" case "$1" in start) $PROG ;; stop) kill -s QUIT $(cat $PIDF) ;; restart) $0 stop $0 start ;; reload) kill -s HUP $(cat $PIDF) ;; *) echo "USAGE:$0 {start | stop | restart | reload}" exit 1 esac exit 0 [root@localhost ~]# chmod +x /etc/init.d/nginx #添加执行权限 [root@localhost ~]# chkconfig --add nginx #添加为系统服务 [root@localhost ~]# systemctl start nginx #启动Nginx服务,以确认脚本的正常运行 [root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf #调整配置文件,以优化web服务 .............. worker_processes 2; #工做进程数 #error_log logs/error.log; #错误日志文件位置 #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; #PID文件的位置 events { use epoll; #在even{ }中添加该行以提升性能 worker_connections 4096; 每一个进程处理4096个链接 }
以上的优化是基于全局配置实施的,各项优化的含义以下:centos
worker_processes :表示工做进程的数量,若服务器由多块CPU或者使用多核处理器,能够参考CPU核心总数来指定工做进程数。具体含义在worker_connections配置项中体现出来,浏览器
三、搭建基于域名的虚拟web主机:缓存
一、HTTP配置:
Nginx的配置文件使用“http { }”界定标记用于设定HTTP服务器,包括访问日志、http端口、网页目录、默认字符集、链接保持,以及虚拟web主机、php解析等网站全局设置,其中大部分包含在子界定标记 “ server { }”内。“ server { }”表明一个具体的网站设置。
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf 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; server { listen 80; #web服务器监听端口,可使用“ip地址:端口”的形式 server_name www.test1.com; #网站域名 charset utf-8; #网站默认字符集,须去掉前面的“#”号 access_log logs/test1.access.log main; # 访问日志文件名 location /status { #添加 location /status 以便开启状态统计,访问位置为/status stub_status on; #打开状态统计功能 access_log off; #关闭此位置的日志记录 } location / { root /var/www/test1; #网站根目录 index index.html index.php; #默认首页,改成index.php以便支持php网页 } ; .......................... error_page 500 502 503 504 /50x.html; #内部错误的反馈页面 location = /50x.html { #错误页面配置 root html; } } }
以上配置只是搭建了一个网站服务,若想运行多个,可复制配置文件最后面提供的模板,粘贴到 “server{ } ”配置上面,由于在配置文件中有太多的 “ { }”,为了不错误,因此才需复制到原有的 “server{ } ”之上,以下:
server { listen 80; server_name www.test2.com; charset utf-8; access_log logs/test2.access.log main; location /status { stub_status on; access_log off; } location / { root /var/www/test2; index index.html index.php; } } server { listen 80; server_name www.test1.com; ...........................
至此,虚拟主机搭建已经完成,需重启服务,以服务生效,来验证web服务器的正常运行(DNS需自行设置,可参考博文:http://www.javashuo.com/article/p-urpobsed-eo.html ):
[root@localhost ~]# nginx -t #重启服务前使用该命令检查配置文件, #若配置文件有错,会提示错在第几行, #若没错,则显示OK,有错误的话,重启服务不会报错,但配置文件不生效。 nginx: [emerg] unexpected ";" in /usr/local/nginx/conf/nginx.conf:44 #表示第44行有错误 nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed [root@localhost ~]# nginx -t #如下显示ok,表示没问题。 nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@localhost named]# mkdir -p /var/www/test1 [root@localhost named]# mkdir -p /var/www/test2 [root@localhost named]# echo "www.test1.com" > /var/www/test1/index.html [root@localhost named]# echo "www.test2.com" > /var/www/test2/index.html
客户机验证:
①访问www.test1.com 的首页:
②访问www.test1.com 的状态统计页:
上述含义以下:
齐活。。。。。。。。。。。。。。。