Nginx

配置yum源

yum install gcc gcc-c++ autoconf make -y

安装nginx第三方库,静态资源压缩功能所需要的gzip lib库,nignx需要支持URL重写,所需的pcre库,perl开发的依赖库,以及nginx搭建加密站点 https,所需的openssl依赖库等

yum install zlib zlib-devel openssl-devel pcre pcre-devel httpd-tools vim -y

关闭防火墙以及selinux

编译安装nginx

# wget http://nginx.org/download/nginx-1.18.0.tar.gz

解压缩

[root@master opt]# tar -zxvf nginx-1.18.0.tar.gz 

[root@master nginx-1.18.0]# ls -l
total 764
drwxr-xr-x. 6 1001 1001 4096 Jul 29 04:21 auto 检测系统模块依赖信息
-rw-r--r--. 1 1001 1001 302863 Apr 21 2020 CHANGES 记录nginx的变化
-rw-r--r--. 1 1001 1001 462213 Apr 21 2020 CHANGES.ru
drwxr-xr-x. 2 1001 1001 168 Jul 29 04:21 conf 存放nginx主配置文件的目录
-rwxr-xr-x. 1 1001 1001 2502 Apr 21 2020 configure 可执行脚本 释放编译文件的定制脚本
drwxr-xr-x. 4 1001 1001 72 Jul 29 04:21 contrib 提供vim插件,让配置文件颜色区分,更友好
drwxr-xr-x. 2 1001 1001 40 Jul 29 04:21 html存放了标准的htnl页面文件
-rw-r--r--. 1 1001 1001 1397 Apr 21 2020 LICENSE
drwxr-xr-x. 2 1001 1001 21 Jul 29 04:21 man
-rw-r--r--. 1 1001 1001 49 Apr 21 2020 README
drwxr-xr-x. 9 1001 1001 91 Jul 29 04:21 src 存放了nginx

编译三部曲

1.进入软件源代码目录,执行编译脚本文件,如制定安装路径,开启额外功能

[root@master nginx-1.18.0]# ./configure --prefix=/opt/nginx-1.8 --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-threads --with-file-aio

2.下一步安装

[root@master nginx-1.18.0]# make

3.安装

[root@master nginx-1.18.0]# make install

[root@master nginx-1.8]# ls
conf html logs sbin

conf 存放nginx的配置文件 nginx.conf

html 存放nginx的网页根目录文件,存放站点的讲台文件数据

logs 存放nginx的各种日志目录

sbin 存放该软件的可执行命令

