美文网首页
Nginx学习

Nginx学习

作者: 知道的太少 | 来源:发表于2020-06-29 22:56 被阅读0次

Nginx组成部分

  • nginx二进制可执行文件
  • nginx.conf配置文件
  • access.log访问日志
  • error.log错误日志

Nginx文件结构

.
├── client_body_temp [error opening dir]
├── conf
│   ├── fastcgi.conf
│   ├── fastcgi.conf.default
│   ├── fastcgi_params
│   ├── fastcgi_params.default
│   ├── koi-utf
│   ├── koi-win
│   ├── mime.types
│   ├── mime.types.default
│   ├── nginx.conf
│   ├── nginx.conf.default
│   ├── scgi_params
│   ├── scgi_params.default
│   ├── uwsgi_params
│   ├── uwsgi_params.default
│   └── win-utf
├── fastcgi_temp [error opening dir]
├── html
│   ├── 50x.html
│   └── index.html
├── logs
│   ├── access.log
│   ├── error.log
│   └── nginx.pid
├── proxy_temp [error opening dir]
├── sbin
│   ├── nginx
│   └── stap-nginx
├── scgi_temp [error opening dir]
├── tapset
│   ├── nginx.stp
│   └── ngx_lua.stp
└── uwsgi_temp [error opening dir]

Nginx配置文件

Nginx配置文件.jpg

三个重要的location匹配:

# 直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。
# 这里是直接转发给后端应用服务器了,也可以是一个静态首页
# 第一个必选规则
location = / {
    proxy_pass http://tomcat:8080/index
}

# 第二个必选规则是处理静态文件请求,这是 nginx 作为 http 服务器的强项
# 有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用
location ^~ /static/ {
    root /webroot/static/;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
    root /webroot/res/;
}

# 第三个规则就是通用规则,用来转发动态请求到后端应用服务器
# 非静态文件请求就默认是动态请求,自己根据实际把握
# 毕竟目前的一些框架的流行,带.php、.jsp后缀的情况很少了
location / {
    proxy_pass http://tomcat:8080/
}
捕获.PNG
nginx配置文件http配置指令块.PNG

Nginx命令行

捕获.PNG

Nginx命令行使用

  • 重载配置文件
nginx -s reload
  • 热部署(更换新版本nginx)
    备份现有nginx二进制执行文件
    用新版本的nginx二进制文件覆盖当前的nginx二进制文件
    向当前的master进程发送USR2信号(收到信号后的master进程会用新的nginx二进制文件再起一个master进程)
    向旧的master进程发送WINCH信号,告诉它关闭它的worker进程
  • 切割日志文件
    重命名当前日志文件
    执行nginx - s reopen

Nginx 静态文件服务

worker_processes  1;        #nginx worker 数量
error_log logs/error.log;   #指定错误日志文件路径
events {
    worker_connections 1024;
}
http {
    include       mime.types;
    #default_type  application/octet-stream;
    # 这个将为打开文件指定缓存,默认是没有启用的,max 指定缓存数量,
    # 建议和打开文件数一致,inactive 是指经过多长时间文件没被请求后删除缓存。
    open_file_cache max=204800 inactive=20s;
    #日志格式,其中“main”为格式名。
    log_format main '$remote_addr - $remote_user  [$time_local]  '
                    ' "$request"  $status  $body_bytes_sent  '
                    ' "$http_referer"  "$http_user_agent" ';
    # open_file_cache 指令中的inactive 参数时间内文件的最少使用次数,
    # 如果超过这个数字,文件描述符一直是在缓存中打开的,如下,如果有一个
    # 文件在inactive 时间内一次没被使用,它将被移除。
    open_file_cache_min_uses 1;

    # 这个是指多长时间检查一次缓存的有效信息
    open_file_cache_valid 30s;

    # 默认情况下,Nginx的gzip压缩是关闭的, gzip压缩功能就是可以让你节省不
    # 少带宽,但是会增加服务器CPU的开销哦,Nginx默认只对text/html进行压缩 ,
    # 如果要对html之外的内容进行压缩传输,我们需要手动来设置。
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types text/plain application/x-javascript text/css application/xml;

    server {
            listen       80;
            server_name www.test.com;
            charset utf-8;
            #使用log
            access_log logs/test.log main;
            root   /data/www.test.com;
            index  index.html index.htm;
            
            #可用于共享文件目录
            #autoindex on;
           
            #限制nginx向浏览器的传输速度
            #set $limit_rate 1k;
           }
}

nginx指定文件路径的规则:
nginx指定文件路径有两种方式root和alias,指令的使用方法和作用域:
[root]
语法:root path
默认值:root html
配置段:http、server、location、if
[alias]
语法:alias path
配置段:location

root与alias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上。
root的处理结果是:root路径+location路径
alias的处理结果是:使用alias路径替换location路径
alias是一个目录别名的定义,root则是最上层目录的定义。
还有一个重要的区别是alias后面必须要用“/”结束,否则会找不到文件的。。。而root则可有可无。

Nginx反向代理

worker_processes 1;

pid logs/nginx.pid;
error_log logs/error.log warn;

events {
    worker_connections 3000;
}

http {
    include mime.types;
    server_tokens off;
    proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;
    ## 下面配置反向代理的参数
    server {
        listen    8866;

        ## 1. 用户访问 http://ip:port,则反向代理到 https://github.com
        location / {
            
            proxy_pass  https://github.com;
            proxy_redirect     off;

            proxy_cache my_cache;
            # 状态码为200 304的缓存12小时
            proxy_cache_valid 200 304 12h;
            # 其余的缓存10分钟
            proxy_cache_valid any 10m;
            # 定义缓存的key
            proxy_cache_key $scheme$proxy_host$uri$is_args$args;
            
            #添加头
            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }

        ## 2.用户访问 http://ip:port/README.md,则反向代理到
        ##   https://github.com/.../README.md
        location /README.md {
            proxy_set_header  X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass https://github.com/moonbingbing/openresty-best-practices/blob/master/README.md;
        }
    }
}

Nginx进程管理

捕获.PNG

nginx的命令行是通过pid文件来向对应master进程发信号的。

Nginx reload过程

捕获.PNG
捕获.PNG

相关文章

网友评论

      本文标题:Nginx学习

      本文链接:https://www.haomeiwen.com/subject/izkgfktx.html