tyltr技术窝

nginx主要分为四部分:

  • nginx 二进制文件
  • nginx配置文件 nginx.conf等
  • access.log 接入日志
  • error.log 错误日志

配置文件控住nginx的行为,一般情况下,linux默认的配置文件的路径为 /etc/nginx/nginx.conf 注:在不同的系统中,配置文件的路径可能会不同

1. 配置文件说明#

1.1 nginx.conf文件#

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

#user nobody;


#----------- worker_processes --------------start
# nginx是master-worker结构,指定worker的数量。一般设置为cup数量,性能最优
# 如果4核cup,建议设置 worker_processes 4;
worker_processes 1;
#----------- worker_processes --------------end


#----------- error_log --------------start
# error_log 指定错误日志的路径。
# 语法:error_log file [level];
# 默认:error_log logs/error.log error;
# 日志等级:debug, info, notice, warn, error, crit, alert, emerg
# error等级的日志,包含error ,crit, alert, emerg 即包含error。其他的等级也一样
# -------------error_log -------------end

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


# pid 指定 pid文件
#pid logs/nginx.pid;




events {
# -------------worker_connections -------------start
# worker_connections 每个worker进程,能并发处理(发起)的最大连接数
# 系统为每个tcp链接创建一个socket句柄,每个socket句柄就是一个文件句柄
# 即每个worker可以打开的文件句柄限制
# 可以通过 ulimit -n 查看每个进程最多打开的文件数限制
# 可使用 ulimit -n <file_num> 进行修改
# -------------worker_connections -------------end
worker_connections 1024;
}



http {
include mime.types;
default_type application/octet-stream;

# -------------log_format -------------start
# log_format 说明
# log 日志的格式
# -------------log_format -------------end

#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 可以写在其他文件目录中-----------------------------
# server upstream 块,用户配置修改频率高于其他的块
# 在文件中进行引入
#---------------server 可以写在其他文件目录中-----------------------------


server {
listen 80;
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;
# }
#}

}

注:#

此为默认写法,一般在生产中,server 与upstream 可以写在其他目录中,引入到http块。

减少对nginx.conf文件的修改,而且目录结构清晰。

例如:

  • nginx.conf文件
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
user  nginx;
worker_processes 2;

error_log /var/log/nginx/error.log error;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}


http {
include /etc/nginx/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 /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

keepalive_timeout 65;

#gzip on;

# 引入其他目录的conf,引入/etc/nginx/conf.d/ 以conf结尾的文件
include /etc/nginx/conf.d/*.conf;
}

```


- 引入目录下的用户配置:
在/etc/nginx/conf.d目录下的文件,以`.conf`结尾

```conf

upstream backend {
server 127.0.0.1:8080 weight=1;
server 127.0.0.1:8081 weight=1;
server 127.0.0.1:8081 weight=1;

}

server {
# 监控端口
listen 80;
server_name localhost;

#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;

#访问localhost:80端口,或被路由到上游backend上面去,即 pstream backend
# 再根据负责均衡策略进行,分发到不同放入server上
location / {
proxy_pass http://backend;
}
}

上述简单的实现了负载均衡的配置。

1.2结构总结#

由上可以看出,nginx的配置文件,是依照 块进行划分的,例如:server、 http、event等。但他们之间有什么区别与联系呢?
nginx主要分为6块:

  • 全局模块,例如 user nobody; worker_processes 1;
  • events 块
  • http 块
    • server 块
      • location 块
    • upstream 块

一个http块里面可以有多个server,一个server里面可以有多个location