脚本宝典收集整理的这篇文章主要介绍了nginx简介,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
nginx
(发音同engine x)是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/pop3)代理服务器,并在一个BSD-like协议下发行。
nginx
由俄罗斯的程序设计师Igor Sysoev所开发,最初供俄国大型的入口网站及搜寻引擎Rambler使用。
nginx
的特点是占有内存少,并发能力强,事实上nginx
的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx
网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
Nginx
是一款自由的、开源的、高性能的HTTP服务器和 反向代理 服务器;同时也是一个IMAP、POP3、SMTP代理服务器;Nginx可以作为一个HTTP服务器进行网站的发布处理,另外Nginx可以作为反向代理进行负载均衡的实现。
Nginx
使用基于事件驱动架构,使得其可以支持数以百万级别的TCP连接 高度的模块化和自由软件许可证使得第三方模块层出不穷(开源)
Nginx
是一个跨平台服务器,可以运行在Linux,Windows,FreeBSD,solaris,aiX,;mac OS等操作系统上
nginx
是一个很牛的高性能Web和反向代理服务器,它具有很多非常优越的特性:
nginx
由内核和模块组成。其中,内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端请求映射到一个location block(location是nginx配置中的一个指令,用于URL匹配),而在这个location中所配置的每个指令将会启动不同的模块去完成相应的工作。
因此模块可以看做Nginx真正的劳动工作者。通常一个location中的指令会涉及一个handler模块和多个filter模块(当然,多个location可以复用同一个模块)。handler模块负责处理请求,完成响应内容的生成,而filter模块对响应内容进行处理。
nginx的模块从结构上分为核心模块、基础模块和第三方模块
用户根据自己的需要开发的模块都属于第三方模块。正是有了如此多模块的支撑,nginx的功能才会如此强大
ginx模块从功能上分为三类,分别是:
nginx模块分为:核心模块、事件模块、标准Http模块、可选Http模块、邮件模块、第三方模块和补丁等
具体的指令,可参考nginx
的官方文档
nginx
的模块直接被编译进nginx
,因此属于静态编译方式。
启动nginx
后,nginx
的模块被自动加载,与Apache
不一样,首先将模块编译为一个so文件,然后在配置文件中指定是否进行加载。
在解析配置文件时,nginx
的每个模块都有可能去处理某个请求,但是同一个处理请求只能由一个模块来完成。
启动nginx
时,会启动一个Master
进程,这个进程不处理任何客户端的请求,主要用来产生worker
线程,一个worker
线程用来处理n个request
。
Nginx主要由一个master主进程和多个worker进程
master主进程主要是管理worker进程,对网络事件进行收集和分发:
接收来自外界的信号
向各worker进程发送信号
监控worker进程的运行状态,当worker进程退出后(异常情况下),会自动重新启动新的worker进程
nginx用一个独立的worker进程来处理一个请求,一个worker进程可以处理多个请求:
当一个worker进程在accept这个连接之后,就开始读取请求,解析请求,处理请求,产生数据后,再返回给客户端,最后才断开连接。
一个请求,完全由worker进程来处理,而且只在一个worker进程中处理。采用这种方式的好处
节省锁带来的开销。对于每个worker进程来说,独立的进程,不需要加锁,所以省掉了锁带来的开销,同时在编程以及问题查上时,也会方便很多
独立进程,减少风险。
采用独立的进程,可以让互相之间不会影响,一个进程退出后,其它进程还在工作,服务不会中断,master进程则很快重新启动新的worker进程。
在一次请求里无需进程切换
每个worker进程都有一个独立的连接池,连接池的大小是worker_connections。这里的连接池里面保存的其实不是真实的连接,它只是一个worker_connections大小的一个ngx_connection_t结构的数组。并且,nginx会通过一个链表free_connections来保存所有的空闲ngx_connection_t,每次获取一个连接时,就从空闲连接链表中获取一个,用完后,再放回空闲连接链表里面。一个nginx能建立的最大连接数,应该是worker_connections * worker_processes。当然,这里说的是最大连接数,对于HTTP请求本地资源来说,能够支持的最大并发数量是worker_connections * worker_processes,而如果是HTTP作为反向代理来说,最大并发数量应该是worker_connections * worker_processes/2。因为作为反向代理服务器,每个并发会建立与客户端的连接和与后端服务的连接,会占用两个连接。
下图展示了基本的WEB服务请求步骤
网页访问流程 利用DNS协议进行域名解析 --> 建立tcp协议三次握手过程 --> 客户端发出访问网站相应页面请求(发出http协议请求报文) --> 服务端发出相应访问页面的请求信息(发出http) --> 断开tcp协议四次挥手过程
Nginx 代理 说到代理,首先我们要明确一个概念,所谓代理就是一个代表、一个渠道此时就涉及到两个角色,一个是被代理角色 (A_),一个是目标角色 (B_),A_ 通过这个代理访问 B_ 完成一些任务的过程称为代理 (C_) 操作过程;比如客人去买双鞋,这个店铺就是 (C_),(A_) 就是厂家,(B_) 就是用户。代理呢又分为正向代理和反向代理
反向代理 反向代理应该是Nginx做的最多的一件事了,什么是反向代理呢,以下是百度百科的说法:反向代理(reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。简单来说就是真实的服务器不能直接被外部网络访问,所以需要一台代理服务器,而代理服务器能被外部网络访问的同时又跟真实服务器在同一个网络环境,当然也可能是同一台服务器,端口不同而已。
如下图:
如下图:
WEB服务器 Nginx本身也是一个静态资源的服务器,当只有静态资源的时候,就可以使用Nginx来做服务器,同时现在也很流行动静分离,就可以通过Nginx来实现,首先看看Nginx做静态资源服务器
静态分离 动静分离是让动态网站里的动态网页根据一定规则把不变的资源和经常变的资源区分开来,动静资源做好了拆分以后,我们就可以根据静态资源的特点将其做缓存操作,这就是网站静态化处理的核心思路。
如下图:
环境说明
系统 | IP |
---|---|
centos8 | 192.168.25.144 |
//关闭防火墙
[root@nginx ~]# systemctl disable --now firewalld
[root@nginx ~]# sed -ri 's/^(SELINUX=).*/1disabled/g' /etc/selinux/config
[root@nginx ~]# setenforce 0
//安装依赖包
[root@nginx ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
[root@nginx ~]# yum -y groups mark install 'Development Tools'
//创建系统用户Nginx
[root@nginx ~]# useradd -r -M -s /sbin/noLOGin nginx
//创建日志存放目录
[root@nginx ~]# mkdir -p /VAR/log/nginx
[root@nginx ~]# chown -R nginx.nginx /var/log/nginx/
//下载Nginx源码包
[root@nginx ~]# cd /usr/local/src/
[root@nginx src]# wget http://nginx.org/download/nginx-1.20.1.tar.gz
//编译安装
[root@nginx src]# tar xf nginx-1.20.1.tar.gz
[root@nginx src]# cd nginx-1.20.1
[root@nginx nginx-1.20.1]# ./configure
--prefix=/usr/local/nginx
--user=nginx
--group=nginx
--with-debug
--with-http_ssl_module
--with-http_realip_module
--with-http_image_filter_module
--with-http_gunzip_module
--with-http_gzip_static_module
--with-http_stub_status_module
--http-log-path=/var/log/nginx/access.log
--error-log-path=/var/log/nginx/error.log
[root@nginx nginx-1.20.1]# make && make install
// 启动nginx
[root@nginx ~]# /usr/local/nginx/sbin/nginx
[root@Nginx ~]# ss -antl
State recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
//配置环境变量
[root@nginx ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@nginx ~]# source /etc/profile.d/nginx.sh
//服务控制方式,使用nginx命令
-t //检查配置文件语法
-v //输出nginx的版本
-c //指定配置文件的路径
-s //发送服务控制信号,可选值有{stop|quit|reopen|reload}
[root@nginx ~]# nginx -t //检查配置文件语法
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx ~]# nginx -v //查看nginx的版本
nginx version: nginx/1.20.1
[root@nginx ~]# nginx -V // 查看更加详细的信息
nginx version: nginx/1.20.1
built by gcc 8.5.0 20210514 (red hat 8.5.0-3) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
[root@nginx ~]# nginx -h //查看帮助
nginx version: nginx/1.20.1
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: /usr/local/nginx/)
-e filename : set error log file (default: /var/log/nginx/error.log)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
// 修改配置文件的方法
[root@nginx conf]# cp nginx.conf /opt/
[root@nginx conf]# cp mime.types /opt/
[root@nginx opt]# ls
mime.types nginx.conf
[root@nginx opt]# vim nginx.conf
#user nobody;
worker_processes 1;
[root@nginx ~]# nginx -s stop;nginx -c /opt/nginx.conf //指定配置文件的路径
root@Nginx ~]# ps -ef | grep nginx
root 1817 1 0 04:42 ? 00:00:00 nginx: master process nginx -c /opt/nginx.conf
nginx 1818 1817 0 04:42 ? 00:00:00 nginx: worker process
root 1821 1734 0 04:42 pts/0 00:00:00 grep --color=auto nginx
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 4; //修改nginx的worker进程数量,默认为1
[root@nginx ~]# nginx -s reload //发送服务控制信号,重新加载配置文件
[root@nginx ~]# ps -ef | grep nginx
root 1725 1 0 04:10 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 1769 1725 0 04:22 ? 00:00:00 nginx: worker process
nginx 1770 1725 0 04:22 ? 00:00:00 nginx: worker process
nginx 1771 1725 0 04:22 ? 00:00:00 nginx: worker process
nginx 1772 1725 0 04:22 ? 00:00:00 nginx: worker process
root 1779 1734 0 04:22 pts/0 00:00:00 grep --color=auto nginx
主配置文件:安装路径/conf/nginx.conf
nginx常见的配置文件及其作用
配置文件 | 作用 |
---|---|
nginx.conf | nginx的基本配置文件 |
mime.types | MIME类型关联的扩展文件 |
fastcgi.conf | 与fastcgi相关的配置 |
proxy.conf | 与proxy相关的配置 |
sites.conf | 配置nginx提供的网站,包括虚拟主机 |
nginx.conf的内容分为以下几段
配置指令:要以分号结尾,语法格式如下
derective value1 [value2 ...];
支持使用变量
daemon {on|off}; //是否以守护进程方式运行nginx,调试时应设置为off
//用法
Syntax: daemon on | off; #语法
Default: daemon on; #默认值
Context: main #可以配置在那个字段中
[root@Nginx ~]# vim /opt/nginx.conf
#user nobody;
worker_processes 1;
daemon off;
//重启nginx
[root@Nginx ~]# nginx -s stop;nginx -c /opt/nginx.conf
[root@Nginx ~]# nginx -s stop
[root@Nginx ~]# nginx
//此时nginx将不会以守护进程的方式运行,会一直占据着前台屏幕
m aster_process {on|off}; //是否以master/worker模型来运行nginx,调试时可以设置为off
master_process {on|off}; //是否以master/worker模型来运行nginx,调试时可以设置为off
//用法
Syntax: master_process on | off;
Default: master_process on;
Context: main
//nginx默认是以master/worker模型来运行
[root@nginx ~]# ps -ef | grep nginx
root 1777 1 0 08:01 ? 00:00:00 nginx: master process nginx
nginx 1778 1777 0 08:01 ? 00:00:00 nginx: worker process
root 1800 1779 0 08:02 pts/0 00:00:00 grep --color=auto nginx
[root@nginx ~]# vim /opt/nginx.conf
#user nobody;
worker_processes 1;
master_process off;
//重读nginx配置文件
[root@nginx ~]# nginx -s stop;nginx -c /opt/nginx.conf
[root@nginx ~]# ps -ef | grep nginx
root 1804 1 0 08:04 ? 00:00:00 nginx -c /opt/nginx.conf
root 1806 1779 0 08:04 pts/0 00:00:00 grep --color=auto nginx
error_log 位置 级别; //配置错误日志
//用法,在不同的字段有不同的含义,比如在main字段写就会对整个nginx产生影响,在http字段写就会对某个网站产生影响
Syntax: error_log file [level];
Default: error_log logs/error.log error;
Context: main, http, mail, stream, server, location
[root@nginx ~]# vim /opt/nginx.conf
#user nobody;
worker_processes 1;
error_log logs/error.log; //没有写记录日志的级别,表示默认记录error级别的日志
//重读nginx配置文件
[root@nginx ~]# vim /opt/nginx.conf
[root@nginx ~]# nginx -s stop;nginx -c /opt/nginx.conf
[root@nginx ~]# ls /usr/local/nginx/logs/
error.log nginx.pid
//访问一个不存在的资源
[root@nginx ~]# curl http://192.168.25.147/tt.txt
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
[root@nginx ~]# tail -f /usr/local/nginx/logs/error.log
2021/10/25 09:24:45 [error] 1817#0: *1 open() "/usr/local/nginx/html/tt.txt" failed (2: No such file or directory), client: 192.168.200.144, server: localhost, request: "GET /tt.txt HTTP/1.1", host: "192.168.200.144"
error_log里的位置和级别能有以下可选项
位置 | 含义 |
---|---|
file | 记录到某个文件中 |
stdree | 记录到标准错误中(不推荐) |
syslog:server=address[,parameter=value] | 记录到某台日志服务器中 |
memory:size | 记录到内存中(不推荐) |
日志级别 | 含义 |
---|---|
debug | 若要使用debug级别,需要在编译nginx时使用–with-debug选项 |
info | 一般信息 |
notice | 不影响正常功能,需要注意的消息 |
warn | 可能影响系统功能,需要提醒用户的重要事件 |
error | 错误信息(常用的) |
crit | 紧急,比较严重的 |
alert | 必须马上处理的 |
emerg | 会导致系统不可用的 |
user USERNAME [GROUPNAME]; //指定运行worker进程的用户和组
//用法
Syntax: user [group]; #语法
Default: user nobody nobody; #默认值
Context: main #可以配置在那个字段中
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx; #建议手动指定用户
worker_processes 1;
pid /path/to/pid_file; //指定nginx守护进程的pid(守护进程),使用时需要把前面的注释取消掉
//用法
Syntax: pid file;
Default: pid logs/nginx.pid;
Context: main
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
worker_rlimit_nofile number; //设置所有worker进程最大可以打开的文件数,默认为1024,最大值为655350
//用法
Syntax: worker_rlimit_nofile number;
Default: 1024
Context: main
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 1;
worker_rlimit_nofile 1024;
worker_rlimit_core size; //指明所有worker进程所能够使用的总体的最大核心文件大小,保持默认即可
//用法
Syntax: worker_rlimit_core size;
Default: —
Context: main
worker_processes n; //启动n个worker进程,这里的n为了避免上下文切换,通常设置为cpu总核心数-1或等于总核心数
worker_cpu_affinity cpumask ...; //将进程绑定到某cpu中,避免频繁刷新缓存
//cpumask:使用8位二进制表示cpu核心,如:
0000 0001 //第一颗cpu核心
0000 0010 //第二颗cpu核心
0000 0100 //第三颗cpu核心
0000 1000 //第四颗cpu核心
0001 0000 //第五颗cpu核心
0010 0000 //第六颗cpu核心
0100 0000 //第七颗cpu核心
1000 0000 //第八颗cpu核心
示例:
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes 3; //设置cpu核心数
worker_rlimit_nofile 1024;
worker_cpu_affinity cpumask 0001 0010 0100; //收到将进程绑定到某cpu核心
timer_resolution interval; //计时器解析度。降低此值,可减少gettimeofday()系统调用的次数
worker_priority number; //指明worker进程的nice值
示例:
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes 2;
worker_rlimit_nofile 1024;
worker_cpu_affinity 0001 0010;
worker_priority -20;
[root@nginx ~]# nginx -s stop;nginx
[root@nginx ~]# ps -elf|grep nginx //原先的80现在设置成了60
1 S root 109329 1 0 80 0 - 20408 - 16:36 ? 00:00:00 nginx: master process nginx
5 S nginx 109330 109329 0 60 -20 - 28556 do_epo 16:36 ? 00:00:00 nginx: worker process
5 S nginx 109331 109329 0 60 -20 - 28556 do_epo 16:36 ? 00:00:00 nginx: worker process
0 S root 109836 1605 0 80 0 - 3087 - 16:36 pts/0 00:00:00 grep --color=auto nginx
accept_mutex {off|on}; //master调度用户请求至各worker进程时使用的负载均衡锁;on表示能让多个worker轮流地、序列化地去响应新请求
lock_file file; //accept_mutex用到的互斥锁锁文件路径
use [epoll | rtsig | select | poll]; //指明使用的事件模型,建议让nginx自行选择
worker_connections number; //每个进程能够接受的最大连接数
示例:
user nginx;
worker_processes 2;
worker_rlimit_nofile 1024;
worker_cpu_affinity 0001 0010;
worker_priority -20;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 10240; //再此设置每个进程最大连接数
}
keepalive_timeout number; //长连接的超时时长,默认为65s
keepalive_requests number; //在一个长连接上所能够允许请求的最大资源数
keepalive_disable [msie6|safari|none]; //为指定类型的UserAgent禁用长连接
tcp_nodelay on|off; //是否对长连接使用TCP_NODELAY选项,为了提升用户体验,通常设为on
client_header_timeout number; //读取http请求报文首部的超时时长
client_body_timeout number; //读取http请求报文body部分的超时时长
send_timeout number; //发送响应报文的超时时长
LNMP:php要启用fpm模型 配置示例如下:
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000; //定义反向代理
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_SCRIPT_NAME;
include fastcgi_params;
}
http{…}:配置http相关,由ngx_http_core_module模块引入。nginx的HTTP配置主要包括四个区块,结构如下:
http {//协议级别
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
gzip on;
upstream {//负载均衡配置
...
}
server {//服务器级别,每个server类似于httpd中的一个<VirtualHost>(也就是相当于一个网站)
listen 80;
server_name localhost;
location / {//请求级别,类似于httpd中的<Location>,用于定义URL与本地文件系统的映射关系
root html;
index index.html index.htm;
}
}
}
我们自己编写一个网站
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
#gzip on;
server { //我们自己编写的网站
listen 88;
server_name www.caiccc.COM;
location / {
root html/test;
index index.html;
}
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
[root@nginx ~]# cd /usr/local/nginx/html/
[root@nginx html]# ls
50x.html index.html
[root@nginx html]# mkdir test
[root@nginx html]# echo 'xiedezhenshuai!!!' > test/index.html
[root@nginx ~]# nginx -s stop;nginx
[root@nginx ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:88 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@nginx ~]#
原网页
我们增加端口后访问网页//listen:指定监听的地址和端口
listen address[:port];
listen port;
//server_name NAME`[...]; 后面可跟多个主机,名称可使用正则表达式或通配符
当有多个server时,匹配顺序如下:
1. 先做精确匹配检查
2. 左侧通配符匹配检查,如*.example.com
3. 右侧通配符匹配检查,如web.*
4. 正则表达式匹配检查,如~ ^.*.example.com$
5. default_server
location / {
root html;
index index.html index.htm;
}
[root@nginx ~]# mkdir -p /var/www/html
[root@nginx ~]# chown -R nginx.nginx /var/www/html/
[root@nginx ~]# echo "hahahhh" > /var/www/html/index.html
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
alias /var/www/html/; //这里路径写绝对路径
index index.html index.htm;
}
}
root path;和alias path;设置的区别
root path方式设置location表示最终访问的资源地址是root path指定的位置+location后面的URI
alias方式设置表示最终访问的资源地址是alias后面跟的路径
location / {
root html;
index index.html index.htm; //网页默认访问主页面
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index test index.html index.htm; //自定义网页默认访问页面
}
}
error_page code [...] [=code] URI | @name
根据http响应状态码来指明特用的错误页面,例如 error_page 404 /404_customed.html
[=code]
:以指定的响应码进行响应,而不是默认的原来的响应,默认表示以新资源的响应码为其响应码,例如 error_page 404 =200 /404_customed.html
[root@lamp ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.php index.html index.htm;
}
error_page 404 /404.html; //状态码如果是404就用404.html页面来响应
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
[root@lamp ~]# cd /usr/local/nginx/html/
[root@lamp html]# ls
50x.html index.html index.php
[root@lamp html]# vi 404.html
<html>
<head>
<title>test page</title>
</head>
<body>
<a href="http://www.baidu.com">baidu</a>
</body>
</html>
[root@lamp html]# nginx -s stop;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 logs/access.log main;
//注意:此处可用变量为nginx各模块内建变量
[root@lamp ~]# vim /usr/local/nginx/conf/nginx.conf
http {
include 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 logs/access.log main;
[root@nginx ~]# nginx -s reload //去重新访问一下nginx网站
[root@nginx ~]# tail -f /usr/local/nginx/logs/access.log
Last login: Wed Oct 27 14:23:46 2021 From 192.168.200.1
[root@nginx ~]# tail -f /usr/local/nginx/logs/access.log
192.168.200.1(代表本机) -(代表本身) -(代表用户) [27/Oct/2021:15:02:22 +0800](访问时间) "GET / HTTP/1.1"(用get方法获取根下面的资源,用的http1.1版本号) 304(状态码) 0 "-" (代表访问网站时没用跳转)"Mozilla/5.0 (Windows NT 10.0; Win64; x64) Applewebkit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36"(所使用的浏览器) "-"(是否是跳转过来访问的,不是用 - 代表,是就表示跳转的路径)
[root@nginx html]# nginx -V
nginx version: nginx/1.20.1
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-3) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
[root@nginx html]#
[root@nginx ~]# ls
echo-nginx-module-master.zip
anaconda-ks.CFg nginx-1.20.1
echo-nginx-module-master nginx-1.20.1.tar.gz
[root@nginx ~]# ./configure
--prefix=/usr/local/nginx
--user=nginx
--group=nginx
--with-debug
--with-http_ssl_module
--with-http_realip_module
--with-http_image_filter_module
--with-http_gunzip_module
--with-http_gzip_static_module
--with-http_stub_status_module
--http-log-path=/var/log/nginx/access.log
--error-log-path=/var/log/nginx/error.log --add-module=../echo-nginx-module-master //添加新的模块
[root@nginx ~]# make
[root@nginx nginx-1.20.1]# ls objs/
addon nginx ngx_auto_headers.h src
autoconf.err nginx.8 ngx_modules.c
Makefile ngx_auto_config.h ngx_modules.o
[root@nginx nginx-1.20.1]# ll objs/nginx /usr/local/nginx/sbin/nginx
-rwxr-xr-x. 1 root root 6831056 10月 27 15:38 objs/nginx
-rwxr-xr-x. 1 root root 6308640 10月 25 11:35 /usr/local/nginx/sbin/nginx
[root@nginx nginx-1.20.1]# cp /usr/local/nginx/sbin/nginx /opt/
[root@nginx nginx-1.20.1]# ls /opt/
nginx
[root@nginx nginx-1.20.1]# nginx -s stop;objs/nginx -c /usr/local/nginx/conf/nginx.conf
[root@nginx nginx-1.20.1]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@nginx nginx-1.20.1]# ps -ef|grep nginx
root 220850 1 0 15:40 ? 00:00:00 nginx: master process objs/nginx -c /usr/local/nginx/conf/nginx.conf
nginx 220851 220850 0 15:40 ? 00:00:00 nginx: worker process
nginx 220852 220850 0 15:40 ? 00:00:00 nginx: worker process
root 221979 1503 0 15:41 pts/0 00:00:00 grep --color=auto nginx
[root@nginx nginx-1.20.1]#
检测新程序有没有问题
location / {
root html;
index index.html index.htm;
}
location /test { //自己查找跟下面的test,查找到后打印一句话
echo "test";
error_page 404 =200 /404.html;
}
[root@nginx ~]# ./nginx-1.20.1/objs/nginx -t //用新文件检查语法
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx ~]#
[root@nginx ~]# ./nginx-1.20.1/objs/nginx -s reload
访问测试
[root@nginx html]# curl http://192.168.200.144
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@nginx html]# curl http://192.168.200.144/test
test
替换原程序
[root@nginx ~]# nginx -t //配置文件修改后用原程序会报错,新程序不会报错
nginx: [emerg] unknown directive "echo" in /usr/local/nginx/conf/nginx.conf:51
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
[root@nginx html]#
[root@nginx ~]# cp nginx-1.20.1/objs/nginx /usr/local/nginx/sbin/
cp:是否覆盖'/usr/local/nginx/sbin/nginx'? y
[root@nginx ~]#
[root@nginx ~]#
[root@nginx ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx ~]#
再切换为原程序来执行
[root@nginx ~]# ./nginx-1.20.1/objs/nginx -s stop;nginx
[root@nginx ~]# ps -ef |grep nginx
root 342545 1 0 16:13 ? 00:00:00 nginx: master process nginx
nginx 342546 342545 0 16:13 ? 00:00:00 nginx: worker process
nginx 342547 342545 0 16:13 ? 00:00:00 nginx: worker process
root 343504 73085 0 16:13 pts/2 00:00:00 grep --color=auto nginx
[root@nginx ~]#
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /test {
echo "test";
}
location = /test{
echo "rush b";
}
error_page 404 =200 /404.html;
[root@nginx ~]# nginx -s reload
[root@nginx ~]# curl http://192.168.200.144/test
rush b
[root@nginx ~]#
location区段,通过指定模式来与客户端请求的URI相匹配
功能:允许根据用户请求的URI来匹配定义的各location,匹配到时,此请求将被相应的 location配置块中的配置所处理,例如做访问控制等功能
语法:location [ 修饰符 ] pattern {…}
常用修饰符说明:
= | 精确匹配 |
---|---|
~ | 正则表达式模式匹配,区分大小写 |
~* | 正则表达式模式匹配,不区分大小写 |
^~ | 前缀匹配,类似于无修饰符的行为,也是以指定模块开始,不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正则表达式 |
@ | 定义命名location区段,这些区段客户端不能访问,只可以由内部产生的请求来访问,如try_files或error_page等 |
//没有修饰符表示必须以指定模式开始,此写法属于模糊匹配
server {
server_name www.example.com;
location /test {
......
}
}
//以上写法可以匹配到
http://www.example.com/test
http://www.example.com/test?p1=11&p2=22
http://www.example.com/test/111
// =: 表示必须与指定的模式精确匹配
server {
server_name www.example.com;
location = /test {
......
}
}
//以上写法可以匹配到
http://www.example.com/test
http://www.example.com/test?p1=11&p2=22
//以下内容无法匹配
http://www.example.com/test/
http://www.example.com/test/abcde
// ~: 表示指定的正则表达式要区分大小写
server {
server {
server_name www.example.com;
location ~ ^/test$ {
......
}
}
//以上写法可以匹配
http://www.example.com/test
http://www.example.com/test?p1=11&p2=22
//以下内容无法匹配
http://www.example.com/test/
http://www.example.com/TEST
http://www.example.com/testfs
// ~*: 表示指定的正则表达式不区分大小写
server {
server_name www.example.com;
location ~* ^/test$ {
......
}
}
//以上写法可以匹配到
http://www.example.com/test
http://www.example.com/test?p1=11&p2=22
http://www.example.com/TEST
//以下内容无法匹配
http://www.example.com/test/
http://www.example.com/testfs
~:类似于无修饰符的行为,也是以指定模式开始,不同的是,如果模式匹配,则停止搜索其他模式
查找顺序和优先级:由高到底依次为
//优先级次序如下
( location = 路径 ) --> ( location ^~ 路径 ) --> ( location ~ 正则 ) --> ( location ~* 正则 ) --> ( location 路径 )
一般用于location段,也可以用于server字段,此时就对当前server字段中的所有location生效,也可以用于http字段,此时就对当前http字段中的所有server字段生效
//用法
allow:白名单,设定允许哪台或哪些主机访问
deny:黑名单,设定禁止哪台或哪些主机访问
location / {
root html;
index index.html;
deny all; //拒绝所以IP访问
}
//语法
auth_basic "欢迎信息";
auth_basic_user_file "/path/to/user_auth_file"
//user_auth_file内容格式为:
username:password
//生成密码文件
[root@nginx ~]# yum -y install httpd-tools
[root@nginx ~]# htpasswd -c -m /usr/local/nginx/conf/.passwd tom
New password:
Re-type new password:
Adding password for user tom
[root@nginx ~]# cat /usr/local/nginx/conf/.passwd
tom:$apr1$KeddkwXz$z0386sTtbbqR45SJV7M3z/
[root@nginx ~]#
[root@nginx ~]# vi /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.html index.htm;
}
location /test {
root html;
index index.html;
auth_basic "xiedezhenshuai";
auth_basic_user_file ".passwd";
}
[root@nginx conf]# cd /usr/local/nginx/html/
[root@nginx html]# ls
404.html 50x.html index.html
[root@nginx html]# mkdir test
[root@nginx html]# vi index.html
[root@nginx html]# echo "hahhaha" > test/index.html
[root@nginx html]# nginx -s reload
生成私钥,生成证书签署请求并获得证书,然后在nginx.conf中配置如下内容: 生成证书
//创建密钥存放目录
[root@nginx ~]# mkdir -p /etc/pki/CA
[root@nginx ~]# cd /etc/pki/CA
[root@nginx CA]# mkdir private
//生成私钥
[root@nginx CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048) //生成密钥,括号必须要
Generating RSA private key, 2048 bit long modulus (2 primes)
.....+++++
........................+++++
e is 65537 (0x010001)
[root@nginx CA]# ls private/
cakey.pem
//CA生成自签名证书
[root@nginx CA]# openssl req -new -x509 -key private/cakey.pem -out cACERt.pem -days 365
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code ) [XX]:CN //国家的名字,简写
State or Province Name (full name) []:HB //省份的名字
Locality Name (eg, city) [Default City]:WH //城市的名字
Organization Name (eg, company) [Default Company Ltd]:runtime //公司的域名
Organizational Unit Name (eg, section) []:runtime //单位的名字
Common Name (eg, your name or your server's hostname) []:www.aabc.com //主机的名字
Email Address []:1@34.com //邮箱
//在CA目录下创建certs newcerts crl目录,并生成序列号
[root@nginx CA]# ls
cacert.pem private
[root@nginx CA]# mkdir certs newcerts crl
[root@nginx CA]# ls
cacert.pem certs crl newcerts private
[root@nginx CA]# cd /usr/local/nginx/conf/ssl
[root@nginx CA]# touch index.txt && echo 01 > serial
[root@nginx CA]# ls
cacert.pem certs crl index.txt newcerts private serial
//创建证书存放目录
[root@nginx ~]# mkdir /usr/local/nginx/conf/ssl
//生成密钥
[root@nginx ssl]# (umask 077;openssl genrsa -out nginx.key 2048)
Generating RSA private key, 2048 bit long modulus (2 primes)
......................................+++++
.+++++
e is 65537 (0x010001)
[root@nginx ssl]# ls
nginx.key
// 生成证书签署请求
[root@nginx ssl]# openssl req -new -key nginx.key -days 365 -out nginx.csr
Ignoring -days; not generating a certificate
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CH
State or Province Name (full name) []:HB
Locality Name (eg, city) [Default City]:WH
Organization Name (eg, company) [Default Company Ltd]:runtime
Organizational Unit Name (eg, section) []:runtime
Common Name (eg, your name or your server's hostname) []:www.aabc.com
Email Address []:1@34.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: //是否需要给证书设置密码,可以直接回车跳过
An optional company name []:
[root@nginx ssl]# openssl ca -in nginx.csr -out nginx.crt -days 365
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 1 (0x1)
Validity
Not Before: Oct 27 10:21:49 2021 GMT
Not After : Oct 27 10:21:49 2022 GMT
Subject:
countryName = CN
stateOrProvinceName = HB
organizationName = runtime
organizationalUnitName = runtime
commonName = www.aabc.com
emailAddress = 1@34.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
89:36:99:05:31:41:2C:86:11:00:22:E4:B9:68:7E:68:44:6F:16:9D
X509v3 Authority Key Identifier:
keyid:E5:88:F3:CB:29:F4:DC:17:15:70:9C:32:81:FA:89:AA:0F:E7:B9:AF
Certificate is to be certified until Oct 27 10:21:49 2022 GMT (365 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
[root@nginx ssl]# ls
nginx.crt nginx.csr nginx.key
//证书生成完毕
[root@nginx ssl]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 443 ssl;
server_name www.aabc.com;
ssl_certificate ssl/nginx.crt; //指定证书位置
ssl_certificate_key ssl/nginx.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
}
[root@nginx ssl]# nginx -s reload
以上是脚本宝典为你收集整理的nginx简介全部内容,希望文章能够帮你解决nginx简介所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。