Nginx配置

1.Nginx简介

C语言编写,采用服务器实现负载均衡,高性能的HTTP和反向代理web服务器,处理高并发能力是十分强大,最高能支持5w个并发连接数。

2.Nginx 的优点

  • 高并发。静态小文件
  • 占用资源少。2万并发、10个线程,内存消耗几百M。
  • 功能种类比较多。web,cache,proxy。每一个功能都不是特别强。
  • 支持epoll模型,使得nginx可以支持高并发。
  • nginx 配合动态服务和Apache有区别。(FASTCGI 接口)
  • 利用nginx可以对IP限速,可以限制连接数。
  • 配置简单,更灵活。
     

3. Nginx应用场合

  • 静态服务器(图片,视频服务),另个lighttpd。并发几万,html,js,css,flv,jpg,gif等。
  • 动态服务,nginx—fastcgi 方式运行PHP,jsp。(PHP并发约500-1500,MySQL 并发约300-1500)。
  • 反向代理,负载均衡。日pv2000W以下,都可直接用nginx做代理。
  • 缓存服务。类似 SQUID,VARNISH。

4.Nginx Docker构建和指定配置启动

编写dockerfile文件并且构建镜像

#基于ubuntu构建的镜像
from java_ubuntu 

user root

run mv /etc/apt/sources.list /etc/apt/alibaba_sources.list
run mv /etc/apt/ubuntu_sources.list /etc/apt/sources.list
run apt-get update
#nginx install
run apt-get install -y nginx
run mkdir /etc/nginx/html
run mkdir /etc/nginx/logs
copy nginx.conf /etc/nginx/
copy index.html /etc/nginx/html

cmd nginx -c /etc/nginx/nginx.conf && tail -f /dev/null
#验证配置文件
nginx -tc /etc/nginx/conf/nginx_my.conf
nginx -t -c /etc/nginx/conf/nginx_my.conf
 
#启动
nginx -c etc/nginx/conf/nginx_my.conf
#重启
nginx -s reload -c /etc/nginx/conf/nginx_my.conf
#停止
nginx -s stop -c /etc/nginx/conf/nginx_my.conf

5.Nginx配置文件

 nginx.conf主要由三部分组成          

  1. 全局块,
  2. events块
  3. http块

完整的配置文件 

nginx.conf

worker_processes  1;   # 服务器并发处理能力,值越大并发能力越强(受自身配置限制)
events {
    worker_connections  1024; # 每个工作进程连接数
}

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

    # 日志格式
    log_format  access  '$remote_addr - $remote_user [$time_local] $host "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /etc/nginx/logs/access.log  access; # 日志输出目录
    gzip  on;
    sendfile  on;

    # 链接超时时间,自动断开
    keepalive_timeout  60;

    # 虚拟主机,访问静态文件
    server {
        listen       8080;
        server_name  localhost; # 浏览器访问域名

        charset utf-8;
        access_log  /etc/nginx/logs/localhost.access.log  access;

        # 路由
        location / {
            root   /etc/nginx/html; # 访问根目录
            index  index.html index.htm; # 入口文件
        }
        location /html {
            root   /etc/nginx; 
            index  index.html index.htm; 
        }

        location /htmlalias {
            alias  /etc/nginx/html/; 
            index  index.html index.htm; 
        }

        location /returnok {
          return 200 "masker success";
        }
    }

    # 访问其他服务
    # server {
    #     listen       9090;
    #     server_name  localhost; 
 
    #     charset utf-8;
    #     access_log  /etc/nginx/logs/localhost9090.access.log  access;
 
    #     # 路由
    #     location /nginxproducterone {
    #         proxy_pass  http://producter-one:8080/;
    #     }
    # }

    server {
        # 不指定端口默认监听80端口
        # http默认监听80端口
        # https默认监听443端口
        # listen       8080;
        server_name  nginx.test.com; # 浏览器访问域名

        charset utf-8;
        access_log  /etc/nginx/logs/nginxtestcom.log  access;

        location /returnok {
          return 200 "masker success nginx.test.com";
        }
    }

    # 引入其他的配置文件
    #include servers/*;
    include /etc/nginx/conf/server.conf;
}

server.conf

    upstream proxyserver {
        server localhost:18080;
        server localhost:28080;
  }

    server {
        listen  18080;
        server_name  localhost; 

        charset utf-8;
        access_log  /etc/nginx/logs/localhost_18080.log  access;

        location / {
          return 200 "18080 returnok";
        }
    }

    server {
        listen  28080;
        server_name  localhost; 

        charset utf-8;
        access_log  /etc/nginx/logs/localhost_28080.log  access;

        location / {
          return 200 "28080 returnok";
        }
    }


server {
    listen  60000;
    server_name  localhost; # 浏览器访问域名
    
    charset utf-8;
    access_log  /etc/nginx/logs/localhost60000.access.log  access;
    

    location /returntest {
      # 结尾不带/,则用proxy_pass+location,结尾带/,则只使用proxy_pass
        proxy_pass  http://proxyserver/;
    }
}

 5.1 全局块

nginx服务器全局生效的配置命令

worker_processes  1;   # 服务器并发处理能力,值越大并发能力越强(受自身配置限制)

5.2 events块

影响nginx和用户网络的连接

