Nginx部署及Web基础

Nginx部署及Web基础

👉Nginx官网

👉Nginx下载

Nginx简介

Nginx是一个开源且高性能、可靠的http web服务、代理服务

  • 开源:直接获取源代码
  • 高性能:支持海量开发
  • 可靠:服务稳定

Nginx特点

  1. 高性能,高并发:nginx支持很高的并发,nginx在处理大量并发的情况下比其他web服务要快
  2. 轻量且高扩展:功能模块少,只保留核心模块,其他代码模块化,需要什么模块再安装模块,不需要全部安装,并且还支持第三方模块
  3. 高可靠性:几乎不会出现问题,其他的web服务需要每隔一段时间进行重启,nginx不需要,nginx的宕机时间,是99999级别
  4. 支持多路复用
  5. 支持热部署: Nginx支持热部署,它的自动特别容易,几乎可以7天*24小时不间断的运行,即使,运行数个月也不需要重新启动,还能够在不间断服务的情况下,对软件版本进行升级。
  6. Nginx使用的网络模型是Epool

网络模型:select、poll、epoll

Web服务

Web服务就是B/S架构,Web服务器常常以B/S(Browser/Server)方式提供服务,浏览器和服务器的交互方式。

Web服务器软件

Web服务器常用的有Apache和Nginx,两者都实现了HTTP 1.1协议,两者的优缺点如下文(写的相当详细了):

👉[Nginx vs Apache](Nginx 和 Apache 区别最全详解? - 云+社区 - 腾讯云 (tencent.com))

Nginx和Apache对比图

image

image

image

image


部署Nginx

yum安装

到官网拷贝一下

👉nginx: Linux packages

image

  1. 创建官网指定文件目录
 [root@web01 ~]# vim /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
module_hotfixes=true

