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

目录
打赏
0
0
0
0
0
分享
相关文章
在秒杀系统中redis的数据和mysql不一致了,要怎么检查出来了(概述)
在秒杀系统中redis的数据和mysql不一致了,要怎么检查出来了(概述)
191 0
文件IO操作的一些练习小案例
文件IO操作的一些练习小案例
173 0
【青训营】写好JS——做好抽象
【青训营】写好JS——做好抽象
165 0
【青训营】写好JS——做好抽象
【青训营】写好JS——各司其责
【青训营】写好JS——各司其责
124 0
【青训营】写好JS——各司其责
【Jetpack】学穿:LiveData → ???(中)
在开始这篇文章前,我就遇到了第一个关于LiveData的问题:该怎么翻译这个词呢?
214 0
Java 中一个你不常用,但是关键时刻可以帮我们提升性能的一个知识点
最近阿粉在实现一个功能的时候,遇到了一个性能问题,一个方法在某些场景下运行时长达到了 4s 多,虽然说业务功能是实现了,但是不管是从业务的角度还是作为一个有追求的程序员,都是不能接受的,所以优化这个方法势在必行。在优化的过程中就用到了本文要说明的一个知识点,看阿粉慢慢道来。
Java 中一个你不常用,但是关键时刻可以帮我们提升性能的一个知识点
PostgreSQL 10.1 手册_部分 III. 服务器管理_第 24 章 日常数据库维护工作_24.3. 日志文件维护
24.3. 日志文件维护 把数据库服务器的日志输出保存在一个地方是个好主意, 而不是仅仅通过/dev/null丢弃它们。 在进行问题诊断的时候,日志输出是非常宝贵的。不过,日志输出可能很庞大(特别是在比较高的调试级别上), 因此你不会希望无休止地保存它们。
1279 0
前端开发常见问题精选(四)
一、Swiper.js的loop模式下,如何正确获取索引值? Swiper.js,相信作为前端开发者的你一定知道它吧。它几乎可以用来制作任何形式的轮播图,非常方便和实用。
1093 0
腾讯高级设计师谈微信的旧容与新妆,Android Design是大势所趋
编者按:本篇投稿选自腾讯大讲堂(更多腾讯产品技术文章,可以关注“腾讯大讲堂”微信公众账号),由腾讯研发管理部高级设计师Vertu撰写,他以产品设计师的视角,对比解读了微信的旧容与新妆,也讲了Android版微信5.2放弃iOS UI设计背后的故事。
1332 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问