nginx.conf配置参数解析

nginx配置文件解析

/usr/local/nginx/conf

vim  /etc/security/limits.conf
#配置生效只能重新启动

*  soft   nproc   65535   
#能打开的进程最大数是软限制655335,65535是最大值
*  hard  nproc  65535 
*  soft  nofile  65535
# 进程打开文件数的最大值65535 
* hard nofile 65535 

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

[root@test2 conf]#vim nginx.conf
1、全局模块
worker_processes  1;
工作进程数,设置为服务器内核数的2倍(一般不超过8个,超过8个会降低性能 一般为4个 1-2个)

events {
    worker_connections  1024;
}
#events模块,决定了nginx能够处理的连接数,连接数和worker_processes的数值相乘

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

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

    default_type  application/octet-stream;
   #默认支持的文件类型 .html .htm  .jps  .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  65;
     #连接保持的时间  65秒

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

#server模块的作用:开启外部服务的模块
    server {
        listen       80;
        #nginx的默认监听端口
        server_name  localhost;
        #配置站点的域名
        #charset koi8-r;
        #网页默认字符集
        
        #location是网页匹配的工作目录地址和支持打开页面的文件类型
        location / {
            root   html;
            #家目录  nginx默认工作目录的家目录  /usr/local/nginx/html
            #alias 也是指匹配nginx的工作目录
            #root是拼接,工作目录与访问路径拼接  alisa是绝对路径
            index  index.html index.htm;
        }
#统计访问模块
        #  location /status {
       #   sdut_stattus on;
         # 打开计数服务
       #   access_log off;
        #关闭access_log日志
       # }
        

[root@test2 conf]# #基于密码的授权进行访问控制      
[root@test2 conf]# yum -y install  httpd-tools
[root@test2 conf]# htpasswd -c /usr/local/nginx/passwd.db flq
New password: 
Re-type new password: 
Adding password for user flq
[root@test2 nginx]# chown nginx passwd.db 
[root@test2 nginx]# chmod 400 passwd.db 
[root@test2 nginx]# pwd
/usr/local/nginx
[root@test2 conf]# pwd
/usr/local/nginx/conf
[root@test2 conf]# vim nginx.conf    
 location / {
            root   html;
            index  index.html index.htm;
            auth_basic "secret";
            #开启用户密码验证
            auth_basic_user_file /usr/local/nginx/passwd.db;
            #使用指定的加密文件
        }
[root@test2 conf]# #基于客户端的访问控制
[root@test2 conf]# vim nginx.conf
location / {
            root   html;
            #添加控制规则
            deny  192.168.11.138/192.168.11.139;
            # deny  192.168.11.0/24;
            allow all;
        }
[root@test2 conf]# #基于域名的nginx主机
[root@test2 conf]# vim nginx.conf
 server {
        listen       80;
        #nginx的默认监听端口
        server_name  www.xy102.com;
     #配置站点的域名
        charset utf-8;
        access_log logs/www.xy102.com.access.log;
     
     #新增一个域名访问
      server {
             listen       80;
             server_name www.flq.com;
             charset utf-8;
             access_log logs/www.flq.com.access.log;
             location / {
               root /var/www/html/flq;
               index index.html;
             }
              error_page   500 502 503 504  /50x.html;
              location = /50x.html {
              root   html;
          }
         }
         
[root@test2 conf]# vim /etc/hosts   #给ip地址映射域名 
192.168.11.137  www.xy102.com www.flq.com

[root@test2 conf]# mkdir -p /var/www/html/flq
[root@test2 conf]# echo "牛啊" > /var/www/html/flq/index.html
[root@test2 conf]# curl www.xy102.com   # 测试
     nginx原本你的页面
[root@test2 conf]# curl www.flq.com
牛啊
[root@test2 conf]# #基于IP地址的访问
[root@test2 conf]# ifconfig ens33:0 192.168.11.199/24    #新增新的虚拟网卡
[root@test2 conf]# vim nginx.conf

#给每个服务设置其对应的ip地址
server {
        listen       192.168.11.137:80;
        server_name  www.xy102.com;
        charset utf-8;
        access_log logs/www.xy102.com.access.log;
        location / {
            root   html;
           index  index.html index.htm;
        }

     server {
             listen   192.168.11.199:80;
             server_name www.flq.com;
             charset utf-8;
             access_log logs/www.flq.com.access.log;
             location / {
               root /var/www/html/flq;
               index index.html;
             }
              error_page   500 502 503 504  /50x.html;
              location = /50x.html {
              root   html;
          }
         }
    
[root@test2 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@test2 conf]# systemctl restart nginx    #重启服务
#测试
[root@test2 conf]# curl 192.168.11.199:80
牛啊
[root@test2 conf]# curl 192.168.11.137:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width:
[root@test2 conf]#  #基于端口实现多个虚拟主机
[root@test2 conf]# vim nginx.conf
#给每个服务设置其对应的端口
server {
        listen       192.168.11.137:80;
        server_name  www.xy102.com;
        charset utf-8;
        access_log logs/www.xy102.com.access.log;
        location / {
            root   html;
           index  index.html index.htm;
        }

     server {
             listen   192.168.11.199:80;
             server_name www.flq.com;
             charset utf-8;
             access_log logs/www.flq.com.access.log;
             location / {
               root /var/www/html/flq;
               index index.html;
             }
              error_page   500 502 503 504  /50x.html;
              location = /50x.html {
              root   html;
          }
         }
[root@test2 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@test2 conf]# systemctl restart nginx   #重启服务
[root@test2 conf]# curl 192.168.11.199:6666
牛啊
[root@test2 conf]# curl 192.168.11.137:8888
nginx默认HTML页面
配置多个虚拟主机域名访问
[root@test2 conf]# vim nginx.conf
http {
    include       mime.types;
    include    /usr/local/nginx/conf.d/*.conf; 
# 可以识别到conf.d下,只包含server模块的conf的文件
    default_type  application/octet-stream;
     server_tokens off;
#    关闭版本号
    sendfile        on;
    #支持文件发送和文件下载
     keepalive_timeout  65;
     #连接保持的时间
}

root的匹配模式 拼接
root的工作目录 访问的url /xy102
location /xy102
/opt/test1
/opt/test1/xy102/
root可以卸载server模块中,也可以在http,也可以在location中

alias匹配的nginx的工作目录,路径是绝对路径
location /xy102
alias /opt/test1/xy102/;
alias 只能写在http模块中server模块中的location模块里面。

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

总结:
1、全局模块
worker_processes  1;   指定进程数
evens 模块决定能够处理的连接数
stream 四层代理模块
http模块
转发和处理http请求,设置代理(正向代理,反向代理),缓存,定义日志格式,
重定向配置,
在http模块当中,包含:
server块 http里面可以有多个server模块
在server模块当中包含:
location模块
在server当中可以有多个location

在这里插入图片描述

Active connections
当前活动的连接数
server accepts handled requests:表示已经处理的连接数
三个数字,从左往右:已经处理的连接数,成功的建立连接的次数已经处理的请求数
Reading: 0 Writing: 1 Waiting: 0
reading 表示服务端正在从客户端读取请求的数据
writing 表示服务端正在把响应数据发送给客户端
waiting  表示有连接处于空闲状态,等待新的请求

nginx优化和防盗链

[root@test2 conf]# #设置页面缓存时间,主要针对动态页面,图片缓存时间

apache是自带日志分割的,按天来进行收集日志  access.log   error-2024'
date -d "-1 day" "+%Y%m%d"

[root@test2 conf]# vim nginx.conf

#location ~ \.(gif|jpg|png)${
           #root html;
           #设置图片缓存时间
           #expires 1d;
#}
[root@test2 logs]# #更改进程数已经CPU绑定
worker_processes  2;
#表示进程有2个,这里和CPU数挂钩,不绑定CPU的花,进程可能会在两个CPU之间来回切换,资
源浪费
worker_cpu_affinity 0001 0010  0100  1000;
#绑定CPU

keepalive_timeout  65;
    #请求完成之后的连接保持的时间
client_header_timeout 80;
#客户端发送一个完整的请求头的超时时间,80秒之内没有发送一个完整的请求头,nginx返回>码408(request time out)
client_body_timeout 80;
    #客户端和服务端建立连接之后,发送请求体的超时时间,客户端在80秒内没有发送任何内容,nginx返回>码408
 gzip  on;
     #配置页面压缩
     #gzip模块,设置是否开启页面压缩
    gzip_min_length 1k;
    #最小的压缩文件,效益等于1k的文件就不压缩了
    gzip_buffers 4 64k;
    ##设置压缩的缓冲区,4个,每个缓冲区的大小64K
    gzip_comp_level 6;
    #压缩比例1-9,数字越小,压缩比例越小,速度越快,数字越大,压缩比例就越高,速度越慢


回收TIME_WAIT:
TIME_WAIT(不是报错,四次挥手之后会进入TIME_WAIT状态)
TIME_WAIT是tcp连接当中的一种状态,出现在四次挥手之后
处于等待状态,双方不再发送数据
time_wait所含的系统资源很小,数量比较少,完全可以忽略不计
但是太多了,就会一定的影响
连接断开(四次挥手)之后,尽快的把time_wait状态的连接进行回收

netstat -n | awk '/^tcp/' {++s[$NF]} END {for (a in s)print a s[a]}
统计当前系统连接状态
 
总结:
隐藏版本号
日志分割
CPU绑定
连接超时
页面压缩
页面缓存时间
time_wait状态回收*
[root@test2 conf]#vim /etc/sysctl.conf           

net.ipv4.tcp_synvookies=1
#防止tcp的半连接队列溢出,可以达到服务端在收到tcp的syn(同步)的请求时能够快速响应
net.ipv4.tcp_tw_reuse=1
#允许复用time——wait状态的连接,新的连接可以直接使用time_wait的状态的端口,可以提高连接的重用率
net.ipv4.tcp_tw_recycle=1
#这个是老版本的配置,时间戳的戳记进行连接复用
net.ipv4.tcp_fin_timeout=30
##控制time_wait状态的持续时间,持续30秒,不是立即把time_wait的连接收回,而是尽可能的把time_wait状态的进行回收,没用的,空闲的,进行回收

[root@test2 conf]# sysctl -p  #立即生效

防盗链

[root@test2 conf]# vim nginx.conf
location ~* \.(gif|jpg)$ {
    #当识别的图片为png时不需要加入到判断中
          valid_referers none blocked *.xy102.com xy102.com;
        #允许xy102.com的网址访问图片,
        if ( $invalid_referer ) {
         
            rewrite ^/ http://www.xy102.com/error.png;
            #如果不是xy102访问,一律跳转到盗链的提示。
           }
    }
[root@test2 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@test2 conf]# systemctl restart nginx
[root@test2 conf]# cd ..
[root@test2 nginx]# cd html/
[root@test2 html]# pwd
/usr/local/nginx/html
[root@test2 html]# vim index.html 
[root@test2 html]# echo 192.168.11.137 www.xy102.com >> /etc/hosts
[root@test2 html]# echo 192.168.11.138 www.xy103.com >> /etc/hosts
[root@test2 html]# systemctl restart nginx
[root@test2 html]# vim index.html 
<img src="111.png">
</body>
</html>
~           

nginx
[root@test2 conf]# cd …
[root@test2 nginx]# cd html/
[root@test2 html]# pwd
/usr/local/nginx/html
[root@test2 html]# vim index.html
[root@test2 html]# echo 192.168.11.137 www.xy102.com >> /etc/hosts
[root@test2 html]# echo 192.168.11.138 www.xy103.com >> /etc/hosts
[root@test2 html]# systemctl restart nginx
[root@test2 html]# vim index.html

~ ```

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/770853.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

最新美联储会议纪要:通胀降温,但不急于降息!

KlipC报道&#xff1a;当地时间周三&#xff0c;美联储公布了6月货币政策会议纪要。纪要显示&#xff0c;数据表明有通胀放缓的迹象&#xff0c;但如果降息需要更多的证据。此外&#xff0c;多位与会者表示&#xff0c;货币政策应随时准备应对意外的经济疲软。 会议纪要显示&a…

python-字典

为什么需要字典 字典的定义 字典数据的获取 字典的嵌套 嵌套字典的内容获取 字典的注意事项&#xff1a; 字典的常用操作 新增元素 更新元素 删除元素 清空字典 汇总 字典的特点

收银系统源码-收银台营销功能-定时折扣

1. 功能描述 定时折扣&#xff1a;在特定的时间段&#xff0c;将商品以打折的方式在收银台售卖&#xff0c;例如生鲜行业&#xff0c;由于生鲜是易耗品&#xff0c;很多门店晚上都会通过打折的方式进行促销&#xff1b; 2.适用场景 新门店开业、门店周年庆、节假日等特定时间…

深度分析和对比本地大语言模型Ollama和LocalAI

前言 在充满活力的人工智能&#xff08;AI&#xff09;世界中&#xff0c;开源工具已成为开发人员和组织利用LLM&#xff08;大型语言模型&#xff09;力量的重要资源。这些工具通过提供对高级LLM模型的访问权限&#xff0c;使各种用户能够构建创新和前沿的解决方案。在众多可…

springboot @configuration注解的配置, @bean注解方法a, 在@bean注解 getb(){}需要注入a

深度解析Configuration注解 /*** General purpose AOP callback. Used when the target is dynamic or when the* proxy is not frozen.*/private static class DynamicAdvisedInterceptor implements MethodInterceptor, Serializable {private final AdvisedSupport advised;…

实验九 存储过程和触发器

题目 创建并执行一个无参数的存储过程proc_product1&#xff0c;通过该存储过程可以查询商品类别名称为“笔记本电脑”的商品的详细信息&#xff1a;包括商品编号、商品名称、品牌、库存量、单价和上架时间信息 2、创建并执行一个带输入参数的存储过程proc_product2&#xff…

Rethinking Federated Learning with Domain Shift: A Prototype View

CVPR2023,针对分布式数据来自不同的域时,私有模型在其他域上表现出退化性能(具有域转移)的问题。提出用于域转移下联邦学习的联邦原型学习(FPL)。核心思想是构建集群原型和无偏原型,提供富有成效的领域知识和公平的收敛目标。将样本嵌入拉近到属于相同语义的集群原型,而…

【前端】IntersectionObserver 实现图片懒加载和无限滚动

【前端】IntersectionObserver 实现图片懒加载和无限滚动 在前端开发中&#xff0c;性能优化是一个重要的考量因素。随着现代网页和应用的复杂性增加&#xff0c;确保页面快速加载和流畅运行变得越来越重要。本文将介绍一种强大的工具——IntersectionObserver API&#xff0c…

智胜未来:AI如何重塑SaaS用户增长战略

在当今这个数字化时代&#xff0c;SaaS&#xff08;软件即服务&#xff09;已成为企业运营的重要支柱&#xff0c;而人工智能&#xff08;AI&#xff09;技术的迅猛发展&#xff0c;正以前所未有的方式重塑着SaaS行业的面貌&#xff0c;特别是对其用户增长战略产生了深远影响。…

每日一题——Python实现PAT乙级1005 继续(3n+1)猜想(举一反三+思想解读+逐步优化)五千字好文

一个认为一切根源都是“自己不够强”的INTJ 个人主页&#xff1a;用哲学编程-CSDN博客专栏&#xff1a;每日一题——举一反三Python编程学习Python内置函数 Python-3.12.0文档解读 目录 我的写法 代码逻辑概述 时间复杂度分析 空间复杂度分析 总结 我要更强 代码优化点…

Openwrt路由器部分ipv6公网地址无法访问的问题

路由器是Openwrt&#xff0c;终端访问ipv6地址经常有些能访问&#xff0c;有些不能访问&#xff0c;一开始以为是运营商问题&#xff0c;后面ssh到openwrt发现所有访问都正常。 查阅资料后才知道是MTU设置问题&#xff0c;Openwrt 默认MTU是1492&#xff0c;使用IPV6应减少60个…

Word文档中公式的常用操作

一、参考资料 二、常用操作 插入公式 Alt 多行公式 Shift Enter 多行公式对齐 WORD Tips: 多行公式编辑及对齐 word自带公式等号对齐&#xff08;可任意符号处对齐&#xff09; 多行公式按照 为基准对齐。 拖动鼠标选中整个公式点击右键&#xff0c;选择【对齐点(…

[激光原理与应用-97]:激光焊接焊中检测系统系列介绍 - 1 - 什么是焊接以及传统的焊接方法

目录 一、什么是焊接 1.1 概述 1.2 基本原理 二、传统的焊接技术与方法 2.1 手工电弧焊&#xff1a; 1、定义与原理 2、特点 3、焊条类型 4、应用领域 5、安全注意事项 2.2 气体保护焊&#xff1a; 1、原理与特点 2、应用领域 3、气体选择 4、注意事项 2.3 电阻…

C++ 文达校内党员管理系统-计算机毕业设计源码20855

目 录 摘要 1 绪论 1.1研究背景与意义 1.2国内外研究现状 1.3论文结构与章节安排 2 文达校内党员管理系统系统分析 2.1 可行性分析 2.2 系统功能分析 2.2.1 功能性分析 2.2.2 非功能性分析 2.3 系统用例分析 2.4 系统流程分析 2.4.1 数据流程 2.5.2 业务流程 2.…

CPU/内存/综合性能评估工具汇总-3:unixbench

目录 一、概括二、UnixBench 一、概括 嵌入式开发中对要设计的产品、立项的项目进行设计时&#xff0c;往往需要对关键芯片进行性能评估&#xff0c;本文主要总结基于linux系统的产品在性能评估时的工具使用总结&#xff0c;在aarch64(arm64平台下测试)&#xff0c;板卡根文件…

前端学习(三)CSS介绍及选择符

##最近在忙期末考试&#xff0c;因此前端笔记的梳理并未及时更新。在学习语言过程中&#xff0c;笔记的梳理对于知识的加深very vital.因此坚持在明天学习新知识前将笔记梳理完整。 主要内容&#xff1a;CSS介绍及选择符 最后更新时间&#xff1a;2024/7/4 目录 内容&#x…

Redis 7.x 系列【15】持久化机制之 RDB

有道无术&#xff0c;术尚可求&#xff0c;有术无道&#xff0c;止于术。 本系列Redis 版本 7.2.5 源码地址&#xff1a;https://gitee.com/pearl-organization/study-redis-demo 文章目录 1. 概述2 执行原理3. 配置项3.1 save3.2 stop-writes-on-bgsave-error3.3 rdbcompress…

HMI 的 UI 风格创造奇迹

HMI 的 UI 风格创造奇迹

关于巴图自动化Profinet协议转Modbus协议网关模块怎么配置IP地址教学

Profinet协议和Modbus协议是工业领域中常用的两种通讯协议&#xff0c;除此以外还有较为常见的&#xff1a;ModbusTCP协议&#xff0c;Profibus协议&#xff0c;Profibus DP协议&#xff0c;EtherCAT协议&#xff0c;EtherNET协议&#xff0c;CAN&#xff0c;CANOPEN等它们在自…

利用运放设计简单有源滤波器(低通、高通、带通)

本文旨在帮助刚接触模电的同学快速设计一个实用可靠的有源滤波器&#xff0c;故我将不会说一些晦涩难懂的原理&#xff0c;只给出仿真电路图。 低通滤波器 图1 低通滤波器 图1所示的是一个截止频率约为1KHz的低通滤波器。 图2 200Hz的情况 图3 2KHz的情况 设计步骤为&#x…