当前位置: 首页 > news >正文

42、nginx之nginx.conf

nginx----web服务器

一、nginx

http就是apache,在国内很少。

nginx是开源的,是一款高性能,轻量级的web服务软件。

稳定性高,而且版本迭代比较快(修复bug速度比较快,安全性快)

消耗系统资源很低,http的请求并发连接,单台服务器开源支持30000-50000个并发请求。(系统资源全部分配给nginx)

单个节点的nginx一般支持20000个并发。

1.1、nginx的功能:

1、静态文件服务:静态页面,可以直接提高静态文件服务,html css jsp。处理静态页面的响应速度很快,效率很好。

2、代理:正向代理,反向代理。可以实现负载均衡,高可用和故障转移。

3、动态内容处理,nginx并不能直接处理动态请求,可以通过中间件nginx 中间件(php,tomat) mysql把动态请求转发给后端服务器。

4、支持加密的http,https

5、可以实现重定向。

6、虚拟主机,一个nginx可以配置多个域名和站点。

7、nginx自带缓存。

8、性能可以扩展。处理能力可以随时调整。

1.2、nginx的应用场景:

  1. 静态页面

  2. 转发动态请求

  3. 反向代理,负载均衡

  4. 缓存服务

1.3、编译过程中代码解释

./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \   ##支持https的加密功能ss/tls
--with-http_v2_module \   ##支持http2.0协议--with-http_realip_module \  ##支持nginx获取客户端的真实ip地址
--with-http_stub_status_module \  ##支持nginx获取访问状态信息的功能
--with-http_gzip_static_module \  ##支持页面压缩功能
--with-pcre \                   ##支持prce库
--with-stream \             ##支持4层代理的模块 
--with-stream_ssl_module \   ##支持对tcp连接的加密
--with-stream_realip_module   ##支持从代理协议中获取客户端的真实ip地址

make -j4 && make install #启动四个cpu进行

conf 配置文件目录

html 工作目录 50x.html默认的访问操作打开的页面

index.html默认的访问主页

logs 日志目录 访问和报错日志

sbin nginx的二进制启动脚本

ln -s /usr/local/nginx/sbin/nginx /usr/sbin/ #让系统识别nginx的二进制脚本

1.3.1、nginx的常用命令

nginx -t 检测nginx.conf配置文件的语法是否正确

nginx -v 显示nginx的版本

nginx -V显示版本和配置项

nignx -s 信号 stop 关闭nginx

reload 重新加载nginx,如果更改了配置文件,nginx -s reload 无需重新启动服务。

systemctl daemon-reload

二、课后重新编译安装nginx

