nginx 学习笔记

nginx进程模型:

  1. nginx进程分为主进程(master)和工作进程(worker)
  2. worker 进程可以配置多个,在配置文件 /conf/nginx.conf 中 worker_processes 参数
  3. 修改完配置文件需要重启nginx,/sbin目录下 nginx -s reload
    image

events 指令快

events {
	# 默认使用的就是linux的epoll模型(多路复用器,异步非阻塞),可以不写
	use epoll;
	# 每个worker允许链接的客户端最大连接数
    worker_connections  1024;
}

nginx.conf

  • 配置结构
    image

  • 解读

# 设置worker进程的用户,指的linux中的用户,会涉及到nginx操作目录或文件的一些权限,默认为nobody
#user  nobody;

# worker进程工作数设置,一般来说CPU有几个,就设置几个,或者设置为N-1也行
worker_processes  1;

# nginx 日志级别 debug | info | notice | warn | error | crit | alert | emerg,错误级别从左到右越来越大
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

# 设置nginx进程 pid
#pid        logs/nginx.pid;

# 设置工作模式
events {

# 默认使用epoll
    use epoll;
# 每个worker允许连接的客户端最大连接数
    worker_connections  10240;
}

# http 是指令块,针对http网络传输的一些指令配置
http {

# include 引入外部配置,提高可读性,避免单个配置文件过大
    include       mime.types;
    default_type  application/octet-stream;

# 设定日志格式,main为定义的格式名称,如此 access_log 就可以直接使用这个变量了
# $remote_addr          客户端ip
# $remote_user          远程客户端用户名,一般为:’-’
# $time_local           时间和时区
# $request              请求的url以及method
# $status               响应状态码
# $body_bytes_send      响应客户端内容字节数
# $http_referer         记录用户从哪个链接跳转过来的
# $http_user_agent      用户所使用的代理,一般来时都是浏览器
# $http_x_forwarded_for 通过代理服务器来记录客户端的ip
    #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使用高效文件传输,提升传输性能。启用后才能使用,tcp_nopush是指当数据表累积一定大小后才发送,提高了效率。
    sendfile        on;
    #tcp_nopush     on;

# keepalive_timeout设置客户端与服务端请求的超时时间,保证客户端多次请求的时候不会重复建立新的连接,节约资源损耗。
    #keepalive_timeout  0;
    keepalive_timeout  65;

# gzip启用压缩,html/js/css压缩后传输会更快
    #gzip  on;

# server可以在http指令块中设置多个虚拟主机
    server {
        listen       80;                          # listen 监听端口
        server_name  localhost;                   # server_name localhost、ip、域名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {                            # location 请求路由映射,匹配拦截
            root   html;                        # root 请求位置
            index  index.html index.htm;        # index 首页设置
        }

        #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;
        }
    }
}

  • gzip设置
gzip on; #开启gzip压缩功能,目的:提高传输效率,节约带宽

gzip_min_length 1; #限制最小压缩,小于1字节文件不会压缩

giz_comp_level 3; #压缩比(1~9),定义压缩的级别,文件越大压缩越多,但cpu使用会越多

#定义压缩文件的类型
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/json;

nginx.conf核心配置文件中可以使用include引入其他配置文件

image

  • imooc.conf文件
server {
        listen       88;
        server_name  localhost;

        location / {
            root   /home/foodie-shop;
            index  index.html;
        }

        location /imooc {
            root   /home;
        }

        location /static {
            alias /home/imooc;
        }
}
  • root与alias
假设服务器路径为:/home/imooc/files/img/face.png
配置的时候为:
location /imooc {
    root /home
}
用户请求访问的时候为: url:port/imooc/files/img/face.png

alias可以为你的路径做一个别名:对用户透明
上面配置就可以改为:
location /hello {
    alias /home/imooc
}
用户访问的时候请求为  url:port/hello/files/img/face.png,相当于为目录imooc做一个自定义的别名。
  • location 的匹配规则
1、空格:默认匹配,普通匹配
location / {
     root /home;
}

2、=:精确匹配
location = /imooc/img/face1.png {
    root /home;
}

3、~*:匹配正则表达式,不区分大小写
#符合图片的显示
location ~* .(GIF|jpg|png|jpeg) {
    root /home;
}

4、~:匹配正则表达式,区分大小写
#GIF必须大写才能匹配到
location ~ .(GIF|jpg|png|jpeg) {
    root /home;
}

5、^~:以某个字符路径开头
location ^~ /imooc/img {
    root /home;
}

nginx.pid打开失败及失效的解决方案

重新配置核心配置文件路径
./nginx -c nginx.conf文件路径

nginx常用命令

./nginx -s stop 暴力关闭,还在请求的情况下会强制断开
./nginx -s quit 优雅关闭,会等请求完了再关闭
./nginx -t 检查配置文件是否正确
./nginx -v 查看版本号
./nginx -h/-? 命令帮助
posted @ 2021-08-26 18:06  金盛年华  阅读(50)  评论(0编辑  收藏  举报