Nginx安装文档
由于nginx也是默认使用的80端口,所以保证80端口没有被占用,如果被占用,就要改其端口
1.给nginx创建一个运行服务器的系统用户,如果不给,默认用nobody
# useradd -s /sbin/nologin www
2.先装pcre-8.12.tar.gz;为了使nginx支持HTTP Rewrite模块,让nginx能够兼容正则表达式,实现正则匹配,写rewrite。
(注释:PCRE(Perl Compatible Regular Expressions即perl语言兼容正则表达式)是一个Perl库,包括 perl 兼容的正规表达式库。这些在执行正则表达式模式匹配时用与Perl 5同样的语法和语义是很有用的。)
# tar zxvf pcre-8.12.tar.gz -C /usr/local/src/
# yum install -y gcc gcc-c++
# cd /usr/local/src/pcre-8.12
# ./configure
# make
# make install
(注:# make clean 清除make的残留文件,在make失败的时候可以使用)
3.安装nginx
wget http://nginx.org/download/nginx-1.8.0.tar.gz
# tar zxvf nginx-1.8.0.tar.gz -C /usr/local/src/
# cd /usr/local/src/nginx-0.8.46/
# yum install -y openssl openssl-devel
#./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
(注:--with-http_ssl_module:启用ssl支持
--with-http_stub_status_module:这个模块可以用来启用nginx的NginxStatus功能,以监控Nginx的当前状态。
--with-http_gzip_static_module 压缩传输 )
# make
# make install
查看nginx版本
# /usr/local/nginx/sbin/nginx -v
查看nginx版本及是否包含某模块
# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/0.8.46
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC)
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
4.nginx启动
# vim /etc/init.d/nginx 写入服务启动脚本或有现成的copy一份过来
# chmod +x /etc/init.d/nginx
# /etc/init.d/nginx start
# chkconfig --add nginx
# chkconfig nginx on
# chkconfig nginx --list
nginx 0:关闭1:关闭2:启用3:启用4:启用5:启用6:关闭
nginx启停(手动)
# cd /usr/local/nginx/
1)启动nginx
# ./sbin/nginx 【-c conf/nginx.conf] 】
-c 指定配置文件,不指定的时候就使用默认配置文件/usr/local/nginx/conf/nginx.conf
2)重新加载nginx
# ./sbin/nginx -s reload
3)停止nginx
# ./sbin/nginx -s stop
4)验证nginx是否启动
# ps -ef | grep nginx
# netstat -antp | grep 80
设置开机自启动 /usr/local/nginx//sbin/nginx 写到rc.local里
加入防火墙规则
# vim /etc/sysconfig/iptables
加入修改为
-I INPUT -p tcp --dport 80 -j ACCEPT
# /etc/init.d/iptables restart
访问测试:
http://IP
如果出来Welcome to nginx证明没有问题
———————————————————————————————————————
主配置文件所在位置:(与安装路径有关系)
/usr/local/nginx/conf/nginx.conf
nginx的配置文件是一个纯文本文件,整个配置文件是以 block的形式组织的;每个 block 使用 {} 包起来;
完整配置文件的结构如下:
nginx配置文件主要由四部分组成:main(全局设置)、server(主机设置)、upstream(负载均衡服务器设置)、location(URL匹配特定位置的设置)。
其中:main部分设置的指令将会影响其他所有设置;
server部分的配置主要用于指定主机和端口
upstream部分的配置用于负载均衡,设置一系列的后端服务器
location部分的配置用于匹配网页位置
它们四个的关系是server继承main,location继承server,upstream既不继承其他设置也不会被继承。
3、配置文件简单讲解
1)全局配置部分
main:全局配置;在这的配置会影响其他的配置
user nginx; // 指定Nginx Worker进程运行用户,默认由nobody用户运行。
worker_processes 1; // 指定nginx要开启的进程数。不能超过CPU核心数
#error_log logs/error.log; // 指定全局错误日志文件,日志级别有debug、info、notice、warn、error、crit,级别从低到高,日志量依次减少。
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid; // 指定存储nginx进程id的文件
events {
worker_connections 1024; // 设置nginx连接数的上限总连接数=worker_processes x worker_connections
use epoll; // use用来指定nginx的工作模式:select,poll,kqueue,epoll
标准:select,poll 高效:kqueue,epoll
不同是epoll用在Linux平台,而kqueue用在BSD系统中,对于Linux系统,epoll工作模式是首选。epoll能显著提高程序在大量并发连接中只有少量活跃的情况下的系统CPU利用率。
}
2)http服务器配置部分
http {
include mime.types; // 实现对配置文件所包含的文件的设定,可以减少主配置文件的复杂度。
default_type application/octet-stream; // 默认的类型为二进制流,即当文件类型未定义时使用这种方式,例如在没有配置php环境时,nginx是不予解析的,这时浏览器访问php会出现下载窗口。
#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; // 表示开启gzip压缩,实时压缩输出数据流。
3)server配置部分
server {
listen 80; // 监听端口,服务器端口
server_name localhost; // 服务器的IP地址或者域名,多个域名之间用空格分隔
#charset koi8-r; // 设置网页的默认编码格式,网页的默认字符集
#access_log logs/host.access.log main; // 访问日志的存放路径,main用于指定访问日志的输出格式
4)URL匹配配置部分(nginx配置中最灵活的部分) 支持正则表达式,也支持条件判断匹配,可以实现nginx对动、静态网页的过滤处理。
location / {
root html; // 用于设置网页的根目录,可以是相对路径也可以是绝对路径/usr/local/nginx/html,nginx默认发布网页的目录
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;
}
}
}
5)StubStatus模块配置:该模块能够获取nginx自上次启动以来的工作状态。
配置nginx的状态查看:
server {
......
location /NginxStatus {
stub_status on; // on表示启用StudStatus的工作状态统计功能。
access_log logs/NginxStatus.log; // 指定该模块的访问日志文件
auth_basic "NginxStatus"; // nginx的认证机制
auth_basic_user_file /usr/local/nginx/conf/.htpasswd; // 指定认证密码文件
}
}
# yum install -y httpd
# htpasswd -c /usr/local/webserver/nginx/conf/.htpasswd webadmin
New password: admin
Re-type new password:admin
Adding password for user webadmin
测试配置文件是否有语法错误
# ./sbin/nginx -t
4、重新加载配置文件,验证是否能够查看nginx
# /usr/local/webserver/nginx/sbin/nginx -s reload
# firefox http://172.16.254.200/NginxStatus &
或者使用nginx的平滑重启:
# cat logs/nginx.pid
22853
# kill -HUP 22853 或者直接/usr/local/webserver/nginx/sbin/nginx -s reload(重新加载配置)
# firefox http://127.0.0.1/NginxStatus &
用户名:webadmin
密码:admin(刚才自己设置的)
结果:
Active connections: 1 // 表示nginx 正处理的活动连接数
server accepts handled requests
13 13 54
Reading: 0 Writing: 1 Waiting: 0
其中:13 13 54三个数字分别代表了:
nginx当前总共处理了13个连接 成功创建了13次握手 总共处理了54个请求
Reading:表示nginx读取到客户端Header信息数
Writing:表示Nginx返回给客户端的Header信息数
Waiting:表示Nginx已经处理完、正在等候下一次请求指令时的驻留连接数,在开启keep-alive的情况下,这个值等于active–
(reading + writing)。
查看nginx版本
# /usr/local/nginx/sbin/nginx -v
查看nginx版本及是否包含某模块
# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/0.8.46
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC)
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --
with-http_ssl_module