码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx

时间:2017-08-01 09:17:56      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:带宽   vim   开源   参数   big   简单   不同的   str   静态   

一、Nginx介绍:

 

Nginx是一款高性能的HTTP和反向代理服务器,能够选择高效的epoll(linux2.6内核)、kqueue(freebsd)、eventport(solaris10)作为网络I/O模型,能够支持高达50000个并发连接数的响应,而内存、CPU等系统资源消耗却非常低、运行非常稳定。

 

2008年12月:Nginx市场占有量为3354329、占有率为1.80%

 

1、选择Nginx的理由

  • 1 支持高并发连接

      通过top命令可以查看系统负载和CPU使用率,由于apache采用的是select网络I/O模型,处理大量连续的读写很低效

  • 内存消耗少

      在服务器3W并发连接下,开启10个Nginx进程消耗150MB内存(15MB*10),开启64个php-cgi进程消耗128MB内存(20MB*64),共消耗不到2GB内存,可以使用webbench做压力测试,运行依旧很快。

  • 成本低廉

      购买F5 BIG-IP、NetScaler等负载均衡交换机需要几十万RMB,而开源Nginx替代这些商业设备。

  •  其他理由

     网络配置简单,对比apache就知道了,支持rewrite重写规则,能够根据域名、URL的不同、将HTTP请求分到不同的后端服务器群组,内置的健康检查功能
     节省带宽,支持GZIP压缩,可以添加浏览器本地缓存的Header头,支持热部署,能够在不间断服务的情况下、对软件版本进行升级

     结论:Nginx在反向代理、rewrite规则、稳定性、静态文件处理、内存消耗等方面都表现出很强的优势。

 

 

安装环境

[root@10 ~]# uname -a
Linux 10.0.0.10 2.6.32-573.el6.x86_64 #1 SMP Thu Jul 23 15:44:03 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
[root@10 ~]# cat /etc/redhat-release
CentOS release 6.7 (Final)

关闭防火墙,非正式环境可以关闭防火墙,便于学习调试Nginx服务。等掌握防火墙技术再开启

[root@10 ~]# /etc/init.d/nginx stop
Stopping nginx:                                            [  OK  ]
[root@10 ~]# /etc/init.d/iptables stop
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]

 

 

yum 安装nginx—1.10

[root@10 ~]# wget http://mirrors.neusoft.edu.cn/epel/6/x86_64/epel-release-6-8.noarch.rpm
--2017-07-31 14:43:09-- http://mirrors.neusoft.edu.cn/epel/6/x86_64/epel-release-6-8.noarch.rpm
Resolving mirrors.neusoft.edu.cn... 219.216.128.25, 2001:da8:a0c4:200::ec43:7f59
Connecting to mirrors.neusoft.edu.cn|219.216.128.25|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 14540 (14K) [application/x-redhat-package-manager]
Saving to: “epel-release-6-8.noarch.rpm”

100%[==================================================================================>] 14,540 28.8K/s in 0.5s

2017-07-31 14:43:11 (28.8 KB/s) - “epel-release-6-8.noarch.rpm” saved [14540/14540]


[root@10 ~]# yum install -y epel-release-6-8.noarch.rpm

Installed:
epel-release.noarch 0:6-8

Complete!

 

[root@10 ~]# yun install -y nginx

Dependency Installed:
GeoIP.x86_64 0:1.6.5-1.el6 GeoIP-GeoLite-data.noarch 0:2017.01-1.el6 
GeoIP-GeoLite-data-extra.noarch 0:2017.01-1.el6 gd.x86_64 0:2.0.35-11.el6 
geoipupdate.x86_64 0:2.2.1-2.el6 libXpm.x86_64 0:3.5.10-2.el6 
nginx-all-modules.noarch 0:1.10.2-1.el6 nginx-filesystem.noarch 0:1.10.2-1.el6 
nginx-mod-http-geoip.x86_64 0:1.10.2-1.el6 nginx-mod-http-image-filter.x86_64 0:1.10.2-1.el6 
nginx-mod-http-perl.x86_64 0:1.10.2-1.el6 nginx-mod-http-xslt-filter.x86_64 0:1.10.2-1.el6 
nginx-mod-mail.x86_64 0:1.10.2-1.el6 nginx-mod-stream.x86_64 0:1.10.2-1.el6


Complete!

安装完成

 

启动Nginx

[root@10 ~]# /etc/init.d/nginx start
Starting nginx:                                            [  OK  ]

