Nginx Web快速入门
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快速入门的更多相关文章
- Nginx Web 基础入门
目录 Nginx Web 基础入门 Nginx快速安装 两种方式部署Nginx 如何升级nginx或者添加功能 使用systemd管理nginx nginx相关配置文件 nginx的配置文件详解 虚拟 ...
- Java Web快速入门——全十讲
Java Web快速入门——全十讲 这是一次培训的讲义,就是我在给学生讲的过程中记录下来的,非常完整,原来发表在Blog上,我感觉这里的学生可能更需要. 内容比较长,你可以先收藏起来,慢慢看. 第一讲 ...
- JAVA WEB快速入门之从编写一个基于SpringBoot+Mybatis快速创建的REST API项目了解SpringBoot、SpringMVC REST API、Mybatis等相关知识
JAVA WEB快速入门系列之前的相关文章如下:(文章全部本人[梦在旅途原创],文中内容可能部份图片.代码参照网上资源) 第一篇:JAVA WEB快速入门之环境搭建 第二篇:JAVA WEB快速入门之 ...
- JAVA WEB快速入门之从编写一个基于SpringMVC框架的网站了解Maven、SpringMVC、SpringJDBC
接上篇<JAVA WEB快速入门之通过一个简单的Spring项目了解Spring的核心(AOP.IOC)>,了解了Spring的核心(AOP.IOC)后,我们再来学习与实践Maven.Sp ...
- JAVA WEB快速入门之通过一个简单的Spring项目了解Spring的核心(AOP、IOC)
接上篇<JAVA WEB快速入门之从编写一个JSP WEB网站了解JSP WEB网站的基本结构.调试.部署>,通过一个简单的JSP WEB网站了解了JAVA WEB相关的知识,比如:Ser ...
- JAVA WEB快速入门之从编写一个JSP WEB网站了解JSP WEB网站的基本结构、调试、部署
接上篇<JAVA WEB快速入门之环境搭建>,在完成了环境搭建后(JDK.Tomcat.IDE),现在是万事具备,就差写代码了,今天就来从编写一个JSP WEB网站了解JSP WEB网站的 ...
- Nginx web基础入门
目录 Nginx web基础入门 如何升级nginx或者添加功能 使用systemd管理nginx nginx相关配置文件 nginx的配置文件详解 日志格式 game日志记录实战 日志切割 手写虚拟 ...
- Node.js web快速入门 -- KoaHub.js
介绍 KoaHub.js -- 基于 Koa.js 平台的 Node.js web 快速开发框架.可以直接在项目里使用 ES6/7(Generator Function, Class, Async & ...
- JAVA WEB快速入门之环境搭建
前言 我是一直致力于:.NET技术栈.WEB前端.架构设计相关的开发与管理工作,但因国内大环境影响及公司技术方向发生转变(由.NET全部转为JAVA),需要熟练掌握JAVA WEB相关的知识,故我也得 ...
- Node.js web快速入门 -- KoaHub.js组件koa-static-server
koa-static-server Static file serving middleware for koa with directory, rewrite and index support k ...
随机推荐
- vs2019远程调试win7系统的程序
vs2019远程调试 一.安装vs2019远程调试工具 首先让调试的电脑(也就是不安装vs2019的电脑),安装vs2019远程调试工具:VS_RemoteTools.exe. 网址:进行下载.h ...
- userdel: user zhangsan is currently used by process 1057
我个人推测是在root用户下su 切换到xiaoming用户,然后在xiaoming用户下又切换回root,但是xiaoming用户还被某个进程占用着,所以进程不死,用户del不掉. 所以我们在命令行 ...
- CentOS 7 时区设置 EST和CST设置
1. https://blog.csdn.net/allway2/article/details/102995747 CentOS 7 时区设置# timedatectl status Lo ...
- 打开CMD方式
打开CMD的方式 win+r 输入cmd 常用的Dos命令 1.#盘符切换2.#查看当前文件目录下的所有文件 dir3.#切换目录 cd change directory4.#cd .. 返回上级5. ...
- odoo 权限管理学习总结
环境 odoo-14.0.post20221212.tar base_user_role-12.0.2.1.2.zip 下载地址: https://apps.odoo.com/apps/modules ...
- 使用containerd搭建MinIO集群服务
概述 因为在云服务器上默认只有一块磁盘,如果直接在系统盘上安装MinIO,会被拒绝,所以这里采用containerd的方式绕过这一限制 操作环境 操作系统 CentOS 7.9 MinIO版本 REL ...
- Linux & 标准C语言学习 <DAY13>
一.字符串 字符:类字形单位或符号,包括字母.数字.运算符号.标点符号和其他符号,以及一些功能性符号 串:是一种数据结构,存储类型相同的若干个数据,对于串型结构的处理是批量性的,会从头 ...
- IDEA配置JDK版本的地方, 适用于Compilation failed: internal java compiler error
错误原因: 1. 编译版本不匹配 2.当前项目jdk版本不支持 解决方法 查看项目的jdk 查看工程的jdk 查看java编译器版本 讲这些改成自己需要的版本, 一般就可以解决编译版本出现的错误
- void关键字
在C++中,void表示为无类型,主要有三个用途: (1)函数的 返回值用void,表示函数没有返回值. void func(int a, int b) { //函数体代码 return; } (2) ...
- Python爬取爬取明星关系并写入csv文件
今天用Python爬取了明星关系,数据不多,一共1386条数据,代码如下: import requests from bs4 import BeautifulSoup import bs4 impor ...