Linux基础31 nginx介绍, 官方安装, 源码安装与升级, nginx配置文件, 搭建小游戏

发布时间 2023-07-31 17:57:42作者: 战斗小人

Nginx Web基础

Nginx概述

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

Nginx特点

1.高性能、高并发

在并发特别高的时候,nginx的响应速度比其他的web服务快很多。

2.轻量且高扩展性

1.轻量: 功能模块少,仅保留http模块和核心模块
2.高扩展性: 模块需要哪个可以装哪个,还可以集成第三方的模块

3.高可靠性

其他的web服务运行一段时间需要重启,nginx不需要
nginx的单机时间 9999,、99999级别

4.支持热部署

可以在不停机服务的情况下进行升级
nginx的master管理进程和worker的工作进程是分离的

5.互联网公司都在用nginx

nginx技术成熟,具备企业里所有需要的功能,负载,缓存,安全,web服务
统一技术栈,降低维护成本,降低技术难度

6.Nginx使用Epoll网络模型

Select:当用户发起一次请求,select模型就会进行一次遍历扫描,从而导致性能低下;(apache采用)

Epoll:当用户发起请求,epool模型会直接进行处理,效率高效,并未连接限制。(nginx采用)

 

其他的web服务

1.apache(httpd):早期使用,性能不高,上手很难;
2.nginx
    Tengine:淘宝基于nginx进行二次开发的产物
    openrensty:基于nginx与lua的高性能web平台
3.lighttpd:消耗的内存和cpu较低,性能好,还有很多模块
4.IIS:windows的web服务
5.GWS:google web server
6.BWS:baidu web server

 

nginx应用场景

 

Nginx部署

1.epol源安装

yum install -y nginx

 

2.官方源安装

1)配置官方源

[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo        # 名字随意,只要以.repo结尾即可
----------------------------------
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
-----------------------------------
# 该文件在 http://nginx.org/下,download->下面stable and mainline-> RHEL/CentOS,采用稳定版的内容[nginx-stable](mainline为最新版,不用) 注意:centos后面的版本要改7

2)安装依赖包

yum install -y gcc gcc-c++ autocnf pcre pcre-devel make automake httpd-tools openssl-devel

3) yum安装nginx

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

4) 配置修改启动用户

[root@web01 ~]# rpm -qc nginx    # 查找配置文件
[root@web01 ~]# vim /etc/nginx/nginx.conf    # 修改配置文件
user  www;
[root@web01 ~]# nginx -t    # 检查配置是否正常

5) 创建用户

groupadd www -g 666
useradd www -u 666 -g 666

6)启动服务

# 方法一:
[root@web01 ~]# nginx
# 方法二:
[root@web01 ~]# systemctl start nginx

注意:使用什么方式启动,就用什么方式关闭,不要混用

7)检查启动

# 方式一:
[root@web01 ~]# systemctl status nginx

# 方式二:
[root@web01 ~]# ps -ef|grep [n]ginx

# 方式三:
[root@web01 ~]# netstat -lntp

# 方式四:
访问页面    http://10.0.0.7/

# 方式五:
[root@web01 ~]# curl 10.0.0.7

# 方式六:
[root@web01 ~]# nginx -v
nginx version: nginx/1.24.0

 

8)nginx常用命令

1.nginx启动
# 方法一:
[root@web01 ~]# nginx
# 方法二:
[root@web01 ~]# systemctl start nginx

#注意:使用什么方式启动,就用什么方式关闭,不要混用
2.nginx停止
# 方法一:
[root@web01 ~]# nginx -s stop
# 方法二:
[root@web01 ~]# systemctl stop nginx

3.nginx重启    # 没有方式一nginx启动对应的重启命令(一般重载配置文件,不重启)
[root@web01 ~]# systemctl restart nginx

4.nginx重新加载配置文件
[root@web01 ~]# nginx -s reload
[root@web01 ~]# systemctl reload nginx

4.检查nginx配置
[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

 

3.源码包安装

1) 下载安装包

在nginx.org下,点击download,选择稳定版(Stable version)下载  例: nginx-1.24.0

[root@web01 ~]# rz nginx-1.24.0.tar.gz

2) 解压安装包

[root@web01 ~]# tar xf nginx-1.24.0.tar.gz

3)创建用户

groupadd www -g 666
useradd www -u 666 -g 666

4) 生成

