Nginx基础知识

nginx优点

高并发、高性能、可扩展性好、高可靠性、热部署、BSD许可证

Nginx主要组成部分

Nginx二进制可执行文件:由各个模块源码编译成的一个文件

Nginx config文件:控制nginx行为

access.log 访问日志:记录每一条http请求信息

error.log 错误日志: 定位问题

Nginx版本

nginx主要的版本有以下几个,Nginx的开源版本和商业版本;阿里巴巴开源的Tengine,是封装了开源版本的Nginx;免费版本的OpenResty以及商业版本的OpenResty;如果开发API服务器或者防火墙,那么openResty是不错的选择。

Nginx的编译和安装

可以使用yarn或者appt直接安装nginx,但是有部分问题。官网下载完后解压进入目录

-rw-r--r--@  1 zhaozexin  staff   310K Oct 19  2022 CHANGES
-rw-r--r--@  1 zhaozexin  staff   474K Oct 19  2022 CHANGES.ru
-rw-r--r--@  1 zhaozexin  staff   1.4K Oct 19  2022 LICENSE
-rw-r--r--@  1 zhaozexin  staff    49B Oct 19  2022 README
drwxr-xr-x@ 25 zhaozexin  staff   800B Oct 19  2022 auto
drwxr-xr-x@ 11 zhaozexin  staff   352B Oct 19  2022 conf
-rwxr-xr-x@  1 zhaozexin  staff   2.5K Oct 19  2022 configure
drwxr-xr-x@  6 zhaozexin  staff   192B Oct 19  2022 contrib
drwxr-xr-x@  4 zhaozexin  staff   128B Oct 19  2022 html
drwxr-xr-x@  3 zhaozexin  staff    96B Oct 19  2022 man
drwxr-xr-x@  9 zhaozexin  staff   288B Oct 19  2022 src

./configure --prefix=/usr/local/nginx-zexin 默认安装命令,安装完成后会有安装summary,并且会生成一些中间件,这些文件会存放在objs文件夹下。

Configuration summary
  + using system PCRE2 library
  + OpenSSL library is not used
  + using system zlib library
​
  nginx path prefix: "/usr/local/nginx-zexin"
  nginx binary file: "/usr/local/nginx-zexin/sbin/nginx"
  nginx modules path: "/usr/local/nginx-zexin/modules"
  nginx configuration prefix: "/usr/local/nginx-zexin/conf"
  nginx configuration file: "/usr/local/nginx-zexin/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx-zexin/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx-zexin/logs/error.log"
  nginx http access log file: "/usr/local/nginx-zexin/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

进入objs文件后可以看到

total 176
-rw-r--r--  1 zhaozexin  staff    40K Jul 12 16:07 Makefile
-rw-r--r--  1 zhaozexin  staff    26K Jul 12 16:07 autoconf.err
-rw-r--r--  1 zhaozexin  staff   5.5K Jul 12 16:07 ngx_auto_config.h
-rw-r--r--  1 zhaozexin  staff   531B Jul 12 16:06 ngx_auto_headers.h
-rw-r--r--  1 zhaozexin  staff   5.7K Jul 12 16:06 ngx_modules.c
drwxr-xr-x  9 zhaozexin  staff   288B Jul 12 16:07 src

所有模块编译进Nginx中的文件都会添加在ngx_modules.c中,进入后可以看到

#include <ngx_config.h>
#include <ngx_core.h>
​
extern ngx_module_t  ngx_core_module;
extern ngx_module_t  ngx_errlog_module;
extern ngx_module_t  ngx_conf_module;
extern ngx_module_t  ngx_regex_module;
extern ngx_module_t  ngx_events_module;
extern ngx_module_t  ngx_event_core_module;
extern ngx_module_t  ngx_kqueue_module;
extern ngx_module_t  ngx_http_module;
extern ngx_module_t  ngx_http_core_module;
extern ngx_module_t  ngx_http_log_module;
extern ngx_module_t  ngx_http_upstream_module;
extern ngx_module_t  ngx_http_static_module;
extern ngx_module_t  ngx_http_autoindex_module;
extern ngx_module_t  ngx_http_index_module;
extern ngx_module_t  ngx_http_mirror_module;
extern ngx_module_t  ngx_http_try_files_module;
extern ngx_module_t  ngx_http_auth_basic_module;
extern ngx_module_t  ngx_http_access_module;
extern ngx_module_t  ngx_http_limit_conn_module;
extern ngx_module_t  ngx_http_limit_req_module;
extern ngx_module_t  ngx_http_geo_module;
extern ngx_module_t  ngx_http_map_module;
extern ngx_module_t  ngx_http_split_clients_module;
extern ngx_module_t  ngx_http_referer_module;
extern ngx_module_t  ngx_http_rewrite_module;
extern ngx_module_t  ngx_http_proxy_module;
extern ngx_module_t  ngx_http_fastcgi_module;
extern ngx_module_t  ngx_http_uwsgi_module;
extern ngx_module_t  ngx_http_scgi_module;
extern ngx_module_t  ngx_http_memcached_module;
extern ngx_module_t  ngx_http_empty_gif_module;
extern ngx_module_t  ngx_http_browser_module;
extern ngx_module_t  ngx_http_upstream_hash_module;
extern ngx_module_t  ngx_http_upstream_ip_hash_module;
extern ngx_module_t  ngx_http_upstream_least_conn_module;
extern ngx_module_t  ngx_http_upstream_random_module;
extern ngx_module_t  ngx_http_upstream_keepalive_module;
extern ngx_module_t  ngx_http_upstream_zone_module;
extern ngx_module_t  ngx_http_write_filter_module;
extern ngx_module_t  ngx_http_header_filter_module;
extern ngx_module_t  ngx_http_chunked_filter_module;
extern ngx_module_t  ngx_http_range_header_filter_module;
extern ngx_module_t  ngx_http_gzip_filter_module;
"ngx_modules.c" 168L, 5859B

