Nginx Web快速入门

Nginx概述

Nginx是一个开源且高性能,可靠的服务,代理服务。

开源:直接获取源代码

高性能: 支持海量并发

可靠: 服务稳定

为什么选择Nginx服务

1.Nginx技术成熟,具备的功能是企业最常使用而且最需要的

2.适合当前主流架构趋势,微服务、云架构、中间层

3.统一技术栈,降低维护成本,降低技术更新成本。

Nginx采用Epool网络模型,Apache采用Select模型

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

Epool:当用户发起请求,epool模型会直接进行处理,效率高效,并无连接限制。

Nginx的应用场景

源码安装nginx

Nginx的官方网站https://nginx.org/

#创建存放源码包的目录
[root@web02 ~]$ mkdir /source_code
[root@web02 ~]$ cd /source_code/
#下载Nginx的源码包
[root@web02 /source_code]$ wget https://nginx.org/download/nginx-1.20.1.tar.gz
[root@web02 /source_code]$ ll
total 1040
-rw-r--r-- 1 root root 1061461 May 25 23:34 nginx-1.20.1.tar.gz
#安装nginx的依赖包
[root@web02 /source_code]$ yum install -y pcre-devel zlib-devel # 1.解压
[root@web02 /source_code]$ tar xf nginx-1.20.1.tar.gz
[root@web02 /source_code]$ ll
total 1040
drwxr-xr-x 8 1001 1001 158 May 25 20:35 nginx-1.20.1
-rw-r--r-- 1 root root 1061461 May 25 23:34 nginx-1.20.1.tar.gz
# 2.生成
[root@web02 /source_code]$ cd nginx-1.20.1/
[root@web02 /source_code/nginx-1.20.1]$ ./configure --prefix=/app/nginx-1.20.1
# 3.编译
[root@web02 /source_code/nginx-1.20.1]$ make
# 4.安装
[root@web02 /source_code/nginx-1.20.1]$ make install
# 5.安装后做软链接
[root@web02 /source_code]$ ln -s /app/nginx-1.20.1 /app/nginx
[root@web02 /source_code]$ ll /app
total 0
lrwxrwxrwx 1 root root 17 Jul 15 16:18 nginx -> /app/nginx-1.20.1
drwxr-xr-x 6 root root 54 Jul 15 16:15 nginx-1.20.1
# 6.添加nginx命令的环境变量
[root@web02 /source_code]$ vim /etc/profile
PATH="/app/nginx/sbin:$PATH"
# 7.加载环境变量
[root@web02 /source_code]$ source /etc/profile
# 8.检测配置文件
[root@web02 /source_code]$ nginx -t
nginx: the configuration file /app/nginx-1.20.1/conf/nginx.conf syntax is ok
nginx: configuration file /app/nginx-1.20.1/conf/nginx.conf test is successful
# 9.启动nginx服务
[root@web02 /source_code]$ nginx
# 10.检测nginx进程
[root@web02 /source_code]$ ps -ef|grep [n]ginx
root 13924 1 0 16:21 ? 00:00:00 nginx: master process nginx
nobody 13925 13924 0 16:21 ? 00:00:00 nginx: worker process
# 11.检测nginx的端口
[root@web02 /source_code]$ netstat -lntup|grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13924/nginx: master
# 12.停止Nginx服务的命令
nginx -s stop

打开浏览器:http://10.0.0.8/

Yum安装nginx

进入Nginx官网,选择download,选择stable and mainline,点击RHEL/CentOS,即可出现Nginx官方源配置文件信息

