nginx.png
Nginx(“engine x”)是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和 反向代理商 服务器,也是一个 IMAP/POP3/SMTP 代理商服务器。
在高连接并发的情况下,Nginx是Apache服务器不错的替代品。
安装Nginx
这里我用CentOS为例:
1.安装先决条件
$ yum install yum-utils2.设置yum存储库,将下面内容写入/etc/yum.repos.d/nginx.repo
$ vim /etc/yum.repos.d/nginx.repo[nginx-stable]name=nginx stable repobaseurl=http://nginx.org/packages/centos/$releasever/$basearch/gpgcheck=1enabled=1gpgkey=https://nginx.org/keys/nginx_signing.keymodule_hotfixes=true3.安装Nginx
$ yum update$ yum install nginx4.验证能否安装成功
启动Nginx
$ nginx查看端口80已被nginx监听$ netstat -ntlpActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 666/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 799/master tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 23779/nginx: master tcp6 0 0 :::22 :::* LISTEN 666/sshd tcp6 0 0 ::1:25 :::* LISTEN 799/master5.设置防火墙
假如在浏览器里输入ip地址发现无法访问此网站,那有可能是防火墙80端口没有开放。
编辑iptables配置文件$ vim /etc/sysconfig/iptables增加开放80端口 -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEP重启iptables$ systemctl restart iptables们刷新页面出现Welcome to nginx!说明安装配置成功
welcone nginx.png
Nginx常用命令
$ nginx — 启动Nginx$ nginx -t — 检查Nginx配置文件$ nginx -s stop — 快速关闭$ nginx -s quit — 正常关闭$ nginx -s reload — 重新加载配置文件$ nginx -s reopen — 重新打开日志文件Nginx主模块
基本命令
daemon语法: daemon on | off缺省值:on
能否以守护进程的方式运行nginx,守护进程是指脱离终端并且在后端运行的进程,关闭守护进程可以方便我们调试nginx。
daemon off;master_process语法:master_process on | off缺省值:on
能否以master/worker方式逬行工作,在实际的环境中nginx是以一个master逬程管理多个worker进程的方式运行的,
关闭后nginx就不会fork出worker子进程来解决请求,而是用master进程自身来解决请求worker_processes number;默认1,在master/worker运行方式下worker进程的数目,一般情况下客户要配置与CPU内核数相等的worker进程。
master_process off;生产环境中不要使用”daemon”和”master_process”指令,这些选项仅用于开发调试。
error_log语法:error_log file [ debug | info | notice | warn | error | crit ]缺省值:${prefix}/logs/error.log
Nginx 增加 –with-debug 编译参数, 你还能够使用以下配置:
error_log LOGFILE [ debug_core | debug_alloc | debug_mutex | debug_event]: | debug_http | debug_imap ;include语法:include file | *缺省值:none
你可以在任意地方使用include指令实现配置文件的包含,相似于apache中的include方法,可减少主配置文件。
include指令还支持像下面配置一样的全局包含的方法,例如包含一个目录下所有以”.conf”结尾的文件:
include vhosts/*.conf;pid语法:pid file缺省值:compile-time option Example:
进程id存储文件。可以使用 kill -HUP cat /var/log/nginx.pid\ 对Nginx进行配置文件重新加载。
pid /var/log/nginx.pid;user语法:user user [group]缺省值:nobody nobody
指定Nginx Worker进程运行客户,默认是nobody帐号。
例如:
user www users;worker_processes语法:worker_processes number缺省值:1
nginx可以使用多个worker进程,起因如下:
1.使用SMP
2.当工作程序在磁盘I / O上阻塞时减少推迟
3.当使用select()/ poll()时限制每个进程的连接数
worker_processes 4;Nginx解决HTTP的核心功能模块
基本指令
alias语法:alias file-path|directory-path;缺省值:no使用字段:location
该伪指令分配用于指定位置的路径。 请注意,它看起来相似于root伪指令,但是文档root不会改变,只是用于请求的文件系统路径。
location /i/ { alias /spool/w3/images/;}# 请求“ /i/1.jpg”将返回文件“ /spool/w3/images/1.jpgkeepalive_timeout语法: keepalive_timeout [ time ]缺省值:keepalive_timeout 75使用字段:http, server, location
第一个参数为与用户端的保持活动连接分配超时。 在此时间之后,服务器将关闭连接。
可选的第二个参数指定了答应头Keep-Alive: timeout=time的“`time““值,这个值可以使少量浏览器知道什么时候关闭连接,以便使服务器不用重复关闭,假如不指定这个参数,nginx不会在答应头中发送Keep-Alive信息。
下面列出了少量服务器如何解决包含Keep-Alive的答应头:
- MSIE和Opera会忽略“ Keep-Alive:timeout = <N>”标头。
- MSIE使连接保持活动状态约60-65秒,而后发送TCP RST。
- Opera可以长时间保持连接状态。
- Mozilla使连接保持活动状态N大约1-10秒。
- Konqueror使连接保持活动状态约N秒钟。
listen语法:listen address:port [ default [ backlog=num | rcvbuf=size | sndbuf=size | accept_filter=filter | deferred | bind | ssl ] ]缺省值:listen 80使用字段:server
listen指令指定封闭服务器{…}块接受的地址和端口。 可以仅指定地址,仅端口或者服务器名称作为地址。
listen 127.0.0.1:8000;listen 127.0.0.1;listen 8000;listen *:8000;listen localhost:8000;IPv6地址(0.7.36)设置:
listen [::]:8000; listen [fe80::1];location语法:location [=|~|~*|^~] /uri/ { ... }缺省值:no使用字段:server
这个参数根据URL的不同需求来进行配置,可以使用字符串与正则表达式匹配,假如要使用正则表达式,你必需指定下列前缀:
- ~*不区分大小写
- ~区分大小写
location = / { # 只匹配 / 的查询 [ configuration A ] }location / { # 匹配任何以 / 开头的查询 # 但是正则表达式与少量较长的字符串将首先匹配 [ configuration B ] }location ^~ /images/ { # 匹配任何以/images/开始的查询并且中止搜索,不检查正则表达式 [ configuration C ] }location ~* \.(gif|jpg|jpeg)$ { # 匹配任何以gif,jpg,jpeg结尾的文件,但是所有/images/目录请求将在Configuration C 中解决 [ configuration D ] }resolver_timeout语法:resolver_timeout time使用字段:http,server,location
解析超时时间。如:
resolver_timeout 5s;root语法:root path缺省值:root html使用字段:http,server,location,location中if字段
请求到达后的文件根目录
location /i/ { root /spool/w3}假如亲求/i/top.jpg文件,nginx将转到/spool/w3/i/top.jpg文件。你可以在参数中使用变量。
注意:在亲求中root会增加这个location到它的后面,即/i/top.jpg并不会请求/spool/w3/i/top.jpg文件,假如要实现上述相似与apache alias的功能,可以使用alias指令。
server语法:server{...}缺省值:no使用字段:http
server字段包含虚拟主机的配置。
没有明确的机制来分开基于域名(请求中的主机头)和基于IP的虚拟主机。
可以通过listen指令来指定必需连接到这个server块的所有地址和端口,并且在server_name指令中可以指定所有的域名。
server_name语法:server_name name [...]使用字段:server
将HTTP请求的主机头与nginx配置文件中的server字段中指定的参数进行匹配,并且找出第一个匹配结果。
- 完整的静态名称
- 名称开头带有通配符的名称— * .example.com
- 名称末尾带有通配符的名称www.example.*
- 具备正则表达式的名称
假如不匹配,将根据以下顺序使用配置文件中的[#server服务器{…}]块:
- listen指令被标记为default的server字段
- 第一个出现listen的server字段