nginx.conf文件配置
user root;
worker_processes 2;
# debug info notice warn error crit
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid
#工作模式以及连接数配置,指令块
events {
#默认使用epoll,对于Linux来说比较适合epoll
use epoll;
#每个worker允许连接的客户端连接数
worker_connections 2048;
}
http {
#可以导入外部文件,mime.type是规定的类型
include mime.types;
default_type application/octet-stream;
#提高文件传输效率
sendfile on;
#tcp_nopush on;
#处理完请求后保持连接
keepalive_timeout 65;
#文件压缩后再传输,提高传输效率
gzip on;
#限制最小压缩,小于1字节的文件不会压缩
gzip_min_length 1;
#值越大,压缩比就越大,文件越大,压缩越多,但是cpu使用会越
gzip_comp_level 3;
#需要压缩的文件类型
gzip_types text/plain aapplication/javascript applicationcation/xml text/javascript application/x-httpd-php image/jpegtion/json;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
include shixin.conf;
}
1.user和worker_processes
user代表的是以什么身份使用此配置文件,使用root能够获得更多权限
worder_processes是工作的进程有多少个,nginx是一种异步非阻塞非阻塞的框架,简单的说,master负责管理worker,就好比企业管理和普通员工的关系,什么是异步非阻塞,如果有请求A请求worker1卡主的情况下,第一点是worker1还是可以接受请求的,第二点是worker2是正常工作的。
2.有关日志
# debug info notice warn error crit
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
没什么好说的,第一行写的是nginx的各种日志级别,剩下的就是配置日志的输出地址。
其实在我们安装的时候就指定好日志的输出位置了,这点不多说了。
3.pid
#pid logs/nginx.pid
配置的是nginx的执行pid,这段被我注释了,因为在一开始安装的时候就已经指定了。
可以拿来做nginx的平滑启动,来实现日志切割。
4.event
event是一个指令块,在nginx的配置文件当中,由众多的指令块组成
worler_connections 不是越大越号,看硬件性能,不同情况不一概而论
#工作模式以及连接数配置,指令块
events {
#默认使用epoll,对于Linux来说比较适合epoll
use epoll;
#每个worker允许连接的客户端连接数
worker_connections 2048;
}
5.http
- include
以下语句代表的是引入外部文件mime.type,当然可以自定义的引入,下面介绍不同的路由方式的时候会引入自定义的文件
include mime.types;
- keepalive_timeout 75;
这个参数代表的是一个client发起http请求之后,需要将这个链接保持活跃多长时间,如果一个文件的下载的时间超过了链接的时间是会报错的,如果链接太大也会出问题,上面有一个参数worker_connections,是设置最大连接数的,假设在我设置的超时时间内有大量的请求过来,而链接并没有关,此时就有可能导致nginx宕机。 - gzip
#文件压缩后再传输,提高传输效率 gzip on; #限制最小压缩,小于1字节的文件不会压缩 gzip_min_length 1; #值越大,压缩比就越大,文件越大,压缩越多,但是cpu使用会越 gzip_comp_level 3; #需要压缩的文件类型 gzip_types text/plain aapplication/javascript application/cation/xml text/javascript application/x-httpd-php image/jpeg tion/json;
- server
- listen,server_name
listen 80; server_name localhost;
代表的是监听localhost:80,为什么本地计算机访问域名会被localhost:80监听?首先要知道的是80端口是默认的http端口,域名也是需要解析成ip才是可以被识别的,解析成ip后会去访问目标主机,在目标主机内,/etc/hosts文件内有ip,127.0.0.1,localhost三者相互转换的配置,所以最终会被监听。
- localtion
location / { root html; index index.html index.htm; }
顾名思义,上面的监听到访问之后需要定位一个地址,这里代表的是直接以域名或者ip(省略了80端口)访问的端口会被转发到/html/index.html下。root代表的是根,需要拼接在location后面。
- listen,server_name
shixin.conf
server {
listen 89;
server_name localhost;
#解决跨域问题的四行配置,1.允许跨域的源地址,2.允许跨域的方法,3.是否允许设置cookies,4.允许设置的请求头
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Credentials 'true';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
#静态资源防盗链
#对源站点进行验证,验证请求端是否来自所配置的域名
#valid_referers *.shixin.xyz;
#如果不请求来源不对,直接抛出错误
#if($invalid_referer){
# return 404;
#}
location / {
root /home/foodie-shop;
index index.html;
}
#这种方式使用别名,隐藏了真实地址,即访问 ip:89/static即相当于访问了 /home/shixin -- > ip:89/static/img/test.jpg --> /home/shixin/img/test.jpg
location /static {
#root /home;
alias /home/shixin;
}
#这种方式会拼接为地址:/home/shixin,前端访问路径为 /shixin
location /shixin {
root /home;
}
}
server {
listen 90;
server_name localhost;
#精确匹配,只能匹配 ip:90
location = / {
root /home/foodie-shop;
index index.html;
}
#精确匹配,只能匹配:ip:90/shixin/img/test.png
location = /shixin/img/test.png {
root /home;
}
}
server {
listen 92;
server_name localhost;
#正则表达式匹配,*不区分表达式的大小写,如下规则表示所有92端以...结尾的请求都会去home/shixin目录,(猜测是全部都会转成小写去匹配)
location ~* \.(GIF|png|jpg) {
root /home/shixin;
}
}
server {
listen 93;
server_name localhost;
#^~ 以某个字符路径开头请求 ^代表非,~代表正则表达式,所以就是不使用正则表达式,使用普通的形式
#代表只允许访问/home/shixin/img下的文件
location ^~ /shixin/img {
root /home;
}
}