前言

随着nginx的使用,一台服务器下的域名及端口多了以后,在nginx.conf配置文件中就需要不断的配置server

久而久之,就会发现nginx.conf特别臃肿。

所以我在这里对这个nginx.conf进行解耦拆分,让服务器上每个项目都有自己独立的子配置文件。

接下来的内容,可能会涉及到
主配置文件:nginx.config
子配置文件:nginx_xxx.config

配置nginx.

配置主配置文件 nginx.config

直接把新安装的Nginx里的原始nginx.config用下面代码替换掉。
如果原始nginx.config中已经配置过server{ },就需要提前备份好,改成子配置文件使用。

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
worker_processes 2;

#日志
# error_log logs/error.log info;

#记录pid
pid logs/nginx.pid;

events{
worker_connections 1024;
}

http{
# 开启GZIP压缩
gzip on;
# 最小压缩文件大小
gzip_min_length 4K;
# 压缩缓冲区
gzip_buffers 4 8K;
# 压缩等级
gzip_comp_level 2;
gzip_vary on;
# 压缩类型
gzip_types application/javascript text/css text/plain application/xml font/truetype;

# 开启高效传输模式
sendfile on;
# 隐藏Nginx版本号
server_tokens off;
# 限制最大上传大小
client_max_body_size 20M;

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

include ./config/*.config;
}

配置子配置文件 nginx_xxx.config

除主配置文件nginx.config以外,子配置下的其他配置文件nginx_xxx.config
一台服务器上有多个项目需要Nginx时,可以根据配置独立的.config文件

  1. 为了方便,新建一个config文件夹
  2. config文件夹中创建一个子配置文件,例如:ngixn_blog.config
    PS:名字自定义就行,为了好区分每个子配置文件所对应的域名及端口,尽量名字规则点,我这里采用ngixn_blogx.config
    PS:nginx.config中有检测/config/*.config的文件,所以要在/usr/local/nginx/config下创建扩展名为.config的子配置文件。
  3. Nginx默认的80端口,我们直接配置IP路径就可以了。127.0.0.1
    PS:如果有特殊需求,这里可以把ip修改成自己的域名,例如:blog.renyuxin.cn
  4. 如果Nginx主端口80被其他项目直接访问占用了,则需要使用别的端口,那就得需要用到ngixn代理转发proxy_pass
    PS:我这里转发到8001端口:proxy_pass http://127.0.0.1:8001;
  5. 测试:在浏览器中输入blog.renyuxin.cn,默认找到ngixn_blog.config下的index.html。即:http://blog.renyuxin.cn/index.html
  6. 可以多配置多个ngixn_blog.config子配置文件,每个文件关联一个server,或者也可以在一个ngixn_blog.config子配置文件中配置多个server{ }
1
2
3
4
5
6
7
8
server{
listen 80;
server_name 127.0.0.1;
location ~^/ {
root html;
index index.html index.htm;
}
}

或者 有域名需求的话,可以用这个进行代理转发
PS:浏览器访问blog.renyuxin.cn时,自动转到http://127.0.0.1:8001这个路径

1
2
3
4
5
6
7
server{
listen 80;
server_name blog.renyuxin.cn;
location ~^/ {
proxy_pass http://127.0.0.1:8001;
}
}

Nginx参数

关于.config中的配置简单讲解下

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
# 指定nginx运行的用户及用户组,默认为nobody
user nobody nobody;

worker_processes 1; # 开启线程数,最大值可设逻辑CPU核数

# 指定一个nginx进程打开的最多文件描述符数目,受系统进程的最大打开文件数量限制
worker_rlimit_nofile 65535

# 定位全局错误日志文件,级别以notice显示,还有debug、info、warn、error、crit模式,debug输出最多,crir输出最少,根据实际环境而定
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

# 指定进程id的存储文件位置
pid logs/nginx.pid;

# envents配置
events {
use epoll; # 设置工作模式为epoll,除此之外还有select,poll,kqueue,rtsig和/dev/poll模式
worker_connections 1024; # 定义每个进程的最大连接数,受系统进程的最大打开文件数量限制
}

# http配置
http {

include mime.types; # 主模块指令,实现对配置文件所包含的文件的设定,可以减少主配置文件的复杂度
default_type application/octet-stream; # 核心模块指令,默认设置为二进制流,也就是当文件类型未定义时使用这种方式

# 下面代码为日志格式的设定,main为日志格式的名称,可自行设置,后面引用
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; # 引用日志main格式

client_max_body_size 20M; # 设置允许客户端请求的最大的单个文件字节数
client_header_buffer_size 32k; # 指定来自客户端请求头的headebuffer大小
client_body_temp_path /dev/shm/client_body_temp; # 指定连接请求试图写入缓存文件的目录路径
large client_header_buffers 4 32k; # 指定客户端请求中较大的消息头的缓存最大数量和大小,目前设置为4个32KB

sendfile on; # 开启高效文件传输模式
tcp_nopush on; # 开启防止网络阻塞
tcp_nodelay on; # 开启防止网络阻塞


keepalive_timeout 65; # 设置客户端连接保存活动的超时时间,设置成0为无限时间
client_header_timeout 10; # 设置客户端请求读取header超时时间
client_body_timeout 10; # 设置客户端请求body读取超时时间

# HttpGZip模块配置
gzip on; # 开启gzip压缩
gzip_min_length 1k; # 设置允许压缩的页面最小字节数
gzip_buffers 4 16k; # 申请4个单位为16K的内存作为压缩结果流缓存
gzip_http_version 1.1; # 设置识别http协议的版本,默认为1.1
gzip_comp_level 2; # 指定gzip压缩比,1-9数字越小,压缩比越小,速度越快
gzip_types text/plain application/x-javascript text/css application/xml; # 指定压缩的类型
gzip_vary on; # 让前端的缓存服务器进过gzip压缩的页面

# server配置
server {
keepalive_requests 120; # 单连接请求上限次数
listen 88; # 监听端口
server_name 111.222.333.123; # 监听地址,可以是ip,最好是域名
#server_name www.baidu.com;
charset utf-8; # 设置访问的语言编码
# 设置虚拟主机访问日志的存放路径及日志的格式为main
access_log /www/wwwlogs/111.222.333.123.log main; # 响应日志
error_log /www/wwwlogs/111.222.333.123.log main; # 错误日志

#PHP-INFO-START PHP引用配置,可以注释或修改
include enable-php-74.conf;
#PHP-INFO-END

# REWRITE-START URL重写规则引用
include /www/server/panel/vhost/rewrite/111.222.333.123.conf;
# REWRITE-END

# 设置主机基本信息
# 请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
location ~*^.+$ {
root html; # 根目录
index index.html index.htm; # 设置默认页
deny 127.0.0.1; # 拒绝的ip,黑名单
allow 172.18.5.54; # 允许的ip,白名单
}

# 禁止访问的文件或目录
location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md){
return 404;
}

# SSL证书验证目录相关设置
location ~ \.well-known{
allow all;
}

# 图片资源配置
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
error_log /dev/null;
access_log off;
}

# 网站js与css资源配置
location ~ .*\.(js|css)?$ {
expires 12h;
error_log /dev/null;
access_log off;
}

#访问异常页面配置
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

}

Nginx启动和关闭

虽然Nginx可以直接实时监听,但是每次修改完配置文件,都需要重启Nginx,新的配置才能生效

启动Nginx

1
/usr/local/nginx/sbin/nginx

关闭Nginx

1
pkill -9 nginx

常见的异常报错

问题1:nginx.conf文件中有多余的空格

nginx: [emerg] invalid number of arguments in “pid” directive in /usr/local/nginx/conf/nginx.conf:7

nginx: [emerg] invalid number of arguments in “ “ directive in /usr/local/nginx/conf/nginx.conf:7

解决方式:

检查一遍配置,删除空格,重启Nginx

问题2. nginx.conf文件的编码格式问题

nginx: [emerg] unknown directive “ server” in /usr/local/nginx/conf/./config/smart_nginx_main.config:1

解决方式:

如果再三确认配置没有问题的话,那就是编码的问题了。
我习惯用记事本编辑,但是nginx.conf文件一旦被记事本编辑保存过,就会变成含BOM头的文件,所以会报错。
记事本编辑UTF-8都会加BOM头,如果其他xxx.conf也出现这个问题,先去看看编码格式对不对。

使用其他编辑器将文件另存为 UTF-8 无BOM头 的格式编码。
我这里用的是Notepad++文本编辑器,所以直接在本地修改编码格式,方便。

``` Nginx