rpm -q nginx        ##检查yum是否安装yum -y remove nginx ##有则卸载cd /opt         rz -E
rz waiting to receive.systemctl stop firewalld       ##关闭安全机制
setenforce 0yum -y install gcc pcre-devel openssl-devel zlib-devel openssl  openssl-devel  ##配置编译环境useradd -M -s /sbin/nologin nginx        ##添加程序用户tar -xf nginx-1.22.0.tar.gz       ##源码解压cd /nginx-1.22.0/         ## 进入nginx的文件夹,配置暗转路径,以及相关组件./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_modulemake -j 4 && make install  ##编译和安装[root@test1 nginx-1.22.0]# cd /usr/local/
[root@test1 local]# ls
bin  etc  games  include  lib  lib64  libexec  nginx  sbin  share  srcchown -R nginx.nginx nginx/    ##更改权限[root@test1 local]# cd nginx
[root@test1 nginx]# ll
总用量 4
drwxr-xr-x. 2 nginx nginx 4096 7月   1 16:04 conf
drwxr-xr-x. 2 nginx nginx   40 7月   1 16:04 html
drwxr-xr-x. 2 nginx nginx    6 7月   1 16:04 logs
drwxr-xr-x. 2 nginx nginx   19 7月   1 16:04 sbin[root@test1 nginx]# cd conf/
[root@test1 conf]# ls
fastcgi.conf            koi-utf             nginx.conf           uwsgi_params
fastcgi.conf.default    koi-win             nginx.conf.default   uwsgi_params.default
fastcgi_params          mime.types          scgi_params          win-utf
fastcgi_params.default  mime.types.default  scgi_params.default
[root@test1 conf]# cd ..
[root@test1 nginx]# ls
conf  html  logs  sbin
[root@test1 nginx]# cd html/
[root@test1 html]# ls
50x.html  index.htmlln -s /usr/local/nginx/sbin/nginx /usr/sbin/  ##做一个软连接,让系统能够识别nginx的指令[root@test1 opt]# 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 successfulvim /lib/systemd/system/nginx.service      ##设置系统控制,系统能对nginx这个软件的运行状态进行控制[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/run/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.targetcd /usr/local/nginx/mkdir run            ##修改nginx的配置文件,把进程文件pid文件的位置指向到设置的位置drwx------. 2 nginx root     6 7月   1 16:14 client_body_temp
drwxr-xr-x. 2 nginx nginx 4096 7月   1 16:04 conf
drwx------. 2 nginx root     6 7月   1 16:14 fastcgi_temp
drwxr-xr-x. 2 nginx nginx   40 7月   1 16:04 html
drwxr-xr-x. 2 nginx nginx   58 7月   1 16:14 logs
drwx------. 2 nginx root     6 7月   1 16:14 proxy_temp
drwxr-xr-x. 2 root  root     6 7月   1 16:24 run
drwxr-xr-x. 2 nginx nginx   19 7月   1 16:04 sbinchown nginx.nginx runcd conf/vim nginx.confpid /usr/local/nginx/run/nginx.pid;nginx -tsystemctl daemon-reloadsystemctl restart nginx

面试题:如果出现了500,怎么来排查这个错误?

1、查看日志

2、查看内部服务器的服务是否启动。

3、查看防火墙的规则配置,是否有限制。

4、查看硬件是否负载过高。

三、nginx.conf

3.1、nginx.conf

1、全局模块

worker_processes 1;--------工作进程数,设置成服务器内核数的2倍(一般不超过8个,超过8个反正会降低性能,4个,1-2个)

#user nobody;------#默认的程序用户就是nginx,这里可以保持注释无需修改

pid /usr/local/nginx/run/nginx.pid;----------#pid文件的位置;

events { worker_connections 1024;}----------#events模块,决定了ngin能够处理的连接数,连接数和worker_connections的数值相乘—1*1024。

处理进程的过程必然涉及配置文件和展示页面,也就是涉及打开文件的数量

linux默认打开的文件就是1024个

vim /etc/security/limits.conf ##进程数量更改

限制先改,进程数量才能生效,默认1024。

*soft nproc 65535----#能打开的进程最大数的软限制是65535

*hard nproc 65535----#能打开的进程最大数的硬限制是65535

*soft nofile 65535-------#软限制进程打开文件数的最大值65535

*hard nofile 65535----------#硬限制进程打开文件数的最大值65535

配置要生效,只能重启,这是系统初始化的一个环节。

在这里插入图片描述

