Nginx部署及Web基础

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介: Nginx部署及Web基础Nginx是一个开源且高性能、可靠的http 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对比图#


部署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的网络模型
  1. 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
# 修改文件的格式内容如下图

 

原文地址https://www.cnblogs.com/48xz/p/15764016.html

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
7天前
|
负载均衡 应用服务中间件 Apache
Nginx与Apache的终极对决:揭秘Web服务器界的“速度与激情”!你不可不知的性能霸主如何颠覆传统,震撼互联网的根基!
【8月更文挑战第13天】互联网技术发展中,Web服务器至关重要,Nginx与Apache是最广泛使用的两种。Apache历史悠久,但Nginx以轻量级和高性能脱颖而出。Nginx采用事件驱动架构,高效处理大量并发连接,而Apache使用进程驱动,高并发下资源消耗大。以餐厅为例,Nginx像大堂经理同时处理多个顾客需求,远比Apache为每位顾客分配服务员更高效。性能测试显示Nginx处理能力远超Apache。此外,Nginx配置简洁灵活,尤其在负载均衡方面表现突出。尽管Apache适合动态内容处理,但在快速变化的互联网环境中,Nginx因其独特优势成为许多开发者和企业的首选。
29 7
|
4天前
|
机器学习/深度学习 JSON API
【Python奇迹】FastAPI框架大显神通:一键部署机器学习模型,让数据预测飞跃至Web舞台,震撼开启智能服务新纪元!
【8月更文挑战第16天】在数据驱动的时代,高效部署机器学习模型至关重要。FastAPI凭借其高性能与灵活性,成为搭建模型API的理想选择。本文详述了从环境准备、模型训练到使用FastAPI部署的全过程。首先,确保安装了Python及相关库(fastapi、uvicorn、scikit-learn)。接着,以线性回归为例,构建了一个预测房价的模型。通过定义FastAPI端点,实现了基于房屋大小预测价格的功能,并介绍了如何运行服务器及测试API。最终,用户可通过HTTP请求获取预测结果,极大地提升了模型的实用性和集成性。
14 1
|
5天前
|
负载均衡 前端开发 应用服务中间件
使用Nginx配置SSL以及部署前端项目
本文介绍了如何使用Nginx配置SSL证书以启用HTTPS,并展示了如何通过Nginx部署前端项目,包括配置SSL证书、设置代理和负载均衡的示例。
21 2
|
5天前
|
关系型数据库 应用服务中间件 PHP
如何在 Ubuntu 16.04 上使用 Nginx 部署 Laravel 应用
如何在 Ubuntu 16.04 上使用 Nginx 部署 Laravel 应用
9 1
|
9天前
|
开发框架 .NET API
在IIS上部署ASP.NET Core Web API和Blazor Wasm详细教程
在IIS上部署ASP.NET Core Web API和Blazor Wasm详细教程
|
7天前
|
弹性计算 安全 搜索推荐
快速部署 ChatGPT Next Web 社区版
ChatGPT Next Web是一款跨平台ChatGPT应用,支持完整的Markdown、LaTex公式、Mermaid流程图等。界面美观,响应迅速,兼容深色模式和PWA,快速加载,数据隐私安全。功能丰富,方便创建、分享个性化对话,内含海量中英文prompt,自动压缩聊天记录,支持多国语言。本文介绍使用计算巢快速部署ChatGPT Next Web社区版。
快速部署 ChatGPT Next Web 社区版
|
9天前
|
JavaScript 安全
SharePoint Online 部署SPFx Web部件
【8月更文挑战第10天】在SharePoint Online中部署SPFx Web部件需先安装Node.js及Yeoman等工具,通过Yo生成器创建项目。开发Web部件后,使用`gulp`命令构建、打包并生成.sppkg文件。接着,在SharePoint管理中心的应用程序目录上传并部署该解决方案包至目标网站。最后,通过编辑页面插入Web部件进行测试,确保遵循最佳实践并维护更新。
|
5天前
|
JavaScript 应用服务中间件 nginx
如何使用 Systemd 和 Nginx 部署 Node.js 应用程序
如何使用 Systemd 和 Nginx 部署 Node.js 应用程序
13 0
|
5天前
|
应用服务中间件 Linux 网络安全
如何在 CentOS 6.5 上使用 Unicorn 和 Nginx 部署 Rails 应用
如何在 CentOS 6.5 上使用 Unicorn 和 Nginx 部署 Rails 应用
9 0
|
5天前
|
缓存 Ubuntu 应用服务中间件
如何在 Ubuntu 14.04 上使用 Passenger 和 Nginx 部署 Rails 应用
如何在 Ubuntu 14.04 上使用 Passenger 和 Nginx 部署 Rails 应用
6 0