赞
踩
目录
修改nginx.conf 配置文件,指定访问位置并添加 stub_status配置
Nginx是一个高性能的HTTP和反向代理服务器。
是一款轻量级的web服务器/反向代理服务器/电子邮件(IMAP/POP3)代理服务器
单台物理服务器可支持30 000~50 000个并发请求。
I/O在计算机中指Input/Output,lOPS (Input/Output Per Second)即每秒的输入输出量(或读写次数),是衡量磁盘性能的主要指标之一。IOPS是指单位时间内系统能处理的I/O请求数量,一般以每秒处理的IO请求数量为单位,I/O请求通常为读或写数据操作请求。
一次完整的I/O是用户空间的进程数据与内核空间的内核数据的报文的完整交换,但是由于内核空间与用户空间是严格隔离的,所以其数据交换过程中不能由用户空间的进程直接调用内核空间的内存数据,而是需要经历一次从内核空间中的内存数据copy到用户空间的进程内存当中,所以简单说I/O就是把数据从内核空间中的内存数据复制到用户空间中进程的内存当中。
磁盘I/O:buff/cache 的区别
网络I/O:一切皆文件,本质为对socket文件的读写
buff(读)cache(写)
网络I/O
获取请求数据,客户端与服务器建立连接发出请求,服务器接受请求(1-3)
构建响应,当服务器接收完请求,并在用户空间处理客户端的请求,直到构建响应完成(4)
返回数据,服务器将已构建好的响应再通过内核空间的网络I/0发还给客户端(5-7)
同步/异步:关注的是消息通信机制,即调用者在等待一件事情的处理结果时,被调用者是否提供完成状态的通知。
同步: synchronous,被调用者并不提供事件的处理结果相关的通知消息,需要调用者主动询问事情是否处理完成
异步: asynchronous,被调用者通过状态、通知或回调机制主动通知调用者被调用者的运行状态
阻塞/非阻塞:关注调用者在等待结果返回之前所处的状态
阻塞: blocking,指IO操作需要彻底完成后才返回到用户空间,调用结果返回之前,调用者被挂起,干不了别的事情。
非阻塞: nonblocking,指IO操作被调用后立即返回给用户一个状态值,而无需等到IO操作彻底完成,在最终的调用结果返回之前,调用者不会被挂起,可以去做别的事情。
阻塞型
非阻塞型同步
多路复用I/O模型 ( epoll poll select)
poll是Linu中的字符设备驱动中的一个函数。Linux 2.5.44版本后,poll被epoll取代。和select实现的功能差不多,poll的作用是把当前的文件指针挂到等待队列。
信号驱动型
异步非阻塞I/O模型
nginx官网地址,我这边下载的是nginx -1.12.0.tar.gz包
解压
安装关系依赖包
[root@localhost opt]#yum install pcre-devel zlib-devel gcc-c++.x86_64 make.x86_64 -y
新建用户 和组便于管理(nginx 服务程序默认 以 nobody 身份运行,建议为其创建专门的用户账户,以便更准确的控制访问权限)
- [root@localhost opt]#useradd -M -s /sbin/nologin nginx
- [root@localhost opt]#id nginx
- uid=1001(nginx) gid=1001(nginx) 组=1001(nginx)
编译安装Nginx
cd到nginx包下面
- [root@localhost nginx-1.12.0]#./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
-
-
-
-
- --prefix=/usr/local/nginx \
- #安装路径
- --user=nginx \
- #指定用户名
- --group=nginx \
- #指定用户组
- --with-http_stub_status_module
- #启用此模块支持状态统计
[root@localhost nginx-1.12.0]#make && make install -j6
做一个软链接
[root@localhost nginx-1.12.0]#ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
检查语法
- [root@localhost nginx-1.12.0]#nginx -t
- 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
启动nginx
- [root@localhost nginx-1.12.0]#nginx
- [root@localhost nginx-1.12.0]#lsof -i :80
- COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
- nginx 63588 root 6u IPv4 149782 0t0 TCP *:http (LISTEN)
- nginx 63589 nginx 6u IPv4 149782 0t0 TCP *:http (LISTEN)
停止nginx
kill -3 <PID号>
kill -s QUIT <PID号>
killall -3 nginx
killall -s QUIT nginx#重载
kill -1 <PID号>kill -s HUP <PID号>
killall -1 nginx
killall -s HUP nginx
信号编号 | 信号名 | 含义 |
---|---|---|
0 | EXIT | 程序退出时收到该信息。 |
1 | HUP | 挂掉电话线或终端连接的挂起信号,这个信号也会造成某些进程在没有终止的情况下重新初始化。 |
2 | INT | 表示结束进程,但并不是强制性的,常用的 "Ctrl+C" 组合键发出就是一个 kill -2 的信号。 |
3 | QUIT | 退出。 |
9 | KILL | 杀死进程,即强制结束进程。 |
11 | SEGV | 段错误。 |
15 | TERM | 正常结束进程,是 kill 命令的默认信号。 |
添加nginx服务
- [root@localhost nginx-1.12.0]#vim /etc/init
-
-
- #!/bin/bash
- #chkconfig: 35 99 20
- #description:Nginx Service Control Script
- cmd="/usr/local/nginx/sbin/nginx"
- pid="/usr/local/nginx/logs/nginx.pid"
-
- case $1 in
-
- start)
- $cmd
- ;;
-
- stop)
- kill -3 `cat $pid`
- ;;
-
- reload)
- kill -1 `cat $pid`
- ;;
-
- restart)
- $0 stop
- $0 start
- ;;
-
- *)
- echo "plaese input start,stop,reload,restart"
- exit 1
-
- esac
- exit 0

