nginx

简介: nginx

# 概念


  • 是什么,可以做什么
  • 反向代理
  • 负载均衡
  • 动静分离


# 安装使用,配置



  • linux中安装
  • 常用命令
  • 查看nginx版本号 ./nginx -v
  • 关闭: ./nginx -s stop
  • 启动: ./nginx
  • 热加载: ./nginx -s reload
  • 检查配置文件:./nginx -t

  • 配置文件
  • 组成部分:
  1. 全局块:
  2. events块:
  3. http块:

# 全局块 --------------------------------------------------------------------------------start
#user  nobody;
# 越大并发数量越多
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
# 全局块 --------------------------------------------------------------------------------end
# events块 ------------------------------------------------------------------------------start
events {
    worker_connections  1024;    # 支持最大的连接数
}
# events块 ------------------------------------------------------------------------------end
# http块 ------------------------------------------------------------------------------start
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;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
        }
        #error_page  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   html;
        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #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;
        #}
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.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;
    #    }
    #}
}
# http块 ------------------------------------------------------------------------------end


  • location


image.png

# 配置实例



  • 反向代理

server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        # 相当于是匹配路径
        location ~ /server1/ {
            proxy_pass http://localhost:9701;
        }
        # ~ 表示是正则
        location ~ /server2/ {
            proxy_pass http://localhost:9702;
        }


  • 负载均衡
  • 分配策略:
  • 轮询(默认), 会自动剔除无效的服务
  • 权重 weight, 默认是1
  • ip_hash,可保证同一个ip(用户)一直访问同一个服务器
  • fair,根据服务响应时间来分配

http {
    # 负载均衡服务
    upstream blance_server{
        # ip_hash的方式分配请求
        #ip_hash;
        # 根据服务响应时间来分配
        # fair
        # 权重的方式分配请求
        server localhost:9701 weight=1;
        server localhost:9702 weight=2;
    }
  server {
        listen       80;
        server_name  localhost;
        location ~ /blance/ {
            proxy_pass http://blance_server;
            proxy_connect_timeout 10;
        }
    }
}


  • 动静分离


提高访问效率


image.png

高可用集群


image.png



# 原理



image.png

相关文章
|
8天前
|
云安全 监控 安全
|
13天前
|
机器学习/深度学习 人工智能 自然语言处理
Z-Image:冲击体验上限的下一代图像生成模型
通义实验室推出全新文生图模型Z-Image,以6B参数实现“快、稳、轻、准”突破。Turbo版本仅需8步亚秒级生成,支持16GB显存设备,中英双语理解与文字渲染尤为出色,真实感和美学表现媲美国际顶尖模型,被誉为“最值得关注的开源生图模型之一”。
1451 8
|
7天前
|
人工智能 安全 前端开发
AgentScope Java v1.0 发布,让 Java 开发者轻松构建企业级 Agentic 应用
AgentScope 重磅发布 Java 版本,拥抱企业开发主流技术栈。
482 11
|
19天前
|
人工智能 Java API
Java 正式进入 Agentic AI 时代:Spring AI Alibaba 1.1 发布背后的技术演进
Spring AI Alibaba 1.1 正式发布,提供极简方式构建企业级AI智能体。基于ReactAgent核心,支持多智能体协作、上下文工程与生产级管控,助力开发者快速打造可靠、可扩展的智能应用。
1259 43
|
19天前
|
人工智能 前端开发 算法
大厂CIO独家分享:AI如何重塑开发者未来十年
在 AI 时代,若你还在紧盯代码量、执着于全栈工程师的招聘,或者仅凭技术贡献率来评判价值,执着于业务提效的比例而忽略产研价值,你很可能已经被所谓的“常识”困住了脚步。
1167 88
大厂CIO独家分享:AI如何重塑开发者未来十年
|
2天前
|
存储 弹性计算 容灾
阿里云服务器ECS自定义购买流程:超详细新手入门教程
本文详细介绍阿里云服务器ECS自定义购买全流程,涵盖付费模式、地域选择、网络配置、实例规格、镜像系统、存储、公网IP、带宽计费及安全组设置等关键步骤,适合新手入门参考,助你轻松完成云服务器选购与部署。
195 121