Nginx部署及Web基础

简介: 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

相关实践学习
通过日志服务实现云资源OSS的安全审计
本实验介绍如何通过日志服务实现云资源OSS的安全审计。
相关文章
|
3月前
|
应用服务中间件 网络安全 nginx
手把手教你使用 Docker 部署 Nginx 教程
本文详解Nginx核心功能与Docker部署优势,涵盖镜像拉取、容器化部署(快速、挂载、Compose)、HTTPS配置及常见问题处理,助力高效搭建稳定Web服务。
1572 4
|
3月前
|
应用服务中间件 Linux nginx
在虚拟机Docker环境下部署Nginx的步骤。
以上就是在Docker环境下部署Nginx的步骤。需要注意,Docker和Nginix都有很多高级用法和细节需要掌握,以上只是一个基础入门级别的教程。如果你想要更深入地学习和使用它们,请参考官方文档或者其他专业书籍。
193 5
|
11月前
|
应用服务中间件 PHP nginx
今日小结通过aliyun的本地容器镜像部署我的nginx和php环境
简介: 本教程介绍如何基于 Dragonwell 的 Ubuntu 镜像创建一个运行 Nginx 的 Docker 容器。首先从阿里云容器镜像服务拉取基础镜像,然后编写 Dockerfile 确保 Nginx 作为主进程运行,并暴露 80 端口。最后,在包含 Dockerfile 的目录下构建自定义镜像并启动容器,确保 Nginx 在前台运行,避免容器启动后立即退出。通过 `docker build` 和 `docker run` 命令完成整个流程。
442 25
今日小结通过aliyun的本地容器镜像部署我的nginx和php环境
|
5月前
|
运维 数据可视化 C++
2025 热门的 Web 化容器部署工具对比:Portainer VS Websoft9
2025年热门Web化容器部署工具对比:Portainer与Websoft9。Portainer以轻量可视化管理见长,适合技术团队运维;Websoft9则提供一站式应用部署与容器管理,内置丰富开源模板,降低中小企业部署门槛。两者各有优势,助力企业提升容器化效率。
424 1
2025 热门的 Web 化容器部署工具对比:Portainer VS Websoft9
|
6月前
|
Java 应用服务中间件 Docker
java-web部署模式概述
本文总结了现代 Web 开发中 Spring Boot HTTP 接口服务的常见部署模式,包括 Servlet 与 Reactive 模型、内置与外置容器、物理机 / 容器 / 云环境部署及单体与微服务架构,帮助开发者根据实际场景选择合适的方案。
254 25
|
6月前
|
安全 JavaScript Java
java Web 项目完整案例实操指南包含从搭建到部署的详细步骤及热门长尾关键词解析的实操指南
本项目为一个完整的JavaWeb应用案例,采用Spring Boot 3、Vue 3、MySQL、Redis等最新技术栈,涵盖前后端分离架构设计、RESTful API开发、JWT安全认证、Docker容器化部署等内容,适合掌握企业级Web项目全流程开发与部署。
543 0
|
8月前
|
存储 应用服务中间件 nginx
在使用Nginx之后,如何在web应用中获取用户IP以及相关原理
但总的来说,通过理解网络通信的基础知识,了解http协议以及nginx的工作方式,我们已经能在大多数情况下准确地获取用户的真实IP地址了,在调试问题或者记录日志时会起到很大的帮助。
477 37
|
8月前
|
应用服务中间件 Linux 网络安全
技术指南:如何把docsify项目部署到基于CentOS系统的Nginx中。
总结 与其他部署方法相比,将docsify项目部署到基于CentOS系统的Nginx中比较简单。以上步骤应当帮助你在不花费太多时间的情况下,将你的项目顺利部署到Nginx中。迈出第一步,开始部署你的docsify项目吧!
375 14
|
8月前
|
人工智能 安全 程序员
用 Colab 和 ngrok 免费部署你的 Web UI 项目,随时随地访问!
用 Colab 和 ngrok 免费部署你的 Web UI 项目,随时随地访问!
1052 12
|
11月前
|
中间件 关系型数据库 数据库
docker快速部署OS web中间件 数据库 编程应用
通过Docker,可以轻松地部署操作系统、Web中间件、数据库和编程应用。本文详细介绍了使用Docker部署这些组件的基本步骤和命令,展示了如何通过Docker Compose编排多容器应用。希望本文能帮助开发者更高效地使用Docker进行应用部署和管理。
352 19