# 1.添加nginx的官方源
[root@web02 ~]$ 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
# 2.先压缩其他仓库,指定从官方下载
[root@web02 /etc/yum.repos.d]$ gzip CentOS-Base.repo
[root@web02 /etc/yum.repos.d]$ gzip epel.repo
[root@web02 /etc/yum.repos.d]$ ll
total 12
-rw-r--r-- 1 root root 542 Jul 5 19:29 CentOS-Base.repo.gz
-rw-r--r-- 1 root root 254 Jul 5 19:24 epel.repo.gz
-rw-r--r-- 1 root root 192 Jul 15 17:21 nginx.repo
# 3.安装nginx
[root@web02 /etc/yum.repos.d]$ yum install -y nginx #检查nginx当前版本
[root@web02 ~]$ /sbin/nginx -v
nginx version: nginx/1.20.1
#查看安装的nginx功能模块
[root@web02 ~]$ /sbin/nginx -V
nginx version: nginx/1.20.1
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' #将利用yum安装的nginx功能模块,添加到我们用源码安装的nginx中
# 1.进入source_code/nginx-1.20.1/目录中
[root@web02 ~]$ cd /source_code/nginx-1.20.1/
[root@web02 /source_code/nginx-1.20.1]$ ll
total 792
drwxr-xr-x 6 1001 1001 326 Jul 15 16:09 auto
-rw-r--r-- 1 1001 1001 311503 May 25 20:35 CHANGES
-rw-r--r-- 1 1001 1001 475396 May 25 20:35 CHANGES.ru
drwxr-xr-x 2 1001 1001 168 Jul 15 16:09 conf
-rwxr-xr-x 1 1001 1001 2590 May 25 20:35 configure
drwxr-xr-x 4 1001 1001 72 Jul 15 16:09 contrib
drwxr-xr-x 2 1001 1001 40 Jul 15 16:09 html
-rw-r--r-- 1 1001 1001 1397 May 25 20:35 LICENSE
-rw-r--r-- 1 root root 442 Jul 15 16:12 Makefile
drwxr-xr-x 2 1001 1001 21 Jul 15 16:09 man
drwxr-xr-x 3 root root 174 Jul 15 16:14 objs
-rw-r--r-- 1 1001 1001 49 May 25 20:35 README
drwxr-xr-x 9 1001 1001 91 Jul 15 16:09 src
# 2.删除Makefile文件
[root@web02 /source_code/nginx-1.20.1]$ rm -rf Makefile
# 3.安装openssl
[root@web02 /source_code/nginx-1.20.1]$ yum install -y openssl-devel
# 4.重新生成功能模块
[root@web02 /source_code/nginx-1.20.1]$ ./configure --prefix=/app/nginx-1.20.1 --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'
# 5.编译并安装
[root@web02 /source_code/nginx-1.20.1]$ make
[root@web02 /source_code/nginx-1.20.1]$ make install
# 6.检查是否安装好功能模块
[root@web02 /source_code/nginx-1.20.1]$ nginx -V
nginx version: nginx/1.20.1
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=/app/nginx-1.20.1 --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'
# 7.检查/source_code/nginx-1.20.1目录下的Makefile是否生成
[root@web02 /source_code/nginx-1.20.1]$ ll
total 79
-rw-r--r-- 1 root root 442 Jul 15 18:00 Makefile
-------------------------------------------------------------------------------------------------------------------------------- PS:我用yum安装的nginx命令位于/sbin/nginx,利用源码安装的nginx的命令位于/app/nginx-1.20.1/sbin/下,但是加入了PATH环境变量,所以直接使用nginx时,用的是源码安装

Nginx相关命令总结

nginx:启动nginx服务

nginx -v:查看当前nginx版本

nginx -V:检查nginx已安装的功能模块

nginx -s stop:停止nginx服务

nginx -s reload:重新加载nginx服务(非重启)

nginx -t:检查配置文件是否正确

./configure:生成指定功能模块

make:源码安装方式时进行编译

make install:源码安装方式时进行安装

Nginx相关的配置文件

1.Nginx主配置文件

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

PS:网站的每个站点都可以在/etc/nginx/conf.d下创建对应的配置文件,default.conf是一个范本,以后自己写的就按照他的格式就行

