Nginx(二)配置详解

一、配置文件nginx.conf

启动nginx     /usr/sbin/nginx       或者     nginx -c /etc/nginx/nginx.conf

重启nginx的小错误 /var/run/nginx/nginx.pid” no such file or directory?

解决:到/var/run下看没有nginx这个目录,创建/var/run/nginx,再启动。

          重启虚拟机,这个目录会被删掉。 

配置文件位置?

cd /usr/local/src/nginx-1.14.2/conf

查看配置文件

 cat nginx.conf

#user  nobody;      # 运行用户
worker_processes  1;    # worker进程数,通常设置等同于CPU数量,auto为自动检测

#全局错误日志及PID文件

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;  

#工作模式及连接数上限
events {
    use   epoll;             #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
    worker_connections  1024;   #单个后台worker process进程的最大并发链接数
    # multi_accept on; 
}

#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {

 #设定mime类型,类型由mime.type文件定义
    include       mime.types;
    default_type  application/octet-stream;

#设定日志格式

access_log    /var/log/nginx/access.log;

    #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 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
    #必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.

    sendfile        on;
    #tcp_nopush     on;

    #连接超时时间

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #开启gzip压缩

    #gzip  on;

    server {

       #监听的端口以及ip地址/域名
        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;
        }

        # 代理服务器的地址在这里配置,同时可也以在这里配置动态资源与静态资源分离
        #正则表达式匹配规则。  = 表示精确匹配   ~ 表示区分大小写 ~* 表示不区分大小写 
        #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;
        #}
    }

负载均衡简要配置

    upstream myserver{

        ip_hash || fair ||下面设置weight来配置负载均衡策略,也可以不管,使用默认的轮训策略

        server:ip1 weight=1;

        server:ip2 weight=2;

    }

    server{

      listen       port;
      server_name  ip;

      location / {
           proxy_pass   http://myserver;
           root  index.htm; 
           index  index.htm;
       }
    }

    }
    动静分离简要配置
    server {
        listen       port;
        server_name  ip;

        location /data/ {
            root   /static/;
            index  index.htm;
        }
    }


nginx 分配服务器策略

第一种 轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除。

第二种 weight weight 代表权重默认为 1,权重越高被分配的客户端越多

第三种 ip_hash 每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器

第四种 fair(第三方) 按后端服务器的响应时间来分配请求,响应时间短的优先分配。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值