Nginx基础

第一章 Nginx介绍

Nginx是什么

Nginx 是一个开源且高性能、可靠的 Http Web 服务、代理服务
已经被F5收购
开源: 直接获取源代码
高性能: 支持海量并发
可靠: 服务稳定

我们为什么选择 Nginx 服务

Nginx 非常轻量
功能模块少 (源代码仅保留 http 与核心模块代码,其余不够核心代码会作为插件来安装)
代码模块化 (易读,便于二次开发,对于开发人员非常友好)
互联网公司都选择 Nginx
1.Nginx 技术成熟,具备的功能是企业最常使用而且最需要的
2.适合当前主流架构趋势, 微服务、云架构、中间层
3.统一技术栈, 降低维护成本, 降低技术更新成本。

Nginx重要特性

Nginx 采用 Epool 网络模型, Apache 采用 Select 模型
Select: 当用户发起一次请求, select 模型就会进行一次遍历扫描,从而导致性能低下。
Epool: 当用户发起请求, epool 模型会直接进行处理,效率高效,并无连接限制

Nginx应用场景

1.提供静态页面解析
2.反向代理负载均衡
3.动静分离
4.访问控制和流量限制
5.搭配其他软件实现经典架构,比如LNMP,LNMT

第二章 Nginx安装部署

Nginx安装方式

1.源码编译    复杂,自由度高
2.epel仓库   版本不是官方的,可能不是最新的
3.官方仓库    推荐使用,可以安装最新版

1.编译安装方法

创建www用户

