1. Nginx介绍
- Nginx是一款轻量级的高性能的Web服务器和反向代理服务器以及电子邮件(IMAP/POP3)代理服务器。
- 反向代理是指以代理服务器来接收Internet上的链接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给Internet上请求连接的客户端,此时代理服务器就对外表现为一个反向代理服务器。
- Nginx的特点是内存和CPU占用率低),高并发处理能力非常好(官方测试为5w并发请求),反向代理性能非常好(可用于负载均衡)。
2. Nginx环境搭建
2.1 安装所需环境
- 安装编译环境
[root@CentOS7 ~]# yum -y install gcc gcc-c++
- 安装pcre库(支持rewrite库)
[root@CentOS7 ~]# yum -y install pcre pcre-devel
- 安装zlib库
[root@CentOS7 ~]# yum -y install zlib zlib-devel
- 安装openssl
[root@CentOS7 ~]# yum -y install openssl openssl-devel
2.2 安装Nginx
- 下载Nginx
[root@CentOS7 ~]# wget http://nginx.org/download/nginx-1.16.1.tar.gz
- 解压源码包
[root@CentOS7 ~]# tar -zxvf nginx-1.16.1.tar.gz
- 使用configure命令创建makeFile文件
[root@CentOS7 ~]# cd nginx-1.16.1/
[root@CentOS7 nginx-1.15.9]#./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
- 编译及安装
[root@CentOS7 nginx-1.16.1]# make -j 4 && make install
2.3 测试
启动nginx,通过浏览器访问
[root@CentOS7 ~]# /usr/local/nginx/sbin/nginx
3. Nginx常用命令
所有命令均在nginx安装目录下的sbin文件夹下执行,也就是/usr/local/nginx/sbin文件夹。
- ./nginx:普通启动
- ./nginx -c /usr/local/nginx/conf/nginx.conf :通过配置文件启动nginx进程
- ./nginx -s signal:发送信号到主进程(signal可以为stop/quit/reopen/reload)。reopen相当于USR1,quit相当于QUIT,stop相当于INT
- ./nginx -v: 查看版本
- ./nginx -t :测试配置文件是否正确
- ./nginx -h :查看帮助信息
4. Nginx信号控制
Nginx的信号控制
语法:kill -信号选项 nginx的主进程号
-TERM,-INT | 快速关闭 |
-QUIT | 优雅的关闭进程,即等请求结束后再关闭 |
-HUP | 改变配置文件,平滑的重读配置文件 |
-USR1 | 重读打开日志文件,在日志按月/日分割时有用 |
-USR2 | 平滑的升级可执行程序 |
-WINCH | 优雅关闭旧的进程(配合USR2来进行升级) |
默认,nginx将其主进程的pid写入到/usr/local/nginx/nginx.pid文件中。
5. Nginx配置文件详解
默认nginx配置文件位于/usr/local/nginx/conf/nginx.conf。
1
2 #user nobody; #配置worker进程运行用户
3 worker_processes 1; #nginx子进程数(worker process),最大为物理CPU数*核数
4
5 #error_log logs/error.log; #配置全局错误日志文件(级别有debug| info| notice| warn| error| crit),默认error
6 #error_log logs/error.log notice;
7 #error_log logs/error.log info;
8
9 #pid logs/nginx.pid; #进程PID存放位置
10
11
12 events { #配置Nginx连接的特性
13 worker_connections 1024; #单个后台worker process进程的最大连接数
14 }
15
16
17 http { #这里是配置http服务器的主要段
18 include mime.types; #文件扩展名与类型映射表
19 default_type application/octet-stream; #默认文件类型
20
21 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
22 # '$status $body_bytes_sent "$http_referer" '
23 # '"$http_user_agent" "$http_x_forwarded_for"'; #设定日志格式
24
25 #access_log logs/access.log main; #配置access.log日志及存放路径,并使用上面定义的main格式
26
27 sendfile on; #是否开启高效传输模式
28 #tcp_nopush on; #是否防止网络阻塞
29
30 #keepalive_timeout 0;
31 keepalive_timeout 65; #连接超时时间,单位s
32
33 #gzip on; #是否开启gzip压缩
34
35 server { #设定虚拟主机配置
36 listen 80; #指定虚拟主机监听的端口
37 server_name localhost; #指定虚拟主机对应的域名,若为多个,用空格隔开
38
39 #charset koi8-r; #设置网页的编码格式
40
41 #access_log logs/host.access.log main; #设置虚主机的访问日志
42
43 location / { #设置虚拟主机的基本信息
44 root html; #当前主机访问网站根目录
45 index index.html index.htm; #设置虚拟主机默认访问的网页
46 }
47
48 #error_page 404 /404.html; #定义错误提示页面(404)
49
50 # redirect server error pages to the static page /50x.html
51 #
52 error_page 500 502 503 504 /50x.html; #定义50x错误提示页面
53 location = /50x.html { #“=”号表示精确匹配
54 root html; #当前主机访问网站根目录
55 }
56
57 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
58 #
59 #location ~ \.php$ { #对php脚本(区分大小写,以php结尾)启用反向代理
60 # proxy_pass http://127.0.0.1;
61 #}
62
63 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
64 #
65 #location ~ \.php$ { #php脚本请求全部转发到fastcgi处理,使用fastcgi默认配置
66 # root html; #网站根目录
67 # fastcgi_pass 127.0.0.1:9000; #抛给本地的9000端口
68 # fastcgi_index index.php; #设定动态首页
69 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
70 # include fastcgi_params; #设定和fastcgi交互的相关参数包含文件
71 #}
72
73 # deny access to .htaccess files, if Apache's document root
74 # concurs with nginx's one
75 #
76 #location ~ /\.ht { #禁止访问.htxx文件
77 # deny all;
78 #}
79 }
80
81
82 # another virtual host using mix of IP-, name-, and port-based configuration
83 #
84 #server { #配置另一台虚拟主机
85 # listen 8000;
86 # listen somename:8080;
87 # server_name somename alias another.alias;
88
89 # location / {
90 # root html;
91 # index index.html index.htm;
92 # }
93 #}
94
95
96 # HTTPS server #配置https服务
97 #
98 #server {
99 # listen 443 ssl;
100 # server_name localhost;
101
102 # ssl_certificate cert.pem; #为这个虚拟主机指定pem格式的证书文件
103 # ssl_certificate_key cert.key; #为这个虚拟主机指定pem格式的密钥
104
105 # ssl_session_cache shared:SSL:1m; #设置储存SSL会话的缓存类型和大小
106 # ssl_session_timeout 5m; #设置客户端能够反复使用储存再缓存中的会话参数时间
107
108 # ssl_ciphers HIGH:!aNULL:!MD5; #指出允许的密码(为openssl支持的格式)
109 # ssl_prefer_server_ciphers on; #使用SSLV3和TLSV1协议的服务器密码将优先于客户端密码
110
111 # location / {
112 # root html;
113 # index index.html index.htm;
114 # }
115 #}
116 }