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对比图


部署Nginx

yum安装

到官网拷贝一下

nginx: Linux packages

  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

这里的源改成了官网的~

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


编译安装

  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配置文件

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

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

配置文件内容

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

成功啦~


中国象棋

  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

成功了


补充:长连接短链接区别

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
# 修改文件的格式内容如下图

Nginx部署及Web基础的更多相关文章

  1. SpringBoot电商项目实战 — 前后端分离后的优雅部署及Nginx部署实现

    在如今的SpringBoot微服务项目中,前后端分离已成为业界标准使用方式,通过使用nginx等代理方式有效的进行解耦,并且前后端分离会为以后的大型分布式架构.弹性计算架构.微服务架构.多端化服务(多 ...

  2. Nginx web基础入门

    目录 Nginx web基础入门 如何升级nginx或者添加功能 使用systemd管理nginx nginx相关配置文件 nginx的配置文件详解 日志格式 game日志记录实战 日志切割 手写虚拟 ...

  3. Linux架构之Nginx Web基础1

    第41章 Nginx Web基础入门 41.1 Nginx部署 41.1.1 Nginx的安装方式   源码编译 官方仓库 epel仓库 优点 规范 安装简单 安装简单   便于管理 配置易读   缺 ...

  4. Nginx Web 基础入门

    目录 Nginx Web 基础入门 Nginx快速安装 两种方式部署Nginx 如何升级nginx或者添加功能 使用systemd管理nginx nginx相关配置文件 nginx的配置文件详解 虚拟 ...

  5. nginx上部署python web

    nginx上部署python web http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

  6. nginx 部署web页面问题

    nginx 部署web页面的时候,路径都是对的,但是css文件就是不起作用,控制台提示如下,原来是格式的问题,截图如下: css 被转成了application/octet-stream,这个是ngi ...

  7. golang学习笔记9 beego nginx 部署 nginx 反向代理 golang web

    golang学习笔记9 beego nginx 部署 nginx 反向代理 golang web Nginx 部署 - beego: 简约 & 强大并存的 Go 应用框架https://bee ...

  8. 如何使用Nginx和uWSGI或Gunicorn在Ubuntu上部署Flask Web应用

    你好!欢迎阅读我的博文,你可以跳转到我的个人博客网站,会有更好的排版效果和功能. 此外,本篇博文为本人Pushy原创,如需转载请注明出处:https://pushy.site/posts/151981 ...

  9. nginx的web基础

    基于nginx的web部署 [root@nginx ~]# cd /data/web/ client_body_temp/ conf/ fastcgi_temp/ html/ logs/ proxy_ ...

随机推荐

  1. spring定时任务执行两次

    最近用Spring的quartz定时器的时候,发现到时间后,任务总是重复执行两次,在tomcat或jboss下都如此. 打印出他们的hashcode,发现是不一样的,也就是说,在web容器启动的时候, ...

  2. Android 高级UI组件(二)

    1.ExpandableListView 显示垂直滚动两级列表的条目,只允许两个层次 整体思路: 要给ExpandableListView设置适配器,那么必须先设置数据源. 数据源,就是此处的适配器类 ...

  3. zabbix之微信报警

    #:先在企业微信注册一个企业微信号 #:注册好之后,进入微信 #:测试一下 #:获取access_token #:开始获取 #:获取 #:在server端安装pip root@ubuntu:~# ap ...

  4. SpringIOC原理浅析

    1. IoC理论的背景我们都知道,在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实现系统的业务逻辑. 图1:软件系统中耦合的对象 如果我们打开机械 ...

  5. 了解LINQ

    本文主要的是泛谈LINQ是啥?以及常见的用法大纲如下: LINQ的那些根基 LINQ的一些基本用法 LINQ的根基 IEnumerable和IEnumerator 为啥能够被foreach? 实际上, ...

  6. 配置文件管理维护到gitlab上

    一.简介 在日常维护服务器中,会修改配置文件或者相应脚本,在修改前要对文件进行按照日期备份,这样会很麻烦,频繁修改的时候也会懒得去备份多个. 维护脚本分为服务端和客户端,服务端监听端口,接收客户端的采 ...

  7. Cyber Apocalypse 2021 pwn write up

    Controller 考点是整数溢出和scanf函数的引发的栈溢出漏洞,泄露libc地址将返回地址覆盖成one_gadgets拿到shell. 1 from pwn import * 2 3 p = ...

  8. AtCoder Beginner Contest 169 题解

    AtCoder Beginner Contest 169 题解 这场比赛比较简单,证明我没有咕咕咕的时候到了! A - Multiplication 1 没什么好说的,直接读入两个数输出乘积就好了. ...

  9. CF638A Home Numbers 题解

    Content Vasya 的家在一条大街上,大街上一共有 \(n\) 座房子,其中,奇数编号的房子在大街的一侧,从左往右依次编号为 \(1,3,5,7,...,n-1\),偶数编号的房子在大街的另一 ...

  10. Python第二周 str的方法

    str.start #!/usr/bin/env python # Author:Zhangmingda while True: cmd = input('输入字符:')#.strip() print ...