2.Nginx代理相关参数文件

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

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/sbin/nginx 命令 Nginx命令行管理终端工具
/usr/sbin/nginx-debug 命令 Nginx命令行与终端调试工具

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

路径 类型 作用
/var/log/nginx 目录 Nginx默认存放日志目录
/etc/logrotate.d/nginx 配置文件 Nginx默认的日志切割
[root@web02 ~]$ ll /var/log/nginx/
total 4
# 访问日志
-rw-r----- 1 nginx adm 0 Jul 15 17:25 access.log
# 错误日志
-rw-r----- 1 nginx adm 512 Jul 15 18:07 error.log

Nginx的配置文件

nginx主配置文件内容详解/etc/nginx/nginx.conf

-------------------------------------------------------------------------
------------------------------- 核心模块 ----------------------------------
# nginx的启动用户:nginx
user nginx;
# worker进程(子进程),根据cpu的核心数,几核CPU就对应几个进程,auto自动获取配置
worker_processes auto;
# 错误日志,以及日志的路径和级别
error_log /var/log/nginx/error.log notice;
# pid文件的存放路径
pid /var/run/nginx.pid;
-------------------------------------------------------------------------
------------------------------- 事件驱动块 --------------------------------
events {
# 每个worker进程支持的最大连接数
worker_connections 1024;
}
-------------------------------------------------------------------------
------------------------------- HTTP网站配置 ------------------------------
http {
# 默认nginx支持的文件类型;在这个文件中配置的文件类型在浏览器中可以直接打开访问
include /etc/nginx/mime.types;
# 默认需要下载的类型,不能直接打开访问
default_type application/octet-stream;
# 日志格式 格式名称;nginx内置变量,定义了nginx日志信息的格式(以空格为分隔符)
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; # 虚拟主机相关配置(网站的配置)
server {
# 监听在80端口
listen       80; (ipv4)
# 配置访问的域名或者IP或者主机,就是浏览器要输入什么才能访问这个网站(_代表用ip访问)
server_name _;
# 指定该网站的站点目录:这个站点目录中包含了50x.html报错页面
root         /usr/share/nginx/html;
# 404页面的报错路径
error_page 404 /404.html;
# 页面跳转的相关配置
location = /404.html {      
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {      
}  
}
# 包含所有nginx的虚拟主机配置文件
include /etc/nginx/conf.d/*.conf;
}

Nginx Web快速入门的更多相关文章

  1. Nginx Web 基础入门

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

  2. Java Web快速入门——全十讲

    Java Web快速入门——全十讲 这是一次培训的讲义,就是我在给学生讲的过程中记录下来的,非常完整,原来发表在Blog上,我感觉这里的学生可能更需要. 内容比较长,你可以先收藏起来,慢慢看. 第一讲 ...

  3. JAVA WEB快速入门之从编写一个基于SpringBoot+Mybatis快速创建的REST API项目了解SpringBoot、SpringMVC REST API、Mybatis等相关知识

    JAVA WEB快速入门系列之前的相关文章如下:(文章全部本人[梦在旅途原创],文中内容可能部份图片.代码参照网上资源) 第一篇:JAVA WEB快速入门之环境搭建 第二篇:JAVA WEB快速入门之 ...

  4. JAVA WEB快速入门之从编写一个基于SpringMVC框架的网站了解Maven、SpringMVC、SpringJDBC

    接上篇<JAVA WEB快速入门之通过一个简单的Spring项目了解Spring的核心(AOP.IOC)>,了解了Spring的核心(AOP.IOC)后,我们再来学习与实践Maven.Sp ...

  5. JAVA WEB快速入门之通过一个简单的Spring项目了解Spring的核心(AOP、IOC)

    接上篇<JAVA WEB快速入门之从编写一个JSP WEB网站了解JSP WEB网站的基本结构.调试.部署>,通过一个简单的JSP WEB网站了解了JAVA WEB相关的知识,比如:Ser ...

  6. JAVA WEB快速入门之从编写一个JSP WEB网站了解JSP WEB网站的基本结构、调试、部署

    接上篇<JAVA WEB快速入门之环境搭建>,在完成了环境搭建后(JDK.Tomcat.IDE),现在是万事具备,就差写代码了,今天就来从编写一个JSP WEB网站了解JSP WEB网站的 ...

  7. Nginx web基础入门

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

  8. Node.js web快速入门 -- KoaHub.js

    介绍 KoaHub.js -- 基于 Koa.js 平台的 Node.js web 快速开发框架.可以直接在项目里使用 ES6/7(Generator Function, Class, Async & ...

  9. JAVA WEB快速入门之环境搭建

    前言 我是一直致力于:.NET技术栈.WEB前端.架构设计相关的开发与管理工作,但因国内大环境影响及公司技术方向发生转变(由.NET全部转为JAVA),需要熟练掌握JAVA WEB相关的知识,故我也得 ...

  10. Node.js web快速入门 -- KoaHub.js组件koa-static-server

    koa-static-server Static file serving middleware for koa with directory, rewrite and index support k ...

随机推荐

  1. CentOS7-jdk1.8下载安装

    一.下载网址 jdk全版本:https://www.oracle.com/java/technologies/downloads/archive/ 本次安装版本(jdk1.8.0_151):https ...

  2. 应用和 IPv6

    应用和 IPv6 前言 在数据中心网络IPv6协议改造时,我们通常更关注路由交换的部分.对于应用系统适配IPv6 网络确缺少关注,本文旨在更多的讨论应用和IPv6 的关系,帮助个人.公司和组织能够更改 ...

  3. SpringBoot——实现WebService接口服务端以及客户端开发

    参考:https://blog.csdn.net/qq_43842093/article/details/123076587 https://www.cnblogs.com/yinyl/p/14197 ...

  4. Windows 下TCP长连接保持连接状态TCP keepalive设置

    TCP长连接建立完成后,我们通常需要检测网络的连接状态,以反馈给客户做响应的处理.通过设置TCP keepalive的属性,打开socket的keepalive属性,并设置发送底层心跳包的时间间隔.T ...

  5. lua的一些封装方法

    获取点击屏幕坐标 1 function getTouchPosition() 2 local locationX, locationY 3 4 local listener = cc.EventLis ...

  6. Google colab复现openpose+st-gcn的心路历程

    Google colab复现openpose+st-gcn的心路历程: 因为本人的大致研究方向为动作识别,加上自己的笔记本比较菜鸡,没有牛皮的显卡,所以采用了google的colab.在网上查了三天, ...

  7. 对使用网站模板编写自己的jsp页面的收获

    收获与问题 先感叹一句模板的强大,模板只要按照我的上一篇博客的步骤进行修改,我们就能拥有一个自己写好看许多的界面了. 我目前套用模板还不是很熟练,套用的速度还需要加快,不过目前的我,已经掌握了基本要领 ...

  8. 关于IDEA发出基于APR的本地库加载失败错误的解决------->求解决!

    问题描述 在没有使用Maven项目启动该Project时,Tomcat可以正常使用,但在这里会显示这样的错误: 这个错误,已经查了两天了,相关文件以及解决方法已经翻烂了,还没有解决,放出来集思广益一下 ...

  9. 卡特兰路径和q,t-enumeration 学一半的笔记

    目录 卡特兰 The1st q-analogue of \(C_n\) The 2nd q-analogue of \(C_n\) /定义\(C_n(q)\) The q-Vandermonde co ...

  10. 有关驱动与应用层数据交互的小例子( 以及驱动 epoll 实现的实现方式 )

    介绍 演示了一个驱动对应多个设备,以及各个设备的存取 演示了应用与驱动,mmap 的映射实现与访问 演示了应用层通过 select, poll, epoll 方式读写设备数据 netlink 的方式待 ...