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

~ ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码要你命

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值