简述

Nginx是一款轻量级的HTTP服务器(相比于Apache、Lighttpd等),同时是一个高性能的HTTP和反向代理服务器。Nginx主要以事件驱动的方式编写,这让它拥有非常好的性能,同时也能非常高效的实现反向代理和负载均衡。

nginx的几项基本特征:

  • 处理静态文件,索引文件以及自动索引;打开文件描述符缓冲
  • 无缓存的反向代理加速,简单的负载均衡和容错
  • FastCGI,简单的负载均衡和容错
  • 模块化的结构,包括gzipping,byte ranges,chunked responses,以及SSI-filter等filter。
  • 支持SSL和TLSSNI.

安装nginx

window安装

安装步骤省略,闲麻烦可以安装一个集成环境–phpStudy。 不是嫌弃windows电脑,极想买一台性能很6的window电脑,实在囊中羞涩。

Mac安装

1
brew update && brew install nginx

安装完成后可以用浏览器打开http://localhost:8080看到Nginx的欢迎信息。

一般情况下mac下nginx的

1
2
3
4
5
//安装路径如下:(可执行程序)
/usr/local/Cellar/nginx/1.12.0_1/bin/nginx

//配置所在路径
/usr/local/etc/nginx/nginx.conf

先看看nginx的默认配置,后面再细讲

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

#user nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


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;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 8080;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root 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;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}


# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}
include servers/*;
}

开启,停止,重新启动

要启动nginx,可以直接运行nginx的可执行文件。也就是刚才说的“/usr/local/Cellar/nginx/1.12.0_1/bin/nginx”这个文件。一但nginx已经开启。可以带上“-s [参数]“执行nginx的可执行文件。如下:

1
2
3
4
5
6
7
8
9
10
11
nginx -s signal
```

signal 如下当中的一种:

- stop 快速关闭
- quit 优雅的关闭
- reload 重新加载配置文件
- reopen 重新打开日志文件。

看到如上的信号,有小伙伴肯定会问stop 和 quit 都是关闭这两者有什么区别。

nginx -s quit //This command should be executed under the same user that started nginx. 官网原话,好懂吧!

1
2


nginx -s reload

1
2
3
4
5
6
7
8
9

重启命令就更厉害了。先看一段官网原话:
Once the master process receives the signal to reload configuration, it checks the syntax validity of the new configuration file and tries to apply the configuration provided in it. If this is a success, the master process starts new worker processes and sends messages to old worker processes, requesting them to shut down. Otherwise, the master process rolls back the changes and continues to work with the old configuration. Old worker processes, receiving a command to shut down, stop accepting new connections and continue to service current requests until all such requests are serviced. After that, the old worker processes exit.

我们来大概翻译下:一但nginx主进程接收到reload的信号,会先去验证新配置的语法是否正确。如果成功也就是语法正确,nginx的主进程会开一个新的进程,发信息给旧的主进程请求关闭。如不reload不成功,主进程会继续使用旧的主进程,使用旧的配置。 旧的主进程接上到关闭命令会停止接收新的链接并继续服务已接收的链接直到服务完成。这之后才会关闭。

我们用人话来说翻译下上面的内容: nginx能够不中断服务启用新的配置,不知道大家如何看,反正我觉得很吊。

#### 其它优雅的结束方式:

kill -s QUIT NGINX-PID

1
2
3
4

主进程的PID(process id)一般会被写入nginx.pid。 这个文件一般在”/usr/local/nginx/logs 或 /var/run“下。

通过如上的方式如果找不到nginx的进程id可以执行如下命令找到。

ps -ax | grep nginx

1
2

### 配置语法检查命令

nginx -t
`

好啦!今天的分享就先到这里了,下期再见。