一、Nginx概述
1、Nginx是一个开源且高性能、可靠的http web服务、代理服务
开源:直接获取源代码
高性能:支持海量开发
可靠:服务稳定
2、Nginx使用的是Epoll网络模型
select:当用户发起一次请求,select模型就会进行一次遍历扫描,从而导致性能低下
Epoll:当用户发起一次请求,epoll模型会直接进行处理,效率高,并无连接限制
官网地址:https://nginx.org/
软件下载地址:https://nginx.org/download/
二、部署Nginx
1、yum安装
进入官网地址--->documentation
[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
[root@web01 ~]# yum install nginx -y
[root@web01 ~]# systemctl stop httpd
[root@web01 ~]# systemctl start nginx
[root@web01 ~]# nginx -V nginx version: nginx/1.20.2 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/
2、二进制安装
3、编译安装
进入官网地址--->download--->复制链接地址
[root@web02 ~]# wget https://nginx.org/download/nginx-1.20.2.tar.gz
[root@web02 ~]# tar -xf nginx-1.20.2.tar.gz
[root@web02 ~]# ./configure
[root@web02 ~]# make
[root@web02 ~]# make install
[root@web02 ~]# /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.20.2 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments:
三、平滑增加Nginx模块
增加模块必须重新编译
[root@web02 ~]# rm -rf nginx-1.20.2
[root@web02 ~]# tar -xf nginx-1.20.2.tar.gz
[root@web02 ~]# cd nginx-1.20.2
# 安装依赖包
[root@web02 nginx-1.20.2]# yum install openssl openssl-devel -y
[root@web02 nginx-1.20.2]# ./configure --help
[root@web02 nginx-1.20.2]#./configure --with-http_ssl_module
[root@web02 nginx-1.20.2]#make
[root@web02 nginx-1.20.2]#make install
[root@web02 ~]# /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.20.2 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --with-http_ssl_module
·优化
[root@lb01 nginx]# mkdir /etc/nginx [root@lb01 nginx]# mv /usr/local/nginx/conf/* /etc/nginx/ [root@lb01 nginx]# mkdir /etc/nginx/conf.d [root@lb01 nginx]# vim /etc/nginx/nginx.conf [root@lb01 nginx]# groupadd www -g 666 [root@lb01 nginx]# useradd www -u 666 -g 666 -M -r -s /sbin/nologin [root@lb01 nginx]# vim /usr/lib/systemd/system/nginx.service [Unit] Description=nginx - high performance web server Documentation=http://nginx.org/en/docs/ After=network-online.target remote-fs.target nss-lookup.target Wants=network-online.target [Service] Type=forking PIDFile=/var/run/nginx.pid ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)" ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)" [Install] WantedBy=multi-user.target [root@lb01 sbin]# ln -s /etc/nginx/nginx.conf /usr/local/nginx/conf/nginx.conf [root@lb01 sbin]# mv /usr/local/nginx/sbin/nginx /usr/sbin/ [root@lb01 sbin]# mkdir /var/log/nginx [root@lb01 sbin]# systemctl daemon-reload # 重载 [root@lb01 sbin]# systemctl start nginx ################################# [root@lb01 nginx-1.20.2]# cd /usr/local/nginx/sbin [root@lb01 sbin]# ll total 6944 -rwxr-xr-x 1 root root 7110376 Jan 9 16:00 nginx [root@lb01 sbin]# which nginx /usr/sbin/nginx [root@lb01 sbin]# rm -rf /usr/sbin/nginx [root@lb01 sbin]# mv nginx /usr/sbin/
四、Nginx的命令
查看nginx的命令:nginx -h
-v:打印版本号
-V:打印版本号和配置项
-t:检查配置文件
[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
-T:测试配置文件并启动
-q:打印错误日志
-s:操作进程
stop:停止
quit:退出
reopen:重启
reload:重载
-p:指定nginx的工作目录
-e:指定错误日志路径
-c:指定配置文件的路径
-g:设置一个全局的nginx配置项
[root@web01 ~]# nginx -g 'daemon off;'
五、Nginx配置文件
全局配置和模块配置
[root@web01 ~]# cat /etc/nginx/nginx.conf user nginx; # 指定nginx的启动用户 worker_processes auto; # 定义nginx的worker进程数;auto:CPU数量 error_log /var/log/nginx/error.log notice; # 错误日志路径 pid /var/run/nginx.pid; # pid的存放文件路径 events { # 事件模块配置 worker_connections 1024; # 每一个worker进程最多同时接入多少个请求; use:指定nginx的网络模型 } http { # web服务的模块 include /etc/nginx/mime.types; # 加载外部的配置项 default_type application/octet-stream; # 如果找不到文件的类型,则按照指定默认类型处理 # log_format:定义日志格式,一般使用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 ; sendfile on; # 高效读取文件 #tcp_nopush on; keepalive_timeout 65; # 长连接保持的时间 #gzip on; include /etc/nginx/conf.d/*.conf; }
[root@web01 ~]# cat /etc/nginx/conf.d/default.conf server { # 网址模块 listen 80; # 监听的端口 server_name localhost; # 定义域名 #access_log /var/log/nginx/host.access.log main; location / { # 访问路径 root /usr/share/nginx/html; # 指定网址路径 index index.html index.htm; # 指定网址的索引文件 }
查看日志,json格式
[root@web01 ~]# tail -f /var/log/nginx/access.log {"@timestamp":"2021-12-31T17:24:21+08:00","host":"192.168.15.7","service":"nginxTest","trace":"-","log":"log","clientip":"192.168.15.1","remote_user":"-","request":"GET / HTTP/1.1","http_user_agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36","size":0,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"192.168.15.7","url":"/index.html","domain":"192.168.15.7","xff":"-","referer":"-","status":"304"}
六、Nginx搭建小游戏
6.1、超级玛丽
1、创建目录
[root@web01 ~]# cd /opt/
[root@web01 opt]# mkdir Super_Marie
2、上传代码
[root@web01 Super_Marie]# ll total 176 drwxr-xr-x 2 root root 329 Dec 31 17:38 images -rw-r--r-- 1 root root 1703 Dec 31 17:38 index.html -rw-r--r-- 1 root root 72326 Dec 31 17:38 jquery.js -rw-r--r-- 1 root root 78982 Dec 31 17:38 QAuIByrkL.js -rw-r--r-- 1 root root 4777 Dec 31 17:38 VNkyVaVxUV.css -rw-r--r-- 1 root root 9539 Dec 31 17:38 wNGu2CtEMx.js
3、编辑配置文件
[root@web01 Super_Marie]# pwd /opt/Super_Marie [root@web01 Super_Marie]# cd /etc/nginx/conf.d/ [root@web01 conf.d]# ll total 4 -rw-r--r-- 1 root root 1072 Nov 16 23:02 default.conf [root@web01 conf.d]# vim sup_game.conf [root@web01 conf.d]# cat sup_game.conf server { listen 80; server_name sup_game.test.com; location / { root /opt/Super_Marie; index index.html; } }
4、测试配置文件是否正常
[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
5、重启Nginx
[root@web01 conf.d]# systemctl restart nginx
6、域名解析
C:\Windows\System32\drivers\etc\hosts
172.16.1.7 sup_game.test.com
6.2、象棋
1、创建目录
[root@web01 opt]# mkdir Chinese_chess
2、上传代码
[root@web01 Chinese_chess]# ll total 4 drwxr-xr-x 2 root root 22 Dec 31 18:02 css drwxr-xr-x 4 root root 36 Dec 31 18:02 img -rw-r--r-- 1 root root 3266 Dec 31 18:02 index.html drwxr-xr-x 2 root root 120 Dec 31 18:02 js
3、编辑配置文件
[root@web01 Chinese_chess]# vim /etc/nginx/conf.d/chess_game.conf [root@web01 Chinese_chess]# cat /etc/nginx/conf.d/chess_game.conf server { listen 80; server_name chess_game.test.com; location / { root /opt/Chinese_chess; index index.html; } }
4、测试配置文件是否正常
[root@web01 Chinese_chess]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
5、重启nginx
[root@web01 Chinese_chess]# systemctl restart nginx
6、域名解析
C:\Windows\System32\drivers\etc\hosts
172.16.1.7 sup_game.test.com chess_game.test.com