[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
module_hotfixes=true
  1. 安装Nginx
[root@web01 ~]# yum install nginx -y

image

这里的源改成了官网的~

  1. 启动nginx
# 如果开启了httpd请关闭服务,再打开nginx
[root@web01 ~]# systemctl stop httpd
[root@web01 ~]# systemctl start nginx

image


编译安装

  1. 获取文件
[root@web01 ~]#  wget https://nginx.org/download/nginx-1.20.2.tar.gz
  1. 解压
[root@web01 ~]# tar -xf nginx-1.20.2.tar.gz
  1. 预处理
[root@web01 nginx-1.20.2]# ./configure

补:configure是一个脚本,一般由Autoconf工具生成,它会检验当前的系统环境,看是否满足安装软件所必需的条件:比如当前系统是否支持待安装软件,是否已经安装软件依赖等。configure脚本最后会生成一个Makefile文件。

  1. 编译安装
[root@web01 nginx-1.20.2]# make
[root@web01 nginx-1.20.2]# make install

补:

make是一个命令,它使用第1步得到的Makefile文件,如果只有"make"命令,而没有指明"目标",一般情况下是编译源码。

make install表示运行"目标为install的make命令",即将编译后的结果复制到相应目录中。

推荐使用yum安装的方式!

平滑增加Nginx模块

平滑增加Nginx模块必须是编译安装的模式,因为yum安装会自动安装模块,无法指定,切换到web02做示例:

  1. 在web02虚拟机中编译安装nginx
[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 nginx-1.20.2]# ./configure
[root@web02 nginx-1.20.2]# make
[root@web02 nginx-1.20.2]# make install

# 补充:如果使用编译安装nginx查看版本,不能直接使用nginx -v,因为没有环境变量,必须切换到/usr/local/nginx/sbin目录下,这个目录下的nginx是可执行的
[root@web02 sbin]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.20.2
  1. 查看模块
[root@web02 sbin]# /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) 
configure arguments:

# 没有模块,重新编译增加模块
  1. 重新编译增加模块
# 如果解压过,删除重新解压编译安装
[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]# ./configure --help
# 增加模块--with-http_ssl_module
[root@web02 nginx-1.20.2]#./configure  --with-http_ssl_module
# 如果报错:./configure: error: SSL modules require the OpenSSL library.就安装openssl,没有就继续安装
[root@web02 nginx-1.20.2]# yum install openssl openssl-devel -y 
# 重新增加
[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
  1. 查看是否增加模块
[root@web02 local]# /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
# 增加成功

Nginx的命令

安装成功后输入nginx -h查看所有参数

[root@web01 nfs]# nginx -h
nginx version: nginx/1.20.2
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
             [-e filename] [-c filename] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -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
  -p prefix     : set prefix path (default: /etc/nginx/)
  -e filename   : set error log file (default: /var/log/nginx/error.log)
  -c filename   : set configuration file (default: /etc/nginx/nginx.conf)
  -g directives : set global directives out of configuration file

常用参数

  1. -v:打印版本号
[root@web01 nfs]# nginx -v
nginx version: nginx/1.20.2
  1. -V:打印版本和配置项
[root@web01 nfs]# 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/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'
  1. -t:检查配置文件
[root@web01 nfs]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
  1. -T:测试配置文件并运行
[root@web01 nfs]# nginx -T 
  1. -q:打印错误日志
[root@web01 nfs]# nginx -q
  1. -s : 操作进程
	stop	:停止
	quit	:退出
	reopen	:重启
	reload	:重载
nginx -s stop #强制停止Nginx服务
nginx -s quit #优雅地停止Nginx服务(即处理完所有请求后再停止服务)
nginx -s reopen #重启Nginx
nginx -s reload #重新加载Nginx配置文件,然后以优雅的方式重启Nginx
  1. 指定路径参数
-p : 指定nginx的工作目录
-e : 指定错误日志路径
-c : 指定配置文件的路径
nginx -p prefix #设置前缀路径(默认是:/usr/share/nginx/)
nginx -e logfile # 设置错误日志路径(默认是:/var/log/nginx/error.log)
nginx -c filename #设置配置文件(默认是:/etc/nginx/nginx.conf)
  1. -g : 设置一个全局的Nginx配置项
[root@web01 ~]# nginx -g 'daemon off;'

Nginx配置文件

image

nginx分为全局配置和模块配置

相关文件:/etc/nginx/nginx.conf

image

配置文件内容

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

配置文件内容剖析

全局配置

  1. user:指定Nginx的启动用户

  2. worker_processes:定义Nginx的worker进程数,auto相当于CPU的数量(auto可以修改为整数)

  3. error_log:错误日志路径

  4. pid:pid的存放文件路径

  5. events : 模块配置,

    # 可配置项:
    worker_connections:代表每一个worker进程最多同时接入多少个请求(每一个进程处理的请求个数)
    use : 指定Nginx的网络模型
    
    
  6. http:web服务模块

    include : 加载外部的配置项,降低了文件的复杂度
    default_type : 如果找不到文件的类型,则按照指定默认类型处理
    log_format : 定义日志格式
    日志格式默认为简洁版(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 ;
    
    sendfile : 高效读取文件
    keepalive_timeout : 长连接保持连接的
    # timeout不能太低,不然和短链接一样 
    
    
    1. include : 加载外部的配置项

    相关文件/etc/nginx/conf.d/*.conf

    # 重点
    server : 网址模块,每一个server代表一个网站,可以有多个
    listen : 监听的端口
    server_name : 定义域名
    location {} : 访问路径
    	root : 指定网址路径
    	index : 指定网址的索引文件
    

小游戏案例

搭建超级马里奥和中国象棋

超级马里奥

  1. 上传代码
# 源代码百度自己下载的,想要私我就可以了
# 在/opt/创建存放游戏文件的目录/opt/Super_Mario
  1. 编辑配置文件
[root@web01 ~]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# vim /etc/nginx/conf.d/game.conf 
server{
    listen 80;
    server_name Mario.test.com;
    location / {
        root /opt/Super_Mario;
        index index.html;
    }
}
  1. 测试配置文件是否正常
[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
  1. 重启Nginx
[root@web01 conf.d]# systemctl restart nginx 
  1. 域名解析
在文件 C:\Windows\System32\drivers\etc\hosts中添加ip 和域名
192.168.15.7 mario.test.com

image

成功啦~


中国象棋

  1. 上传游戏文件,并创建存放游戏的目录
[root@web01 opt]# mkdir chess
  1. 编辑配置文件
[root@web01 conf.d]# cd /etc/nginx/conf.d
[root@web01 conf.d]# vim chess.conf
server{
    listen 80;
    server_name chess.test.com;
    location / {
        root /opt/chess;
        index index.html;
    }
}

  1. 测试配置文件是否正常
[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 successfu
  1. 重启Nginx
[root@web01 conf.d]# systemctl restart nginx
  1. 域名解析
在文件 C:\Windows\System32\drivers\etc\hosts中添加ip 和域名
192.168.15.7 mario.test.com chess.test.com

image

成功了


补充:长连接短链接区别

HTTP 1.0 短链接
HTTP 1.1 长连接

长连接意味着进行一次数据传输后,不关闭连接,长期保持连通状态。如果两个应用程序之间有新的数据需要传输,则直接复用这个连接,无需再建立一个新的连接。

短连接意味着每一次的数据传输都需要建立一个新的连接,用完再马上关闭它。下次再用的时候重新建立一个新的连接,如此反复。

补充:解决NFS持久化

方式一

  1. 通过开机自启动脚本挂载

使用方式一,注意开机需要关闭防火墙和selinux

[root@web01 ~]# vim /etc/rc.local
/usr/bin/mount -t nfs 172.16.1.31:/web/upload /var/www/html/upload
[root@web01 ~]# chmod +x /etc/rc.d/rc.local 

方式二

  1. 通过/etc/fstab配置文件

使用方式二,需要在服务端开启nfs-server服务systemctl start nfs-server

[root@web02 ~]# vim /etc/fstab
172.16.1.31:/web/upload  /var/www/html/upload   nfs       defaults          0                0
# 测试是否挂载成功,不报错就是挂载成功了!
[root@web02 ~]# mount -a
# 修改文件的格式内容如下图

image

posted @ 2022-01-04 20:45  HammerZe  阅读(2942)  评论(11编辑  收藏  举报