1.Nginx介绍
Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名,在连接高并发的情况下,Nginx是Apache服务器不错的替代品。Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
2.Nginx安装
首先安装必要的库(nginx 中rewrite模块需要 pcre 库,gzip模块需要 zlib 库,ssl 功能需要openssl库),安装目录/usr/local/
2.1 pcre库安装
- http://www.pcre.org/上获取pcre编译安装包
- 解压缩pcre-xx.tar.gz包
- 进入解压缩目录,执行./configure。
- make & make install
[root@localhost ~]# cd /usr/local/
[root@localhost local]# tar -zxvf pcre-8.42.tar.gz
[root@localhost local]# cd pcre-8.42/
[root@localhost pcre-8.42]# ./configure
[root@localhost pcre-8.42]# make
[root@localhost pcre-8.42]# make install
2.2 zlib库安装
- http://www.zlib.net/上获取zlib编译安装包
- 解压缩zlib-xx.tar.gz包
- 进入解压缩目录,执行./configure。
- make & make install
[root@localhost ~]# cd /usr/local/
[root@localhost local]# tar -zxvf zlib-1.2.11.tar.gz
[root@localhost local]# cd zlib-1.2.11/
[root@localhost zlib-1.2.11]# ./configure
[root@localhost zlib-1.2.11]# make
[root@localhost zlib-1.2.11]# make install
2.3 openssl库安装
- http://www.openssl.org/source/上获取openssl编译安装包
- 解压缩openssl-xx.tar.gz包
- 进入解压缩目录,执行./configure。
- make & make install
[root@localhost ~]# cd /usr/local/
[root@localhost local]# tar -zxvf openssl-1.1.1-pre8.tar.gz
[root@localhost local]# cd openssl-1.1.1-pre8/
[root@localhost openssl-1.1.1-pre8]# ./config
[root@localhost openssl-1.1.1-pre8]# make
[root@localhost openssl-1.1.1-pre8]# make install
2.4 nginx安装
- http://nginx.org/en/download.html上获取nginx安装包
- 解压缩nginx-xx.tar.gz包
- 进入解压缩目录,执行./configure。
- make & make install
[root@localhost ~]# cd /usr/local/
[root@localhost local]# tar -zxvf nginx-1.14.0.tar.gz
[root@localhost local]# cd nginx-1.14.0/
[root@localhost nginx-1.14.0]# ./configure
[root@localhost nginx-1.14.0]# make
[root@localhost nginx-1.14.0]# make install
3.启动
先测试一下配置文件是否正确:/usr/local/nginx/sbin/nginx -t
[root@localhost local]# /usr/local/nginx/sbin/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
无问题可以启动:/usr/local/nginx/sbin/nginx
[root@localhost local]# /usr/local/nginx/sbin/nginx
检查启动情况,浏览器访问http://ip:nginx端口出现welcome to nginx!即为启动成功
4.常用命令
测试配置文件是否正常:
/usr/local/nginx/sbin/nginx –t
重启:
/usr/local/nginx/sbin/nginx –s reload
停止服务:
/usr/local/nginx/sbin/nginx –s stop
强制关闭:
pkill nginx
5.配置文件说明
#user nobody; #配置用户或者组,默认为nobody nobody
worker_processes 1;#允许生成的进程数,默认为1
#制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid; #指定nginx进程运行文件存放地址
events {
worker_connections 1024;#最大连接数
}
http {
include mime.types;#文件扩展名与文件类型映射表
default_type application/octet-stream;#默认文件类型,默认为text/plain
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'"$request_body" $body_bytes_sent $status ';
# '"$http_referer" "$http_user_agent" "$http_x_forwarded_for"';#自定义格式
sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
keepalive_timeout 65; #连接超时时间,可以在http,server,location块。
lingering_timeout 10;
client_max_body_size 500m;
#远程服务器
upstream pserver2Proxy {
server 192.168.30.1:8081;
}
server {
listen 9091;#监听端口
server_name 127.0.0.1;#监听ip地址或者域名,多个配置之间用空格分隔
access_log logs/host.access.log main;
location /pweb/ {
proxy_pass http://pserver2Proxy/pweb/; #充当代理服务器,转发请求
proxy_redirect off;#修改301或者302转发过程中的Location
}
location / {#表示匹配访问根目录
root qrcbstatic/pweb/static;#用于指定访问根目录时,访问虚拟主机的web目录
index index.html;#在不指定访问具体资源时,默认展示的资源文件列表
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}