查看Nginx服务对应的端口是否启动成功

[root@10 ~]# lsof -i :80
COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   52257  root    6u  IPv4  83444      0t0  TCP *:http (LISTEN)
nginx   52257  root    7u  IPv6  83445      0t0  TCP *:http (LISTEN)
nginx   52259 nginx    6u  IPv4  83444      0t0  TCP *:http (LISTEN)
nginx   52259 nginx    7u  IPv6  83445      0t0  TCP *:http (LISTEN)

也可以通过 netstat -lnt | grep 80 查看

[root@10 ~]# netstat -lnt | grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      
tcp        0      0 :::34880                    :::*                        LISTEN      
tcp        0      0 :::80                       :::*                        LISTEN     

查看Nginx启动的实际效果

打开浏览器输入 http://10.0.0.12(安装Nginx服务器的IP地址),让后回车,如果显示

Welcome to nginx on EPEL!

表示Nginx已经启动成功

 

部署一个Web站点

Nginx 的默认站点目录在 /usr/share/nginx/html/下,如果要部署王者业务数据,只需把开发好的程序全部放到 /usr/share/nginx/html/目录下面即可

进入/usr/share/nginx/html/目录下

[root@10 ~]# cd /usr/share/nginx/html/
[root@10 html]# ll
total 20
-rw-r--r--. 1 root root 3652 Oct 31 2016 404.html
-rw-r--r--. 1 root root 3695 Oct 31 2016 50x.html
-rw-r--r--. 1 root root 3698 Oct 31 2016 index.html
-rw-r--r--. 1 root root 368 Oct 31 2016 nginx-logo.png
-rw-r--r--. 1 root root 2811 Oct 31 2016 poweredby.png

删掉Nginx默认的首页index.html(原始内容为 Welcome to nginx on EPEL)

[root@10 html]# rm -f index.html

然后新建立一个index.html

[root@10 html]# vim index.html

加入以下内容

<html>
<head><title>wanglan,s Nginx server blog .</title></head>
<body>
Hi, I am wanglan
</body>
</html>

打开浏览器输入http://10.0.0.12 让后回车,应该可以看到以下内容

Hi, I am wanglan


Nginx主配置文件 nginx.conf

 

nginx.conf 文件默认在/etc/nginx 目录下

1. 配置基于域名的 nginx.conf 虚拟机主机

进入到nginx目录中

 

[root@10 ~]# cd /etc/nginx/

 

去掉所有的默认注释行空行,生成新文件nginx.conf

 

[root@10 nginx]# egrep -v "#|^$" nginx.conf.default >nginx.conf

 

进入nginx.conf 文件

 

[root@10 ~]# vim /etc/nginx/nginx.conf

 

更改前

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

更改后

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.wanglan.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

上面配置了一个基于www.wanglan.com 域名的站点,这里省略了一切无关的配置参数,虚拟机的关键部分在server{}标签大括号里的内容

2.创建域名对应的站点目录及文件

创建对应站点

[root@10 nginx]# mkdir -p ../html/www

生成一个默认的首页文件index.html,文件内容是 “www.wanglan.com”

[root@10 nginx]# echo "www.wanglan.com" >html/www/index.html 

查看index.html

[root@10 nginx]# cat html/www/index.html 
www.wanglan.com

3.检查语法并从新加载

先检查修改过的Nginx配置文件语法是否正确

[root@10 nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

出现 “syntax is ok”  及 “successful” ,说明语法正确,如果有问题则需要调整配置,直到语法检查正确为止

 

平滑重启Nginx,即重新加载配置文件

[root@10 nginx]# nginx -s reload

再检查Nginx重新加载后的情况,例如进程和端口是否OK

[root@10 nginx]# ps -ef|grep nginx
root 52257 1 0 15:10 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 52397 52257 0 17:19 ? 00:00:00 nginx: worker process
root 52404 2134 0 17:22 pts/1 00:00:00 grep nginx

[root@10 nginx]# netstat -lntp|grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      52257/nginx         
tcp        0      0 :::34880                    :::*                        LISTEN      1308/rpc.statd   

在Windows客户端测试域名站点配置的访问结果

在客户端做hosts解析

win+ R 键,在运行窗口输入 %systemroot%\system32\drivers\etc\hosts ,选择记事本打开

 

Nginx

标签:带宽   vim   开源   参数   big   简单   不同的   str   静态   

原文地址:http://www.cnblogs.com/wanglan/p/7266647.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!