返回主目录下执行make编译命令,编译完成后就会生成可执行的二进制文件和中间文件,可以在objs中看到,这就是Nginx的安装文件,当我们进行Nginx的版本升级的时候,不能直接make install ,需要在这里将文件拷贝到安装目录中。所有的中间文件都放在src中。

首次安装可以使用make install命令进行安装,完成后进入prefix执行的目录

total 0
drwxr-xr-x@ 17 zhaozexin  staff   544B Jul 12 16:23 conf
drwxr-xr-x@  4 zhaozexin  staff   128B Jul 12 16:23 html
drwxr-xr-x@  2 zhaozexin  staff    64B Jul 12 16:23 logs
drwxr-xr-x@  3 zhaozexin  staff    96B Jul 12 16:23 sbin

access.log\error.log 在log目录下,二进制文件在 sbin目录下,决定功能的config文件在conf目录下。

conf目录内容:

total 136
-rw-r--r--@ 1 zhaozexin  staff   1.1K Jul 12 16:23 fastcgi.conf
-rw-r--r--@ 1 zhaozexin  staff   1.1K Jul 12 16:23 fastcgi.conf.default
-rw-r--r--@ 1 zhaozexin  staff   1.0K Jul 12 16:23 fastcgi_params
-rw-r--r--@ 1 zhaozexin  staff   1.0K Jul 12 16:23 fastcgi_params.default
-rw-r--r--@ 1 zhaozexin  staff   2.8K Jul 12 16:23 koi-utf
-rw-r--r--@ 1 zhaozexin  staff   2.2K Jul 12 16:23 koi-win
-rw-r--r--@ 1 zhaozexin  staff   5.2K Jul 12 16:23 mime.types
-rw-r--r--@ 1 zhaozexin  staff   5.2K Jul 12 16:23 mime.types.default
-rw-r--r--@ 1 zhaozexin  staff   2.6K Jul 12 16:23 nginx.conf
-rw-r--r--@ 1 zhaozexin  staff   2.6K Jul 12 16:23 nginx.conf.default
-rw-r--r--@ 1 zhaozexin  staff   636B Jul 12 16:23 scgi_params
-rw-r--r--@ 1 zhaozexin  staff   636B Jul 12 16:23 scgi_params.default
-rw-r--r--@ 1 zhaozexin  staff   664B Jul 12 16:23 uwsgi_params
-rw-r--r--@ 1 zhaozexin  staff   664B Jul 12 16:23 uwsgi_params.default
-rw-r--r--@ 1 zhaozexin  staff   3.5K Jul 12 16:23 win-utf

sbin目录:

total 1640
-rwxr-xr-x  1 zhaozexin  staff   819K Jul 12 16:23 nginx

Nginx配置语法

通用语法
  1. 配置文件由指令和指令块构成

  2. 每条指令以;分号结尾,指令与参数之间用空格符号分隔

  3. 指令块以{} 大括号将多条指令组织在一起

  4. include语句允许组合多个配置文件以提升可维护性

  5. 使用#符号添加注释,提高可读性

  6. 使用$符号使用变量

  7. 部分指令的 参数支持正则表达式

配置参数:时间单位

ms:millisecond、 d:days、 s:seconds、 w:weeks、 m:minutes、M:months,30 days、h:hours, y:years、365 days

配置参数:空间单位

bytes、k/K: kilobytes、 m/M:megabytes、g/G:gigabytes

Nginx命令行

 重载配置文件

./nginx -s reload 可以在不停止服务的情况下使用最新更改的配置内容

热部署

热部署就是在不停止服务的情况下,更新nginx的二进制文件,首先cp nginx文件为nginx.old, 然后把最新的二进制文件mv 过来。给master进程发送消息,查看进程命令是 ps -ef |grep nginx, 找到master process的id号,kill - USR2 process_id, 这时候会再启动一个master进程,使用新的配置文件,这时候老的进程就不再监听对应的端口了。需要发送kill -WINCH process_id命令告诉老的master进程优雅的关闭。关闭意味着不再接受新的命令,全部切换到新的master中,这时候如果新的出现问题,可以使用reload命令重新拉起老的进程,完成版本回退。

日志切割文件

切割日志文件需要执行两个步骤,首先备份log中的access.log文件,然后使用./nginx -s reopen 命令重新打开,就会完成日期切割。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值