[root@master ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

启动nginx

[root@master sbin]# /opt/nginx-1.8/sbin/ nginx
[root@master sbin]# ps -ef | grep nginx |grep -v greproot      26968      1  0 05:20 ?        00:00:00 nginx: master process /opt/nginx-1.8/sbin/nginxnobody    26969  26968  0 05:20 ?        00:00:00 nginx: worker process

直接启动会失败

将nginx加入环境变量

[root@master profile.d]# cat /etc/profile.d/nginx.sh export PATH="$PATH:/opt/nginx-1.8/sbin/"

退出会话,重新登陆

[root@master ~]# echo $PATH/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/nginx-1.8/sbin/:/root/bin

停止nginx

nginx -s stop

加载配置文件,不用重启

nginx -s reload

nginx配置文件

[root@master nginx-1.8]# cat  conf/nginx.conf#user  nobody;worker_processes  1;#error_log  logs/error.log;#error_log  logs/error.log  notice;#error_log  logs/error.log  info;#pid        logs/nginx.pid;        # 全局指令events {    worker_connections  1024;      # 局部指令}# http语法块 核心功能配置http {    include       mime.types;    default_type  application/octet-stream;    #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        on;    #tcp_nopush     on;    #keepalive_timeout  0;    keepalive_timeout  65;    #gzip  on;    # server{}语句块    虚拟主机配置    server {        listen       80;        server_name  localhost;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {            root   html;            index  index.html index.htm;        }        #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;        }        # proxy the PHP scripts to Apache listening on 127.0.0.1:80        #        #location ~ \.php$ {        #    proxy_pass   http://127.0.0.1;        #}        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000        #        #location ~ \.php$ {        #    root           html;        #    fastcgi_pass   127.0.0.1:9000;        #    fastcgi_index  index.php;        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;        #    include        fastcgi_params;        #}        # deny access to .htaccess files, if Apache's document root        # concurs with nginx's one        #        #location ~ /\.ht {        #    deny  all;        #}    }    # another virtual host using mix of IP-, name-, and port-based configuration    #    #server {    #    listen       8000;    #    listen       somename:8080;    #    server_name  somename  alias  another.alias;    #    location / {    #        root   html;    #        index  index.html index.htm;    #    }    #}    # HTTPS server    #    #server {    #    listen       443 ssl;    #    server_name  localhost;    #    ssl_certificate      cert.pem;    #    ssl_certificate_key  cert.key;    #    ssl_session_cache    shared:SSL:1m;    #    ssl_session_timeout  5m;    #    ssl_ciphers  HIGH:!aNULL:!MD5;    #    ssl_prefer_server_ciphers  on;    #    location / {    #        root   html;    #        index  index.html index.htm;    #    }    #}

Nginx命令行

1.nginx启停指令 ,-s 给nginx进程发送某种信号nginx # 初次启动nginx -s stopnginx -s reload # 平滑重启,利用reload可以在不重启nginx进程的情况下,重新加载配置文件2.查看nginx的帮助信息[root@master conf]# nginx -hnginx version: nginx/1.18.0Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]Options:  -?,-h         : this help   -v            : show version and exit  -V            : show version and configure options then exit  # 列出nginx版和编译参数信息  -t            : test configuration and exit #检查nginx配置文件,语法是否正确  -T            : test configuration, dump it and exit# 检查配置,输出配置信息  -q            : suppress non-error messages during configuration testing # 在检测配置文件期间屏蔽非错误信息  -s signal     : send signal to a master process: stop, quit, reopen, reload # 给nginx主进程发送信号 停止、优雅停止、重读配置文件、重新记录配置文件  -p prefix     : set prefix path (default: /opt/nginx-1.8/)  # 设置nginx目录前缀  -c filename   : set configuration file (default: conf/nginx.conf) # 指定配置文件启动  -g directives : set global directives out of configuration file # 覆盖设置一些默认参数

Nginx master信号传递

1.master 主进程是不处理请求的。而是分配请求发给worker进程,主机进程负责重启,热加载,热部署等2.master是根据nginx.conf 中work_process定义启动时创建的工作进程数3.当worker运行后,master就处于一个等待的状态,等待用户的请求来临,或者系统信号4.系统管理员可以发送kill指令,或者nginx -s信号,这样的形式操纵nginx

nginx信号集

nginx -s 对应的信号功能如下参数		信号		含义stop	TERM		强制关闭nginx服务null	INT			强制关闭整个nginx服务quit    QUIT 		优雅的关闭整个服务reopen	USR1		重新打开日志记录reload  HUB			重新读取配置文件,并且优雅的退出旧的worker

Nginx热部署功能

高可用的特性,热部署

热部署的特点:在不重启或者关闭进程的情况下,新的应用直接替换旧的应用

更换nginx的二进制命令版本热部署大致流程1.备份旧的程序,二进制文件 备份命令 ,/opt/nginx-1.8/sbin/nginx2.编译安装新的二进制文件,覆盖旧的二进制文件(再装一个版本的nginx,且替换旧的nginx命令)3.发送USR2信号发给旧的master进程4.发送WINCH信号给旧的matser进程5.发送QUIT信号给旧的master进程

环境准备

1。旧的nginx程序版本[root@master ~]# nginx -vnginx version: nginx/1.18.02.准备一个新的nginx版本wget http://nginx.org/download/nginx-1.21.1.tar.gz备份旧的二进制命令[root@master sbin]# mv nginx nginx1.83.检查旧的二进制命令参数[root@master ~]# nginx1.8 -Vnginx version: nginx/1.18.0built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) built with OpenSSL 1.0.2k-fips  26 Jan 2017TLS SNI support enabledconfigure arguments: --prefix=/opt/nginx-1.8 --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-threads --with-file-aio4.下载编译安装新版本[root@master opt]# wget http://nginx.org/download/nginx-1.21.1.tar.gz[root@master opt]# cd nginx-1.21.1[root@master nginx-1.21.1]# ./configure --prefix=/opt/nginx-1.8 --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-threads --with-file-aio# 编译三部曲[root@master nginx-1.21.1]# make && make install 5.检查新的版本的nginx信息[root@master nginx-1.8]# cd sbin[root@master sbin]# lsnginx  nginx1.8[root@master sbin]# nginx -vnginx version: nginx/1.21.16.发送一个USR2信号给旧的matser process,租用使得nginx旧的版本停止接收用户请求,并且切换为新的nginx版本[root@master ~]# kill -USR2 'cat /opt/nginx-1.8/logs/nginx.pid'8.发送WINCH信号,给旧的master进程,让旧的master进程优雅的退出# kill -WINCH 'cat /opt/nginx1.8/logs/nginx.pid.oldbin'

nginx工作模式master-worker

nginx热部署功能,在不影响用户体验下,进行软件版本升级,也就是不主动的杀死worker,就能够更换软件的二进制命令

Nginx日志切割

 ab -kc 10 # 并发-n 100 http://10.35.202.10

日志切割,能够控制单个日志文件的大小,便于对日志进行管理

[root@master logs]# lltotal 20-rw-r--r--. 1 root root 10480 Jul 30 03:15 access.log-rw-r--r--. 1 root root  1260 Jul 30 02:20 error.log-rw-r--r--. 1 root root     6 Jul 30 02:21 nginx.pid1. 对nginx日志进行切割给当前日志文件重命名[root@master logs]# mv access.log "access.log$(date +"%Y-%m-%d")" 2.发送信号给nginx主进程,给他发送一个重新打开的信号,让nginx生成新的日志文件[root@master logs]# nginx -s reopen  # 这个命令等同于 kill -USR1 ‘cat nginx.pid’3. 注意在以上的nginx重命名日志切割链,不要立即对文件修改,而是要注意等待几秒钟,应为nginx的工作模式特点,master下发指令给worker去干活,刚发指令的时候,只是一个标记,当业务量很大的时候,修改操作有点慢。4.日志切割主要以定时任务及脚本[root@bogon sbin]# cat cut_nginx_log.sh#! /bin/bash#nginxlogs_path="/opt/nginx/logs/"mkdir -p ${logs_path}$(date -d "yesterday" +"% Y")/$(date -d "yesterday" + "%m")mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" + "%Y-%m-%d").logkill -USR1 ` cat /opt/tngx232/logs/nginx.pid`定时任务 * * * * *分 时 日 月 周#crontab -e0 0 * * * /bin/bash  /hekang/cut_log.sh

Nginx虚拟主机

虚拟主机指的就是一个独立的站点,具有独立的域名,有完整的www服务,例如网站、FTP、邮件等。

Nginx支持多虚拟主机,在一台机器上可以运行完全独立的多个站点。

虚拟主机可以在一台服务器,同一个Nginx进程上运行多个网站。

nginx.confuser nobody;http{	# 在http{}标签内定义虚拟主机	# 默认加载 自上而下	server{		}	# 第二个虚拟主机	# 一个server可以理解为一个网站	server{		}}
#user  nobody;worker_processes  1;#error_log  logs/error.log;#error_log  logs/error.log  notice;#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    #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        on;    #tcp_nopush     on;    #keepalive_timeout  0;    keepalive_timeout  65;    #gzip  on;    server {        # 定义虚拟主机的端口号,用户访问网站的入口        listen       80;        # 填写主机的域名配置,没有域名可以写localhost 或者 _ 也行        server_name  localhost;        # 定义网站编码        charset utf-8;        #access_log  logs/host.access.log  main;# nginx的路径匹配规则,任何的nginx请求都会进入如下location的配置去定义的目录找资料        location / {           # root 关键词 是定义网页的根目录 这个html是以nginx安装的路径为相对            root   html;            # index关键词 定义nginx首页文件名字,默认找哪个文件            index  index.html index.htm;        }        #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;        }        # proxy the PHP scripts to Apache listening on 127.0.0.1:80        #        #location ~ \.php$ {        #    proxy_pass   http://127.0.0.1;        #}        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000        #        #location ~ \.php$ {        #    root           html;        #    fastcgi_pass   127.0.0.1:9000;        #    fastcgi_index  index.php;        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;        #    include        fastcgi_params;        #}        # deny access to .htaccess files, if Apache's document root        # concurs with nginx's one        #        #location ~ /\.ht {        #    deny  all;        #}    }}

静态网站

1. nginx.conf server {        # 定义虚拟主机的端口号,用户访问网站的入口        listen       80;        # 填写主机的域名配置,没有域名可以写localhost 或者 _ 也行        server_name  localhost;        # 定义网站编码        charset utf-8;        #access_log  logs/host.access.log  main;# nginx的路径匹配规则,任何的nginx请求都会进入如下location的配置去定义的目录找资料        location / {           # root 关键词 是定义网页的根目录 这个html是以nginx安装的路径为相对            root   /www/html;            index  index.html index.htm;        }2.[root@master html]# pwd/www/html[root@master html]# lsindex.html3. nginxnginx -s reload

Nginx静态资源压缩

nginx支持gzip压缩功能,经过gzip压缩之后的页面图片动态图等这类静态文件,能够压缩为原本的30%甚至更小,用户访问网站的体验会好很多

http://10.35.202.10/cc.txt

nginx.conf

gzip  on;gzip_http_version 1.1;gzip_comp_level 4;gzip_types text/plain application/javascript application/x-javascript text/cssapplication/xml test/javascript application/x-httpd-php image/jpeg image/gif image/png;
[root@master html]# nginx -s reload  

Nginx基于ip的多虚拟主机

# ifconfig ens32:1 10.35.202.11 netmask 255.255.255.0 broadcast 10.35.202.255 up[root@master html]# ifconfig ens32:2 10.35.202.12 netmask 255.255.255.0 broadcast 10.35.202.255 up[root@master html]# ifconfig | grep "inet 10.35"        inet 10.35.202.10  netmask 255.255.255.0  broadcast 10.35.202.255        inet 10.35.202.11  netmask 255.255.255.0  broadcast 10.35.202.255        inet 10.35.202.12  netmask 255.255.255.0  broadcast 10.35.202.255

添加nginx的配置,添加多个server{}标签,让nginx支持基于ip的多虚拟主机,返回站点内容

1.给nginx添加include包含语法,让其他目录下的配置文件参数,导入到nginx.conf中,这样的写法,能够让nginx每一个配置文件,看起来更简洁,更清晰修改nging.conf2.和conf文件夹同目录# mkdir extra # 添加多个基于ip的虚拟主机配置第一个基于ip的虚拟主机,写在nginx.conf 中,部分代码如下:    server {        listen       80;        server_name  localhost;        charset utf-8;        #access_log  logs/host.access.log  main;        location / {            root   /www/10;            index  index.html index.htm;        }3.添加10.35.202.11 虚拟主机的配置[root@master extra]# cat 11.conf server{listen 10.35.202.11:80;server_name _;location / {        root /www/11;        index index.html;}}4. [root@master extra]# vi 12.confserver{listen 10.35.202.12:80;server_name _;location / {        root /www/12;        index index.html;}}# nginx -t  检查语法5.nginx.conf include extra/*.conf;       6. 准备3个基于ip的站点内容[root@master ~]# echo " I'm 10 nginx test." > /www/10/index.html[root@master ~]# echo " I'm 11 nginx test." > /www/11/index.html [root@master ~]# echo " I'm 12 nginx test." > /www/12/index.html         7.重新加载nginx配置          nginx -s stop        nginx        

Nginx基于多域名的虚拟主机配置

基于多ip的虚拟主机,用的不多。

多域名

​ -前提条件:

要么配置DNS服务器,将你想用的域名解析到对应的ip

使用hosts文件,进行本地访问测试

1.环境准备,在客户端,修改hosts文件[root@master nginx-1.8]# cat /etc/hosts127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4::1         localhost localhost.localdomain localhost6 localhost6.localdomain610.35.202.10 hekang520.com10.35.202.10 hekang666.cn10.35.202.10 hekang999.com server {        # 定义虚拟主机的端口号,用户访问网站的入口        listen       10.35.202.10:80;        # 填写主机的域名配置,没有域名可以写localhost 或者 _ 也行        server_name  hekang520.com;        # 定义网站编码        charset utf-8;        #access_log  logs/host.access.log  main;# nginx的路径匹配规则,任何的nginx请求都会进入如下location的配置去定义的目录找资料        location / {           # root 关键词 是定义网页的根目录 这个html是以nginx安装的路径为相对            root   /www/10;            index  index.html index.htm;        } server {            listen       10.35.202.10:80;            server_name  hekang666.cn;            location / {            root    /www/10;            index  index.html index.htm;}        [root@master conf]# curl hekang666.cn && curl hekang520.com I'm 10 nginx test. I'm 10 nginx test.

nginx多虚拟主机日志定义

不同的ip

不同的端口

不同的域名

​ 都能够访问到一台计算机的配置,nginx的多虚拟主机的功能配置,利用虚拟主机可以在一台服务器上,运行多个站点配置。

针对每一个虚拟主机都配置好access.log对每一个主机进行方可信息管理

server {listen 83;server_name hekang520.com;access_log logs/test01.log;location / {         root /www/test1/;         index index.html index.htm;        }}[root@ansible extra]# cat test2\.conf server {   listen 82;   server_name _;access_log logs/test02.log;   location / {       root /www/test2/;       index index.html index.htm;        }}

Nginx之Access日志功能

http {    include       mime.types;    default_type  application/octet-stream;	# 定义日志的内容格式    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/ccaccess.log  main;  }[root@ansible logs]# lsaccess.log  ccaccess.log  error.log  nginx.pid  test01.log  test02.log[root@ansible ~]# cat /etc/nginx-1.8cat: /etc/nginx-1.8: No such file or directory[root@ansible ~]# cat /opt/nginx-1.8/logs/ccaccess.log 192.168.200.1 - - [31/Jul/2021:01:26:34 -0400] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36" "-"192.168.200.1 - - [31/Jul/2021:01:26:34 -0400] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.200.30/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36" "-"

关闭日志

access_log off;

Nginx目录浏览功能

提供访问快速访问。

[root@ansible extra]# vi test1.conf server {listen 83;server_name hekang520.com;charset utf-8;access_log logs/test01.log;location / {         root /www/test1/;         #index index.html index.htm;         autoindex on; }}[root@ansible test1]# pwd && ll/www/test1total 4-rw-r--r--. 1 root root  0 Jul 31 01:36 dd.sh-rw-r--r--. 1 root root 18 Jul 31 01:37 hekang.txt

目录下将index.html删除或者修改文件后缀名

image-20210731134645237

Nginx状态信息功能

nginx提供了status模块,用于检测nginx的请求连接信息

这编译安装的时候,添加--with-http_stub_status_module参数,才能使用

nginx -V #检查nginx是否支持conf配置文件,用于检查状态页的功能
支持功能 --with-http_stub_status_module
1.创建一个status.conf,放入extra目录下include extra/*.conf创建status.conf文件[root@ansible extra]# cat status.conf server {listen 88;location / {stub_status on;  # 开启状态页面功能access_log off;   # 关闭访问日志	       }}
Active connections: 4 server accepts handled requests 16 16 23 Reading: 0 Writing: 1 Waiting: 3 server 启动后创建的请求数accepts handled nginx启动后创建的握手数requests 表示nginx一共处理了多少次的请求Reading nginx读取到客户端的headers数量Writing nginx响应给客户端的headers数量Waiting nginx处理完毕请求之后,等待下一次的请求驻留的连接数waiting值=active-(reading+write)

使用ab命令, 对nginx进行压力测试

1.安装ab命令# yum install httpd-tools -y-n 请求数量-c 请求并发数-k 表示启动keepalive保持连接
# ab -kc 10 -n 100 http://192.168.200.30:83/

Nginx错误日志

错误日志的语法

error_log file level;

日志级别有如下几种

debug

info

notice

warn

error

crit

alert

emerg 级别越来越严重级别越低,记录的越详细

写在

http{

}中 或者单独的主机中

Nginx location匹配

Nginx的locaiton作用是根据用户请求的URI不同,来执行不同的应用。针对用户请求的网站URL进行匹配,匹配成功后进行对应的操作。

nginx.conf server{} location指令如下location / {root html;index index.html index.htm;}location = /50x.html {root html;}
location [ = | ~|~*|^~ ] url {#指定对应的动作}#正则表达式解释匹配符匹配规则优先级= 精确匹配1^~以某个字符串开头,不做正则2~*正则匹配3/blog/ 匹配常规字符串,有正则就优先正则4/ 通用匹配,不符合其他location的默认匹配5
posted @ 2021-07-31 16:41  给我一碗炒粉  阅读(66)  评论(0)    收藏  举报