http {
include mime.types;
default_type application/octet-stream;

#http转发和处理http请求,设置代理(正向代理,反向代理),缓存。定义日志格式,重定向配置

include mime.types;------#文件扩展名于文件类型的映射表。nginx能够打开的文件和支持的文件类型。

default_type application/octet-stream;--------------#默认支持的文件类型 。.html .htm .jsp .js .php

 #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,访问日志的格式,error.log也是一样的格式

#access_log logs/access.log main;-------##默认的访问日志的存放路径

sendfile on;—#支持文件发送和下载

#tcp_nopush on;–#默认就是异步非阻塞模式功能

#keepalive_timeout 0;
keepalive_timeout 65;–#连接保持时间,单位为秒/s。

#gzip on;----#gzip模块,设置是否开启页面压缩功能。

##开启web服务的模块

 server {listen       80;#nginx的默认监听端口server_name  localhost;#配置站点的域名#charset koi8-r;#网页的字符集#access_log  logs/host.access.log  main;
#网页匹配的工作目录的地址和支持的打开页面的文件类型。location /xy102(URI) {root   html(/opt/test1)(URL);
#192.168.168.10/opt/test1/xy102/index.html
#家目录。nginx工作目录的家目录,/usr/local/nginx/html
index  index.html index.htm;}

错误日志位置

tail -f /usr/local/nginx/logs/error.log

在这里插入图片描述

在这里插入图片描述

访问是缓存,清除缓存

在这里插入图片描述

#alisa也是指匹配nginx的工作目录

root 换成 alisa

在这里插入图片描述

在这里插入图片描述

alisa是绝对路径。URL/URI

3.2、root和alias的区别:

root的匹配模式 :拼接

root的工作目录,访问的是uri /xy102

location /xy102;

/opt/test1/

拼接为/opt/test1/xy102

alias匹配nginx的工作目录,路径是绝对路径

location /xy102

alias /opt/test1/xy102/;

alias只能写在http模块当中server模块的location模块里面。

root 可以写在server模块,也可以在http,也可以在location中。

alias匹配工作目录,不能使用重定向功能。

3.3、全局模块

work_processes 1:指定进程数

events:模块决定了能够处理的连接数

stream:四层代理模块

http模块

转发和处理http请求,设置代理(正向代理,反向代理),缓存。定义日志格式,重定向配置

在http模块当中,包含:

server块 http里面可以有多个server模块

在server模块当中包含:

location模块

在server当中可以有多个location。

在这里插入图片描述

实验1:#统计访问状态

Active connections:1 #当前活动的连接数

server accepts handled requests #表示已经处理的连接数

36 36 36 #三个数,从左往右,已经处理的连接数;成功建立连接的次数;已经处理的请求数

Reading:0 writing:1 Waiting:0

Reading:表示服务端正在从客户端读取请求的数据

writing:表示服务端正在把响应数据发送给客户端

Waiting:表示有连接处于空闲状态,等待新的请求。

server里写入统计访问状态

  location /status {stub_status on;#打开状态统计功能access_log off;#关闭status的访问日志;}

访问192.168.168.10/status

结果如下:

在这里插入图片描述

实验2:基于密码的授权进行访问控制

yum -y install httpd-tools #先安装工具

location / 根目录加密

vim nginx.conf

    #access_log  logs/host.access.log  main;location / {root   html;index  index.html index.htm;auth_basic "secret";#开启用户密码验证auth_basic_user_file /usr/local/nginx/passwd.db;#使用指定的加密文件}yum -y install httpd-toolshtpasswd -c /usr/local/nginx/passwd.db dn
[root@test1 nginx]# chown nginx passwd.db 
[root@test1 nginx]# chmod 400 passwd.db

在这里插入图片描述

需要输入设置的用户名和对应设置的密码进行访问

实验3:基于客户端的访问控制 ip地址来进行控制

#添加控制规则

vim /usr/local/nginx/conf/nginx.confdeny 192.168.168.20;allow all;

用192.168.168.20测试结果:

在这里插入图片描述

实验4:基于域名的nginx主机

vim nginx.conf

server {listen       80;server_name  www.xy102.com;charset utf-8;access_log  logs/www.xy102.com.access.log;#access_log  logs/host.access.log  main;location / {root   /var/www/html/xy102;index  index.html index.htm;}#新增加域名访问server {listen       80;server_name  www.ly.com;charset utf-8;access_log  logs/www.ly.com.access.log;#access_log  logs/host.access.log  main;location / {root   /var/www/html/ly;index  index.html index.htm;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}
vim /etc/hosts----本地映射192.168.168.10 www.xy102.commkdir -p /var/www/html/xy102echo "我们都爱学习" > /var/www/html/xy102/index.htmlcurl www.xy102.com
我们都爱学习192.168.168.10 www.xy102.com www.ly.com
#域名可以用同一个IP地址mkdir -p /var/www/html/lyecho "我真帅" > /var/www/html/ly/index.htmlcurl www.ly.com
我真帅

实验5:基于ip地址的虚拟主机

ifconfig ens33:0 192.168.168.100/24
[root@test1 conf]# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 192.168.168.10  netmask 255.255.255.0  broadcast 192.168.168.255inet6 fe80::20c:29ff:fe1a:7434  prefixlen 64  scopeid 0x20<link>ether 00:0c:29:1a:74:34  txqueuelen 1000  (Ethernet)RX packets 10190  bytes 895390 (874.4 KiB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 6728  bytes 886953 (866.1 KiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 192.168.168.100  netmask 255.255.255.0  broadcast 192.168.168.255ether 00:0c:29:1a:74:34  txqueuelen 1000  (Ethernet)

多个ip地址:

 server {listen      192.168.168.10:80;server_name  www.xy102.com;charset utf-8;access_log  logs/www.xy102.com.access.log;location / {root   /var/www/html/xy102;index  index.html index.htm;}location /xy103 {alias /opt/test1/xy103;index index.html;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}server {listen      192.168.168.100:80;server_name  www.ly.com;charset utf-8;access_log  logs/www.ly.com.access.log;#access_log  logs/host.access.log  main;location / {root   /var/www/html/ly;index  index.html index.htm;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}

测试结果

在这里插入图片描述

实验6:基于端口实现多个虚拟主机

ifconfig ens33:0 192.168.168.100/24server {listen      192.168.168.10:80;server_name  www.xy102.com;charset utf-8;access_log  logs/www.xy102.com.access.log;location / {root   /var/www/html/xy102;index  index.html index.htm;}location /xy103 {alias /opt/test1/xy103;index index.html;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}server {listen      192.168.168.100:80;server_name  www.ly.com;charset utf-8;access_log  logs/www.ly.com.access.log;#access_log  logs/host.access.log  main;location / {root   /var/www/html/ly;index  index.html index.htm;}​        error_page   500 502 503 504  /50x.html;
location = /50x.html {
​    root   html;
}}

在这里插入图片描述

[root@test1 nginx]# cd logs
[root@test1 logs]# ls
access.log  error.log  www.ly.com.access.log  www.xy102.com.access.log

在这里插入图片描述

实验7:多个配置文件

vim nginx.confinclude /usr/local/nginx/conf.d/*.conf--------##可以识别到conf.d下,只包含server模块的conf文件。把server单独放到一个文件当中去。不在nginx.conf文件当中。
cd /usr/local/nginxmkdir conf.dvim test1.conf

在这里插入图片描述

server {listen      8881;server_name  localhost;charset utf-8;location /test1 {root   /opt/conf;index  index.html index.htm;}
}
server {listen      8882;server_name  localhost;charset utf-8;location /test2 {root   /opt/conf;index  index.html index.htm;}}

mkdir -p /opt/conf/test1

[root@test1 conf.d]# 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@test1 conf.d]# mkdir -p /opt/conf/test1
[root@test1 conf.d]# echo "wozhenshuai" > /opt/conf/test1/index.html
[root@test1 conf.d]# 
[root@test1 conf.d]# mkdir -p /opt/conf/test2
[root@test1 conf.d]# echo "wozhenchou" > /opt/conf/test2/index.html
[root@test1 conf.d]# netstat -antp | grep nginx
tcp        0      0 192.168.168.10:8080     0.0.0.0:*               LISTEN      15027/nginx: master 
tcp        0      0 192.168.168.10:8081     0.0.0.0:*               LISTEN      15027/nginx: master 

在这里插入图片描述

实验8:nginx优化与防盗链:

[root@test1 conf.d]# curl -I 192.168.168.10:8881/test1/
HTTP/1.1 200 OK
Server: nginx/1.22.0
Date: Tue, 02 Jul 2024 08:27:16 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 12
Last-Modified: Tue, 02 Jul 2024 08:02:36 GMT
Connection: keep-alive
ETag: "6683b41c-c"
Accept-Ranges: bytes
[root@test1 conf]# vim nginx.confserver_tokens off;[root@test1 conf]# 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@test1 conf]# systemctl restart nginx
[root@test1 conf]# curl -I 192.168.168.10:8881/test1/
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 02 Jul 2024 08:31:42 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 12
Last-Modified: Tue, 02 Jul 2024 08:02:36 GMT
Connection: keep-alive
ETag: "6683b41c-c"
Accept-Ranges: bytes

在这里插入图片描述

隐藏版本号:在/usr/local/nginx/conf/nginx.conf添加server_tokens off;

基于第八个实验拓展

 cd /opt/
[root@test1 opt]# ls
aa  conf  jenkins-2.396-1.1.noarch.rpm  nginx-1.22.0  nginx-1.22.0.tar.gz  test
[root@test1 opt]# cd nginx-1.22.0/
[root@test1 nginx-1.22.0]# ls
auto     CHANGES.ru  configure  html     Makefile  objs    src
CHANGES  conf        contrib    LICENSE  man       README
[root@test1 nginx-1.22.0]# cd src/
[root@test1 src]# ls
core  event  http  mail  misc  os  stream
[root@test1 src]# cd core/
[root@test1 core]# ls[root@test1 core]# vim nginx.h#define NGINX_VERSION      "wozhenshuai"
#define NGINX_VER          "nibiecai/" NGINX_VERSION[root@test1 core]# cd ..
[root@test1 src]# cd ..
[root@test1 nginx-1.22.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module[root@test1 nginx-1.22.0]# make -j 4 && make installcd /usr/local/nginx/
[root@test1 nginx]# cd conf
[root@test1 conf]# vim nginx.confserver_tokens on;[root@test1 conf]# 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@test1 conf]# systemctl restart nginx[root@test1 conf]# curl -I 192.168.168.10:8881/test1/
HTTP/1.1 200 OK
Server: nibiecai/wozhenshuai
Date: Tue, 02 Jul 2024 08:49:31 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 12
Last-Modified: Tue, 02 Jul 2024 08:02:36 GMT
Connection: keep-alive
ETag: "6683b41c-c"
Accept-Ranges: bytes

在这里插入图片描述

http://www.lryc.cn/news/389271.html

相关文章:

  • 高薪程序员必修课-java为什么要用并发编程
  • postgreSQL学习
  • 【3】系统标定
  • 网安小贴士(3)网安协议
  • 大数据面试题之HBase(1)
  • git回退commit的方式
  • [Information Sciences 2023]用于假新闻检测的相似性感知多模态提示学习
  • 自定义vue3 hooks
  • 《昇思25天学习打卡营第21天 | 昇思MindSporePix2Pix实现图像转换》
  • 【文档+源码+调试讲解】科研经费管理系统
  • linux 下 rm 为什么要这么写?
  • 【Spring Boot】Spring AOP中的环绕通知
  • docker部署前端,配置域名和ssl
  • 初学Spring之 IOC 控制反转
  • rpc的仅有通信的功能,在网断的情况下,比网通情况下,内存增长会是什么原因
  • 从零开始:如何设计一个现代化聊天系统
  • 香橙派OrangePi AIpro初体验:当小白拿到一块开发板第一时间会做什么?
  • 【C语言内存函数】
  • Mysql部署MHA高可用
  • 【算法学习】射线法判断点在多边形内外(C#)以及确定内外两点连线与边界的交点
  • SQL语句(DML)
  • uniapp小程序打开地图导航
  • webstorm格式化或保存时 vue3引入的组件被删除了
  • Java时间转换
  • Spring Boot与WebFlux的实战案例
  • vue3引入本地静态资源图片
  • git 禁止dev合并到任何其他分支
  • 第二节:如何使用thymeleaf渲染html(自学Spring boot 3.x的第一天)
  • 计算机相关术语科普之什么叫网关(Gateway)
  • B站网页部分API