[root@web01 ~]# cd nginx-1.24.0
[root@web01 nginx-1.24.0]# ./configure --prefix=/usr/local/nginx-1.24.0 --user=www --group=www --with-http_addition_module --with-http_auth_request_module
# --prefix 把nginx装到哪   后面的模块随便加的,可以不加    with-http_auth_request_module用户认证模块    
# 编译完就不能增量安装模块

注意: 如果有一台nginx服务,要在另一台服务器上装一台一模一样的nginx

[root@web01 ~]# nginx -V    # 参看版本,参数(装了哪些模块,插件)
nginx version: nginx/1.24.0
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/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

# 以相同的参数进行安装
./configure --prefix=...

5) 编译安装

[root@web01 nginx-1.24.0]# make && make install
# 做软链接,方便地址写入和nginx版本管理,要哪个版本,软链接连接哪个即可
[root@web01 nginx-1.24.0]# ln -s /usr/local/nginx-1.24.0 /usr/local/nginx

6) 监测配置

[root@web02 nginx-1.24.0]# cd /usr/local/
[root@web02 local]# ./nginx/sbin/nginx -t    # 检查配置是否正常
# 如果测试失败    nginx: [emerg] getpwnam("222") failed
[root@web02 local]# vim /usr/local/nginx-1.24.0/conf/nginx.conf
user  www;
# 检查配置即可通过

7)启动

# 启动的时候没有办法使用systemctl管理,编辑使system可以管理nginx
[root@web02 local]# vim /usr/lib/systemd/system/nginx.service
------------------------
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop

[Install]
WantedBy=multi-user.target

 

nginx升级

# 升级版本    (重新安装一个版本后替换软链接即可)
[root@web02 ~]# wget http://nginx.org/download/nginx-1.17.8.tar.gz
[root@web02 ~]# tar xf nginx-1.17.8/
[root@web02 nginx-1.17.8.tar.gz]# cd nginx-1.17.8.tar.gz
[root@web02 nginx-1.17.8]# ./configure --prefix=/usr/local/nginx-1.17.8 --user=www --group=www --with-http_addition_module --with-http_auth_request_module
[root@web02 local]# rm -rf /usr/local/nginx && ln -s /usr/local/nginx-1.17.8 /usr/local/nginx
[root@web02 local]# systemctl restart nginx    # 重启就会升级 (因为systemctl配置为软链接不用改)
[root@web02 local]# /usr/local/nginx/sbin/nginx -v    # 查看版本

--------------------------------------------------

# 添加模块    (相当于全量重装)
[root@web02 ~]# cd nginx-1.24.0    # 重新进入安装包
[root@web02 nginx-1.24.0]# rm -rf Makefile    # 不会对之前nginx造成影响
# 重新编译安装 (prefix安装路径要替换,不然就覆盖了。后面追加新的模块)
[root@web02 nginx-1.24.0]# ./configure --prefix=/usr/local/nginx-new-1.24.0 --user=www --group=www --with-http_addition_module --with-http_auth_request_module --with-http_mp4_module
[root@web02 nginx-1.24.0]# make && make install
[root@web02 ~]#cd /usr/local
[root@web02 local]# rm -rf /usr/local/nginx && ln -s /usr/local/nginx-new-1.24.0 /usr/local/nginx
# 重启
[root@web02 local]# systemctl restart nginx
# 确认
[root@web02 local]# /usr/local/nginx/sbin/nginx -V

 

Nginx相关文件

# 查看nginx相关配置文件
[root@web01 ~]# rpm -ql nginx

1.Nginx主配置文件

路径类型作用
/etc/nginx/nginx.conf 配置文件 nginx主配置文件
/etc/nginx/conf.d/default.conf 配置文件 默认网站配置文件

注意: 源码包安装在nginx/conf下,例:/usr/local/nginx/conf/nginx.conf

2.Nginx代理相关参数文件

路径类型作用
/etc/nginx/fastcgi_params 配置文件 Fastcgi代理配置文件(php)
/etc/nginx/scgi_params 配置文件 scgi代理配置文件
/etc/nginx/uwsgi_params 配置文件 uwsgi代理配置文件(python)

3.Nginx编码相关配置文件

路径类型作用
/etc/nginx/win-utf 配置文件 Nginx编码转换映射文件(不用管)
/etc/nginx/koi-utf 配置文件 Nginx编码转换映射文件(不用管)
/etc/nginx/koi-win 配置文件 Nginx编码转换映射文件(不用管)
/etc/nginx/mime.types 配置文件 Content-Type与扩展名(设置可以识别的文件类型)