[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -s /sbin/nologin -M -u 666 -g 666
[root@web01 ~]# id www
uid=666(www) gid=666(www) 组=666(www)

安装依赖包

[root@web01 ~]# yum install openssl-devel pcre-devel gcc -y

下载解压软件包

[root@web01 ~]# mkdir /data/soft -p
[root@web01 ~]# cd /data/soft/
[root@web01 /data/soft]# wget http://nginx.org/download/nginx-1.20.2.tar.gz
[root@web01 /data/soft]# tar zxvf nginx-1.20.2.tar.gz 

配置编译参数

[root@web01 ~]# cd /data/soft/nginx-1.20.2/
[root@web01 /data/soft/nginx-1.20.2]# ./configure --help
[root@web01 /data/soft/nginx-1.20.2]# ./configure --user=www --group=www --prefix=/opt/nginx-1.20.2 --with-http_stub_status_module --with-http_ssl_module --with-pcre

编译安装

[root@web01 /data/soft/nginx-1.20.2]# make && make install

创建软链接

[root@web01 /data/soft/nginx-1.20.2]# ln -s /opt/nginx-1.20.2/ /opt/nginx
[root@web01 /data/soft/nginx-1.20.2]# ls -lh /opt/
总用量 4.0K
lrwxrwxrwx  1 root root   18 7月  29 20:27 nginx -> /opt/nginx-1.16.0/
drwxr-xr-x 11 1001 1001 4.0K 7月  29 20:26 nginx-1.20.2

检查语法

[root@web01 /opt/nginx]# /opt/nginx/sbin/nginx -t
nginx: the configuration file /opt/nginx-1.20.2/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx-1.20.2//conf/nginx.conf test is successful

启动nginx

[root@web01 /opt/nginx]# /opt/nginx/sbin/nginx

检查测试

[root@web01 /opt/nginx]# netstat -lntup|grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      12828/nginx: master 
[root@web01 /opt/nginx]# curl 172.16.1.7

2.编译安装新添加模块

1.先把以前的nginx文件备份下

cp /opt/nginx-1.20.2/sbin/nginx /root/nginx-v1

2.进入源码包安装目录

/data/soft/nginx-1.20.2

3.找出原来编译的参数

[root@web01 /data/soft/nginx-1.20.2]# nginx -V
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/opt/nginx-1.20.2/ --with-http_stub_status_module --with-http_ssl_module --with-pcre

4.重新编译

./configure --user=www --group=www --prefix=/opt/nginx-1.20.2 --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-mail

5.重新安装

make && make install 

6.检查

nginx -V 

比喻

下载     		   					买菜
./configure		 				  切菜.洗菜,做什么菜
make 			 				  炒菜,抄完之后,菜还在锅里
make install     				  把菜端出来
/opt/nginx/sbin/nginx -t   		  尝一尝味道
/opt/nginx/sbin/nginx		      吃菜
/opt/nginx/sbin/nginx -s reload   重新加载/加菜 
/opt/nginx/sbin/nginx -s stop     收拾餐盘

3.YUM安装方法

安装依赖包

[root@web01 ~]# yum install openssl-devel pcre-devel -y

配置官方yum源

[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
[root@web01 ~]# cat /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key

安装nginx服务

[root@web01 ~]# yum install nginx -y

启动服务并配置开机自启动

[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx

测试访问

[root@web01 ~]# curl 172.16.1.7

Nginx启动方式说明

编译安装启动管理方式

nginx -t
nginx
nginx -s reload
nginx -s stop

yum安装启动管理方法

nginx -t
systemctl start nginx
systemctl reload nginx
systemctl restart nginx
systemctl stop  nginx

第三章 Nginx重要配置文件说明

查看配置文件

[root@web01 ~]# rpm -ql nginx 
...................................................                     
/etc/logrotate.d/nginx                     #nginx日志切割的配置文件
/etc/nginx/nginx.conf                      #nginx主配置文件 
/etc/nginx/conf.d                          #子配置文件
/etc/nginx/conf.d/default.conf             #默认展示的页面一样 
/etc/nginx/mime.types                      #媒体类型 (http协议中的文件类型)
/etc/sysconfig/nginx                       #systemctl 管理 nginx的使用的文件
/usr/lib/systemd/system/nginx.service      #systemctl 管理nginx(开 关 重启 reload)配置文件       
/usr/sbin/nginx                            #nginx命令
/usr/share/nginx/html                      #站点目录 网站的根目录 
/var/log/nginx                             #nginx日志 access.log 访问日志
...................................................                     

查看已经编译的模块

[root@web01 ~]# nginx -V

配置文件注解

Nginx 主配置文件/etc/nginx/nginx.conf 是一个纯文本类型的文件,整个配置文件是以区块的形式组织的。
一般,每个区块以一对大括号{}来表示开始与结束。
Nginx 主配置文件整体分为三块进行学习,分别是 
CoreModule(核心模块)
EventModule(事件驱动模块)
HttpCoreModule(http 内核模块)

第一部分:配置文件主区域配置

user  nginx;                    #定义运行nginx进程的用户
worker_processes  1;            #Nginx运行的work进程数量(建议与CPU数量一致或 auto)

error_log  /var/log/nginx/error.log warn;             #nginx错误日志
pid        /var/run/nginx.pid;                        #nginx运行pid

第二部分:配置文件事件区域

events {
    worker_connections  1024;  #每个 worker 进程支持的最大连接数
}

第三部分:配置http区域

http {
    include       /etc/nginx/mime.types;          #Nginx支持的媒体类型库文件
    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"';
	'''
	将日志改成json格式:
    log_format json '{"@timestamp":"$time_iso8601",'
                  '"host":"$server_addr",'
                  '"service":"nginxTest",'
                  '"trace":"$upstream_http_ctx_transaction_id",'
                  '"log":"log",'
                  '"clientip":"$remote_addr",'
                  '"remote_user":"$remote_user",'
                  '"request":"$request",'
                  '"http_user_agent":"$http_user_agent",'
                  '"size":$body_bytes_sent,'
                  '"responsetime":$request_time,'
                  '"upstreamtime":"$upstream_response_time",'
                  '"upstreamhost":"$upstream_addr",'
                  '"http_host":"$host",'
                  '"url":"$uri",'
                  '"domain":"$host",'
                  '"xff":"$http_x_forwarded_for",'
                  '"referer":"$http_referer",'
                  '"status":"$status"}';
    		access_log /var/log/nginx/access.log json ;
'''
    access_log  /var/log/nginx/access.log  main;    #访问日志保存路径

    sendfile        on;                             #开启高效传输模式
    #tcp_nopush     on;                       
    keepalive_timeout  65;                          #连接超时时间
    #gzip  on;                                      #开启压缩
    include /etc/nginx/conf.d/*.conf;               #包含子配置文件
}    

第四部分:子配置文件内容

[root@web01 ~]# egrep -v "#|^$" /etc/nginx/conf.d/default.conf     
server {
    listen       80;             #指定监听端口
    server_name  localhost;      #指定监听的域名
    location / {              
        root   /usr/share/nginx/html;     #定义站点的目录
        index  index.html index.htm;      #定义首页文件
    }
    error_page   500 502 503 504  /50x.html;    #优雅显示页面信息
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

http server location 扩展了解项

http{}层下允许有多个 Server{}层,一个 Server{}层下又允许有多个 Location
http{} 标签主要用来解决用户的请求与响应。
server{} 标签主要用来响应具体的某一个网站。
location{} 标签主要用于匹配网站具体 URL 路径

补充!!!nginx小游戏

1、上传代码
cd /opt/
操作上传

2、编辑配置文件
[root@web01 conf.d]# vim /etc/nginx/conf.d/game.conf 
server {
    listen 80;
    server_name game.test.com;
    location / {
        root /opt/Super_Marie;
        index index.html;
    }
}

3、测试配置文件是否正常
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

4、重启Nginx
[root@web01 conf.d]# systemctl restart nginx 

5、域名解析
C:\Windows\System32\drivers\etc\hosts
172.16.1.7 game.test.com

将main日志改成json格式

log_format json '{"@timestamp":"$time_iso8601",'
              '"host":"$server_addr",'
              '"service":"nginxTest",'
              '"trace":"$upstream_http_ctx_transaction_id",'
              '"log":"log",'
              '"clientip":"$remote_addr",'
              '"remote_user":"$remote_user",'
              '"request":"$request",'
              '"http_user_agent":"$http_user_agent",'
              '"size":$body_bytes_sent,'
              '"responsetime":$request_time,'
              '"upstreamtime":"$upstream_response_time",'
              '"upstreamhost":"$upstream_addr",'
              '"http_host":"$host",'
              '"url":"$uri",'
              '"domain":"$host",'
              '"xff":"$http_x_forwarded_for",'
              '"referer":"$http_referer",'
              '"status":"$status"}';
		access_log /var/log/nginx/access.log json ;
posted @ 2022-01-04 16:07  hai起奈  阅读(87)  评论(2编辑  收藏  举报