worker_connections 1024; #最大连接数1024个,需灵活配置

5.3 http块

nginx配置最频繁的部分,比如代理,日志,缓存、第三方模块等等。http块可以包括http全局块和server块

http {
    include       mime.types;
    default_type  application/octet-stream;
    gzip  on;
    sendfile  on;
    server{
    }
    
    server {
    }
}

  1 http的全局块

    包括文件引入、MIME-TYPE定义,日志自定义、连接超时等等

http {
    include       mime.types;      #文件扩展名与文件类型映射表
    default_type  application/octet-stream;  # 访问到未定义的扩展名的时候,就默认为下载该文件
}

2 http的server块

与虚拟主机有密切关系,主要是为了节省硬件成本,一个http块可以包含多个server块,而一个server块就等于一个虚拟主机server块又包含全局server块和location块。

       a.server块

 server {
        listen       8080;
        server_name  localhost; # 浏览器访问域名

        charset utf-8;
        access_log  /etc/nginx/logs/localhost.access.log  access;

        location / {
             xxx xxx;
        }
    }

   b.location块

语法规则: location [=|~|~*|^~] /uri/ { … }

  • =         严格匹配。如果请求匹配这个location,那么将停止搜索并立即处理此请求
  • ~         区分大小写匹配(可用正则表达式)
  • ~*       不区分大小写匹配(可用正则表达式)
  • !~       区分大小写不匹配
  • !~*     不区分大小写不匹配
  • ^~      如果把这个前缀用于一个常规字符串,那么告诉nginx 如果路径匹配那么不测试正则表达式
# 路由
location / {
     root   /etc/nginx/html; # 访问/etc/nginx/html/index.html
     index  index.html index.htm; # 入口文件
}

location /html/ {
     root   /etc/nginx; # 访问/etc/nginx/html/other/index.html
     index  index.html index.htm; # 入口文件
}

location /htmlalias/ {
    alias /etc/nginx/html/;
    index  index.html index.htm; 
}

location /producterone/ {
     proxy_pass  http://172.18.0.4:8080;    #转发到其他服务http://172.18.0.4:8080//producterone/say
}

location /returnok/ {  
     return 200 "success";    #返回200和消息
}

#root 实际访问文件路径会拼接URL中的路径
#alias 实际访问文件路径不会拼接URL中的路径
#proxy_pass 表示代理转发

  注意:

producter-one的访问地址http://producter-one:8080/producterone/say

1.location 中的最后有没有 / 都没有影响。/producterone和/producterone/都是可以正常请求的。

2. proxy_pass url结尾的/,如果结尾不带/,则用proxy_pass+location的路径,表示相对路径,;如果结尾带/,则只使用proxy_pass,表示绝对根路径。

 a.proxy_pass结尾带/

访问只使用了proxy_pass

则访问:localhost:9090/producterone/say 访问会提示404

所以需要修改请求地址:localhost:9090/nginx-producterone/producterone/say

a.proxy_pass结尾不带/

访问使用proxy_pass+location,则访问:localhost:9090/producterone/say  正常

6. 负载均衡规则

    在http块中增加upstream 的配置。upstream的三种策略分别是ip_hash,weight权重,轮询(默认),轮询不需要特殊设置,是直接的默认方式。这三种方式优先级是ip_hash > weight > 轮询 。

upstream配置语法 

upstream <名称> {
	[ip_hash策略];
    server <服务信息> [权重信息];
    server <服务信息> [权重信息];
}

 6.1轮询(默认)

        每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除

upstream producteroneserver {
    server producter-one:8080;
    server producter-one2:8080;
}

server {
    listen       9090;
    server_name  localhost; # 浏览器访问域名
    
    charset utf-8;
    access_log  /etc/nginx/logs/localhost9090.access.log  access;
    
    # 路由
    location /producterone {
        proxy_pass  http://producteroneserver;
    }
}

6.2 weight权重

       weight 代表权重默认为 1,通过设置权重值指定集群中不同机器的权重,权重越高,落到该机器的请求次数越多。

upstream producteroneserver {
    server producter-one:8080  weight=2;
    server producter-one2:8080 weight=1;
}

server {
    listen       9090;
    server_name  localhost; # 浏览器访问域名
    
    charset utf-8;
    access_log  /etc/nginx/logs/localhost9090.access.log  access;
    
    # 路由
    location /producterone {
        proxy_pass  http://producteroneserver;
    }
}

6.3 ip_hash

    每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决session问题

upstream producteroneserver {
	ip_hash;
    server producter-one:8080;
    server producter-one2:8080;
}

server {
    listen       9090;
    server_name  localhost; # 浏览器访问域名
    
    charset utf-8;
    access_log  /etc/nginx/logs/localhost9090.access.log  access;
    
    # 路由
    location /producterone {
        proxy_pass  http://producteroneserver;
    }
}

参考:

Nginx配置使用详解_shstart7的博客-CSDN博客_nginx 配置详解

nginx详细参数配置(史上最全) - hanease - 博客园

Nginx配置_giunwr的博客-CSDN博客_nginx 配置

Nginx 配置 location 以及 return、rewrite 和 try_files 指令 - 走看看

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教学资源的高效利用。同时,教学质量评估中心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教学和管理流程,为学生、教师和家长提供了更加便捷、个性化的教育体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值