4.Nginx管理相关命令

路径类型作用
/usr/sbing/nginx 命令 Nginx命令行管理终端工具
/usr/sbin/nginx-debug 命令 Nginx命令行与终端调试工具(基本用不上)

注意: 源码包安装nginx命令在 /usr/local/nginx/sbin/nginx

5.Nginx日志相关目录与文件

路径类型作用
/var/log/nginx 目录 Nginx默认存放日志目录
/etc/logrotate.d/nginx 配置文件 Nginx默认的日志切割

注意,nginx日志为adm用户权限,为自带用户。nginx有自动分割日志,自动打包功能,用的是该用户

 

Nginx配置文件

Nginx主配置文件/etc/nginx/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形式组织的。一般,每个区块以一对大括号{}来表示开始与结束,{} 内每行以 ; 结尾。

Nginx主配置文件整体分为三块进行学习,分别是CoreModule(核心模块),EventModule(事件驱动模块),HttpCoreModule(http内核模块)
#------------------------核心模块-----------------------
# 启动用户
user  nginx;
# 工作进程数量
worker_processes  1;
# 错误日志                日志级别 debug/info/notice/warn/error/crit/alter/emerg
error_log  /var/log/nginx/error.log warn;
# pid文件
pid        /var/run/nginx.pid;

#------------------------事件驱动模块---------------------
# 每个启动的worker可以支持多少个连接,这里是1024
events {
    worker_connections  1024;
}

#------------------------http内核模块---------------------
http {
    # nginx包含的文件类型
    include       /etc/nginx/mime.types;
    # nginx默认不识别的文件类型直接下载(不识别就不会跳转,改为直接下载)
    default_type  application/octet-stream;
    # 日志格式  日志格式叫main  
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
                      
# body_bytes_sent: 访问的文件内容大小
# http_referer: 上一个页面,从哪个页面跳转过来
# "$http_user_agent": 客户端信息,加引号是为了包住信息,好分辨
# "$http_x_forwarded_for": 用户真实ip
# 日志格式如下:
120.242.21.178 - lhd [20/Feb/2020:15:13:23 +0800] "GET /picture/nginx_02.jpg HTTP/1.1" 200 38036 "https://www.mumusir.com/" "Mozilla/5.0 (Windows NT 10.0; wow64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.4098.3 Safari/537.36" "-"

    #访问日志    存在哪                  日志格式调用
    access_log  /var/log/nginx/access.log  main;
    #优化部分
    sendfile        on;
    #tcp_nopush     on;
    # 保持长链接时间(65秒),如果为0和短连接一样
    keepalive_timeout  65;
    #是否开启压缩
    #gzip  on;
    
    # 包含配置文件
    include /etc/nginx/conf.d/*.conf;
}

# -------------------------------
# *.conf内部文件
    #使用server配置网站,每个server代表一个网站(我们称之为虚拟主机)
    server {
        #监听端口
        listen        80;
        # 提供的域名
        server_name     _;
        # 控制网站访问路径
        location / {
            # 站点目录,指定存放页面的地址
            root /code;        # 访问查询的文件目录
            # 默认访问的页面
            index index.html    # 如果/后面什么都不加,就访问index.html
        }
    }
    ...
    server{
    ...
    }
# -------------------------------

 

搭建小游戏

1.编写配置文件

[root@web01 conf.d]# vim /etc/nginx/conf.d/game.conf
-----------------------------------
server {
    listen 80;
    server_name localhost;

    location / {
        root /code;
        index index.html;
    }
}
-----------------------------------
# 注意,改完 nginx -t校验会报冲突,域名和端口和default.conf重复了。访问会随机到一个
[root@web01 conf.d]# gzip default.conf    # 把default.conf临时压缩下
[root@web01 conf.d]# ls
default.conf.gz  game.conf

2.创建站点目录

[root@web01 conf.d]# mkdir /code

3.上传代码包

[root@web01 conf.d]# cd /code
[root@web01 code]# rz mario.zip
[root@web01 code]# unzip mario.zip
[root@web01 code]# cp -r html/* ./

4.重载nginx

[root@web01 code]# nginx -s reload    # 重新加载配置(注意重启要和启动方式配套)
[root@web01 code]# systemctl restart nginx    (建议使用system管理)

注:mario.zip文件没有,可以参考贪吃蛇的html文件

https://blog.csdn.net/m0_56417836/article/details/125596508