Nginx安装及使用详细步骤

一、Nginx安装

1、下载地址http://nginx.org/en/download.html

2、安装Nginx

tar -xvf nginx-1.17.8.tar

#安装Nginx依赖,pcre、openssl、gcc、zlib
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

cd nginx-1.17.8
./configure
make
make install

3、安装完成后,会在/usr/local下会产生一个nginx目录

4、启动nginx ,nginx默认监听80端口

5、测试是否启动成功

6、nginx常用命令

①启动:./nginx

②终止:./nginx -s stop

③重新加载nginx.conf文件:./nginx -s reload

7、nginx.conf配置文件

#======================全局块=======================
#运行用户
#user  nobody;
#worker进程数,通常设置为和cpu核数相同 
worker_processes  1;

#全局错误日志及pid文件位置
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;
#====================================================


#=====事件块,影响一个worker允许的连接数==============
events {
    worker_connections  1024;
}
#=====================================================


#======http块=========================================
http {
    # 引入mime类型定义文件
    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压缩
    #gzip  on;
    #负载均衡策略
    upstream myserver{
         server 10.60.131.17:8081;
         server 10.60.131.17:8082;
     }

    server {
        #监听端口
        listen       9090;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main
        #代理配置
        location / {
           proxy_pass http://myserver/;
           #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;
        }
}

 

8、location语法:

①、location =/test {...} 精准匹配,只会匹配/test,而/test/abc则不会匹配成功

②、location ^~ /test {...} 非正则匹配,当匹配到该模式后,将不再继续搜索正则匹配模式

③、location ~* /test {...}不区分大小写正则匹配

④、location ~ /test {...}正则匹配

⑤、location /test 普通匹配

优先级 ①>②>③>④>⑤

9、upstream负载均衡策略

①、轮询策略(默认)

upstream myserver {
   server 192.168.16.1:8081;
   server 192.168.16.2:8081;
}

②、ip_hash策略

upstream myserver {
   ip_hash;
   server 192.168.16.1:8081;
   server 192.168.16.2:8081;
}

③、权重

upstream myserver {
   server 192.168.16.1:8081 weight=1;
   server 192.168.16.2:8081 weight=2;
}

④、一致性hash负载均衡策略

upstream myserver {
   consistent_hash $remote_addr;
   server 192.168.16.1:8081;
   server 192.168.16.2:8081;
}

说明:ngx_http_upstream_consistent_hash 模块是⼀个负载均衡器,使⽤⼀个内部⼀致性hash算法来选择 合适的后端节点。 该模块可以根据配置参数采取不同的⽅式将请求均匀映射到后端机器,

consistent_hash $remote_addr:可以根据客户端ip映射

consistent_hash $request_uri:根据客户端请求的uri映射

consistent_hash $args:根据客户端携带的参数进⾏映

ngx_http_upstream_consistent_hash 模块是⼀个第三⽅模块,需要我们下载安装后使⽤

a、github下载nginx⼀致性hash负载均衡模块 https://github.com/replay/ngx_http_consistent_hash

b、将下载好的压缩包上传的nginx服务器,并解压

c、在nginx目录下,执行以下命令

./configure —add-module=/root/ngx_http_consistent_hash-master
make
make install

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值