nginx.conf文件中的默认配置信息
worker_process 1; #此配置项全局生效
events{ #以下配置只在events部分中生效
worker_connections; 1024
}
http{ #以下配置只在http部分中生效
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server{ #以下配置只在http部分的server部分中生效
listen 80;
server_name localhost;
location /{ #以下配置只在http/server部分的location部分中生效
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html
location = /50x.html{
root html;
}
}
}
nginx.conf文件的规范结构
... #全局块
events{} #events块
http{ #http块
server{ #server块
location [pattern] {} #location块
location [pattern] {} #location块
}
server{}
}
全局块
主要配置:pid文件位置、允许的工作进程数、日志文件位置、引入其他配置文件。
- pid pidfile_path:设置存储nginx服务进程号的文件路径
- worker_processes number:设置nginx服务启动后开启的工作进程数,工作进程是用来处理客户端连接请求及url请求的
- user username groupname:授权给特定用户运行nginx服务的权限。如果没有设置则默认授权所有用户,或者user nobody nobody也是授权所有用户
- error_log log_file_path | stderr :设置存储错误日志的文件路径,或者将错误日志输出到标准错误
- include other_configfile_path:引入其他配置文件
events块
此块中的配置主要影响客户端与nginx的网络连接,对nginx的性能影响较大,需要根据实际情况灵活调整。
- worker_connections number:每个工作进程可以同时保持的最大连接数。默认500.
- access_mutex on | off :防止
- multi_access on | off :是否允许工作线程并发地接收客户端连接请求
http块
代理、缓存、日志定义等绝大多数的功能和第三方模块的配置。
- keepalive_timeout number1 number2 :当工作进程与某个客户端建立会话后,此会话的保持时间。number2将被添加到响应头中的"Keep-Alive: timeout=number2"中
- keepalive_requests:当工作进程与客户单建立会话后,在单个会话上允许处理的最大请求数。默认100。
- sendfile on | off :是否调用sendfile()方式传输文件
- sendfile_max_chunk size:每次调用sendfile()时传输数据最大字节数。如果size小于等于0,则无限制。
server块
配置虚拟主机名
基于名称的虚拟主名配置
server_name name1 name2 …:配置虚拟主机名,这里的“虚拟主机”就是server块对外提供的虚拟主机。当设置了虚拟主机名后,必须为其配置好DNS,然后客户端就可以向这个虚拟主机名发请求了。
基于IP的虚拟主机配置
server_name ip1:需要先为每台虚拟主机(即server块所代表的虚拟主机)逻辑地设计一个不同的IP地址,然后将nginx服务所在机器的网卡设置为能同时监听多个IP地址。
使用命令ifconfig ens33:0 ip1 netmask 255.255.255.0 up
来为网卡设置更多IP,其中up参数表示立即生效。
这种方式配置后,机器重启后就配置就没有了。要想让这个配置永久生效,可以将此命令添加到rc.local中:
echo "ifconfig ens33:0 ip1 netmask 255.255.255.0 up" >> /etc/rc.local
linux启动后会执行rc.local中的所有命令。
使用ifconfig工具为同一块网卡添加多个IP别名,首先查看当前使用的网卡的监听的IP地址:
ifconfig #打印网络配置信息
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.174.200 netmask 255.255.255.0 broadcast 192.168.174.255
inet6 fe80::20c:29ff:fe7f:f753 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:7f:f7:53 txqueuelen 1000 (Ethernet)
RX packets 9919 bytes 878654 (858.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 6606 bytes 982272 (959.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 224 bytes 19448 (18.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 224 bytes 19448 (18.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
可以看到ip为192.168.174.200,然后我再为此网卡添加要监听的两个IP
ifconfig ens33:0 192.168.174.211 netmask 255.255.255.0 up
ifconfig ens33:1 192.168.174.212 netmask 255.255.255.0 up
再次查看网卡信息:
ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.174.200 netmask 255.255.255.0 broadcast 192.168.174.255
inet6 fe80::20c:29ff:fe7f:f753 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:7f:f7:53 txqueuelen 1000 (Ethernet)
RX packets 9887 bytes 875982 (855.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 6579 bytes 976520 (953.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.174.211 netmask 255.255.255.0 broadcast 192.168.174.255
ether 00:0c:29:7f:f7:53 txqueuelen 1000 (Ethernet)
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.174.212 netmask 255.255.255.0 broadcast 192.168.174.255
ether 00:0c:29:7f:f7:53 txqueuelen 1000 (Ethernet)
location块
location [ = | ~ | ~* | ^~ ] uri {...}
nginx会对用户请求字符串"/server_name/uri"中的uri与location后的数据做匹配,如果根据规则,某个请求的url字符串匹配到了当前location,则服务器就会使用此location中的配置对请求进行处理。
location后的uri有两种,一种是标准匹配,一种是正则匹配。
当uri前的[]中的选项为空时,规则如下:
当一个请求到来,会先找到并记录标准匹配中匹配度最高的location。然后,进行正则匹配,找到第一个正则匹配的,即结束搜索,根据第一个正则匹配的location处理请求。如果正则匹配全部失败,则使用标准匹配度最高的location处理请求。
[]中的可选项,是用来改变匹配方式的,其中的每个可选项的含义如下:
- =,用于标准uri前,如果用户请求的uri与此uri标准匹配,则立即使用此location处理请求,结束搜索。
- ~,用于表示uri包含正则表达式,并且区分大小写
- ~*,用于表示uri包含正则表达式,并且不区分大小写
- ^~,如果找到了标准匹配度最高的location,则直接使用此location处理请求,不再进行正则匹配的搜索。
注意:如果uri包含正则表达式,则其前必须加上可选项~*或者~。
配置根目录
root path
# path为Nginx服务器接收到请求后查找资源的根目录路径。
此指令可以在http、server、location中配置,一般配置在location中。
location /data/
{
root /locationtest1; # 当location接收到uri为"/data/index.html"请求时,将在/locationtest1/data/目录下查找index.htm来响应。
}
将uri路径替换成别名
location ~^/data/(.+\.(htm|html))$
{
alias /locationtest1/other/$1
}
当location块接收到uri为"/data/index.html"的请求时,会到/locationtest1/other/目录下查找index.html文件
设置网站的默认首页
index file...
# 如果没有设置,则默认为index.html。如果设置了多个file,则首先找到了哪个file,就用哪个file响应请求。
设置默认首页可以有两个作用:用户访问网站时,请求地址可以不写首页名称;二是,对一个请求,可以根据其请求内容设置不同的首页。
设置错误页面
error_page code1 code2 [=[response]] uri
code1 code2是指错误代码
response 是指将code指定的错误代码转化为新的错误代码response
uri:响应给客户端的错误页面的路径或者网址,如果是一个网址,nginx会访问该网址获取页面并响应给客户端。
error_page 404 /404.html
# 当遇到404时,nginx会去找 nginx安装路径/html/404.html 文件,然后响应给前端。
如果错误页面404.html不是放置在 nginx安装路径/html/ 目录中的,可以在erro_page指令下加上一个location块,来指定404页面的位置:
location /404.html
{
root /myserver/errorpages/
}