- [root@localhost system]#vim nginx.service
-
- [Unit]
- Description=nginx
- After=network.target
- [Service]
- Type=forking
- PIDFile=/usr/local/nginx/logs/nginx.pid
- ExecStart=/usr/local/nginx/sbin/nginx
- ExecReload=/usr/bin/kill -s HUP $MAINPID
- ExecStop=/usr/bin/kill -s QUIT $MAINPID
- PrivateTmp=true
- [Install]
- WantedBy=multi-user.target
-
-
-
-
- ##配置参数解释##
- [Unit]
- Description=nginx ####描述
- After=network.target ####描述服务类别
- [Service]
- Type=forking ###后台运行形式
- PIDFile=/usr/local/nginx/logs/nginx.pid ###PID文件位置
- ExecStart=/usr/local/nginx/sbin/nginx ###启动服务
- ExecReload=/usr/bin/kill -s HUP $MAINPID ###根据PID重载配置
- ExecStop=/usr/bin/kill -s QUIT $MAINPID ###根据PID终止进程
- PrivateTmp=true
- [Install]
- WantedBy=multi-user.target

重载服务
记得要把之前进程杀掉然后再开启
- [root@localhost system]#systemctl daemon-reload
- [root@localhost system]#systemctl start nginx
由各种配置语句组成,不使用特定的界定标记。全局配置部分包括 Nginx 服务的运行 用户、工作进程数、错误日志、PID 存放位置等基本设置
- [root@localhost system]#cd /usr/local/nginx/conf/
- [root@localhost conf]#ls
- fastcgi.conf koi-utf nginx.conf uwsgi_params
- fastcgi.conf.default koi-win nginx.conf.default uwsgi_params.default
- fastcgi_params mime.types scgi_params win-utf
- fastcgi_params.default mime.types.default scgi_params.default
修改配置文件最好,备份一份
- [root@localhost conf]#cp nginx.conf nginx.conf.bak
- [root@localhost conf]#vim nginx.conf
#user nobody; #运行用户
worker_processes 1; #工作进程数,可配置成服务器内核数*2,如果网站访问量不大,一般设为1就够用了#error_log logs/error.log; #错误日志文件的位置
#pid logs/nginx.pid; #PID文件的位置
events {
use epoll; #使用 epoll 模型以提高性能,2.6 以上版本建议使用
worker_connections 4096; #每个进程处理4096个连接
}
epoll(socket描述符)是Linux内核]为处理大批量文件描述符而作了改进的poll,是Linux下多路复用IO接口select/poll的增强版本,它能显著提高程序在大量并发连接中只有少量活跃的情况下的系统CPU利用率
若工作进程数为 8,每个进程处理 4 096 个连接,则允许 Nginx 正常提供服务的连接数 已超过 3 万个(4 096×8=32 768),当然具体还要看服务器硬件、网络带宽等物理条件的性 能表现。
使用“http { }”界定标记,包括访问日志、HTTP 端口、网页目录、默认字符集、连接保 持,以及后面要讲到的虚拟 Web 主机、PHP 解析等一系列设置,其中大部分配置语句都包 含在子界定标记“server { }”内
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;
#此选项允许或禁止使用socketde TCP_CORK的选项(发送数据包前先缓存数据),此选项仅在使用 sendfile时使用
keepalive_timeout 0;
keepalive_timeout 65;
#连接保持超时时间,单位是秒
gzip on;
#gzip模块设置,设置是否开启gzip压缩输出
###web服务的监听设置
server {
listen 80;
#监听地址及端口
server_name localhost;
#站点域名,可以有多个,用空格隔开
#charset koi8-r;
#网页的默认字符集
#access_log logs/host.access.log main;location / {
root html;
#网站根目录的位置/usr/local/nginx/html
index index.html index.htm;
#默认首页文件名
}#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;
}
与http_x_forwarded_for用以记录客户端的ip地址;
$remote_user:用来记录客户端用户名称;
$time_local:用来记录访问时间与时区;
$request:用来记录请求的url与http协议;
$status:用来记录请求状态:成功是200,300,400,500
$body bytes_sent :记录发送给客户端文件主体内容大小;
$http_referer:用来记录从哪个页面链接访问过来的;
$http_user_agent:记录客户浏览器的相关信息;
通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote add拿到的IP地址是反向代理服务器的IP地址。反向代理服务器在转发请求的http头信息中,可以增加x forwarded for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。
location常见配置指令,root、alias、 proxy _pass
例如:
- [root@localhost conf]#cd /usr/local/nginx/html
- [root@localhost html]#ls
- 50x.html index.html
- [root@localhost html]#vim demo.html
-
-
- <h1>this is nginxweb <h1>
编辑配置文件
添加俩个location
[root@localhost html]#vim /usr/local/nginx/conf/nginx.conf
重启服务
- [root@localhost html]#nginx -t
- 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 html]#systemctl restart nginx.service
如果 是root 就表示 /test 就代表/usr/local/nginx/html/test/ 目录下的文件
是alias 就表示 就是/usr/local/nginx/html/test.html
nginx 内置了 HTTP_STUB_STATUS 状态统计模块,用来反馈当前的 Web 访问情况, 配置编译参数时可添加--with-http_stub_status_module 来启用此模块支持,可以使用命令
可以使用命令/usr/local/nginx/sbin/nginx –V 查看已安装的 Nginx 是否包含 HTTP_STUB_STATUS 模块。
[root@localhost html]#/usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.12.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@localhost html]#vim /usr/local/nginx/conf/nginx.conf
重启服务
- [root@localhost html]#nginx -t
- 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 html]#systemctl restart nginx.service
[root@localhost html]#curl http://192.168.37.101/status
Active connections: 2#当前连接数两个
server accepts handled requests
2 2 2#表示已经处理的连接信息,三个数字一次表示已处理的连接数、成功的TCP握手次数、已处理的请求数
Reading: 0 Writing: 1 Waiting: 1
和apache也差不多,都是改改配置文件,生成用户密码认证文件
现在这台机器没有安装lamp平台,所有要安装一个http工具
- [root@localhost nginx]#cd conf/
- [root@localhost conf]#yum install httpd-tools.x86_64 -y
[root@localhost conf]#htpasswd -c /usr/local/nginx/passwd.db wangwu
New password:
Re-type new password:
Adding password for user wangwu
加密的密码已经生成
[root@localhost conf]#cat /usr/local/nginx/passwd.db
wangwu:$apr1$6EolDU2q$5BROZGhkgxOi8GucEv23Y1
- [root@localhost conf]#vim /usr/local/nginx/conf/nginx.conf
- location / {
- root html;
- index index.html index.htm;
- auth_basic "secret";
- auth_basic_user_file /usr/local/nginx/passwd.db;
- }
改变/usr/local/nginx/passwd.db文件的用户
- [root@localhost conf]#chown nginx /usr/local/nginx/passwd.db
- [root@localhost conf]#ll /usr/local/nginx/passwd.db
- -rw-r--r--. 1 nginx root 45 11月 8 21:23 /usr/local/nginx/passwd.db
修改权限,只读
- [root@localhost conf]#chmod 400 /usr/local/nginx/passwd.db
- [root@localhost conf]#ll /usr/local/nginx/passwd.db
- -r--------. 1 nginx root 45 11月 8 21:23 /usr/local/nginx/passwd.db
重启服务i
- [root@localhost conf]#nginx -t
- 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 conf]#systemctl restart nginx.service
利用虚拟主机,不用为每个要运行的网站提供一台单独的 Nginx 服务器或单独运行一 组 Nginx 进程,虚拟主机提供了在同一台服务器,同一组 Nginx 进程上运行多个网站的功 能。跟 Apache 一样,Nginx 也可以配置多种类型的虚拟主机,分别是基于 IP 的虚拟主机、 基于域名的虚拟主机、基于端口的虚拟主机。 使用 Nginx 搭建虚拟主机服务器时,每个虚拟 Web 站点拥有独立的“server{}”配置段, 各自监听的 IP 地址、端口号可以单独指定,当然网站名称也是不同的。
[root@localhost var]#mkdir -p www/html/{xbin,MyNginx} [root@localhost www]#cd html/ [root@localhost html]#ls MyNginx xbin [root@localhost html]#echo "this is xbin web" > xbin/index.html [root@localhost html]#echo "this is MyNginx web" > MyNginx/index.html [root@localhost html]#cat xbin/index.html this is xbin web
[root@localhost html]#vim /usr/local/nginx/conf/nginx.conf
#gzip on;
server {
listen 80;
server_name www.MyNginx.com;charset utf-8;
access_log logs/MyNginx.access.log;
location / {
server {
listen 80;
server_name www.MyNginx.com;server_name www.MyNginx.com;
charset utf-8;
access_log logs/MyNginx.access.log;
location / {
root /var/www/html/MyNginx;
index index.html index.htm;
}
#error_page 404 /404.html;# redirect server error pages to the static page /50x.html
#
index index.html index.htm;
}
#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;
}
}
server {
listen 80;
#server_name localhost;server_name www.xbin.com;
charset utf-8;
access_log logs/xbin.access.log;
#access_log logs/host.access.log main;location / {
root /var/www/html/xbin;
index index.html index.htm;
}#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;
}
重启服务
- [root@localhost html]#nginx -t
- 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 html]#systemctl restart nginx.service
然后再window系统中修改dns解析
地址:C:\Windows\System32\drivers\etc\hosts
用记事本打开hosts文件,修改
最后测试
成功!!!
[root@localhost html]#vim /usr/local/nginx/conf/nginx.conf
因为我们所添加的网段系统是无法识别的,所以我们要添加一块虚拟网卡
[root@localhost html]#ifconfig ens33:0 192.168.37.111
重启服务
- [root@localhost html]#nginx -t
- 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 html]#systemctl restart nginx.service
去网页输入IP地址
这样就搞定了
基于端口,就在IP地址后面加端口号就可,步骤和基于IP没啥区别,就不多赘述了
Nginx是一个高性能的HTTP和反向代理服务器。是一款轻量级的web服务器/反向代理服务器/电子邮件(IMAP/POP3)代理服务器。
而Apache是以进程为基础的结构,进程要比线程消耗更多的系统开支,不太适用于多处理器环境,因此,在一个apache Web站点扩容时,通常是增加服务器或扩充群集节点而不是增加处理器。
两者的优缺点比较:
(1)nginx相对于apache的优点:
轻量级,同样是web服务,比apache占用更少的内存及资源
抗并发,nginx处理请求是异步非阻塞的,而apache是阻塞型的高并发下,nginx能保持低资源低消耗高性能
高度模块化的设计,编写模块相对简单
(2)apache相对于nginx的优点:
Rewrite比nginx的rewrite强大 ###rewrite的主要功能就是实现统一资源定位符(URL)的跳转
模块多,基本想到的都可以找到
少bug,nginx的bug相对较多
超稳定
存在的理由:一般来说,需要性能的web服务,用nginx。若不需要性能只求稳定,就选用apache。
2、作为web服务器:
相比apache,nginx使用更少的资源,支持更多的并发连接,体现更高的效率。
Nginx作为负载均衡服务器:nginx既可以在内部直接支持rails和php程序对外进行服务,也可以支持http代理服务器对外进行服务。
Nginx采用C进行编写,不论是系统资源开销还是CPU使用效率都比较好。
作为邮件代理服务器:最早开发这个产品的目的之一也是作为邮件代理服务器。
3、nginx配置简洁, apache较复杂
4、最核心的区别在于:
apache是同步多进程模型,一个连接对应一个进程,nginx是异步的,多个连接可以对应一个进程。
Nginx处理静态文件好,耗费内存少,只适合静态和反向。
Apache在处理动态有优势,
nginx并发性比较好,CPU占用内存低,如果rewrite频繁,选用apache最佳。
总的来说,apache依然是大部分公司的首选。(当然国内还用nginx,只能说国外公司多,人少吧)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。