nginx相关

1.下载:http://nginx.org/en/download.html
下载Stable version(稳定版本)

2.将下载完成的nginx-1.20.2.tar.gz放到linux环境下/home目录

3.将包解压,然后进入到解压后的目录
tar -zxvf nginx-1.20.2.tar.gz

4.安装相关环境
yum -y install gcc gcc-c++ autoconf automake make

5.配置基本信息(在nginx的解压目录下执行)
./configure --prefix=/usr/local/nginx-1.20.2 --with-http_ssl_module --with-http_stub_status_module

(1)报错

<1>./configure: error: the HTTP rewrite module requires the PCRE library.

安装:yum -y install pcre-devel

<2>./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library

yum -y install openssl openssl-devel

执行成功后再次执行

./configure --prefix=/usr/local/nginx-1.20.2 --with-http_ssl_module --with-http_stub_status_module

6.进行编译安装(在nginx解压目录下执行)
make & make install

7.进入到安装目录里/usr/local/nginx-1.20.2
进入到sbin目录,执行启动命令
启动:./nginx
关闭:./nginx -s stop
重新加载:./nginx -s reload

8.验证
访问ip:80

页面显示:Welcome to nginx!
 

Nginx负载均衡配置
vi ../conf/nginx.conf

在http模块添加:三个localhost 是三台机器
    upstream Test.com{
        server localhost:8081;
        server localhost:8081;
        server localhost:8081;
    }
     
     
     
    server {
        #请求nginx的端口
        listen       8090;
        #请求nginx的ip
        server_name  localhost;
        
        #charset koi8-r;
        
        #access_log  logs/host.access.log  main;
        
        location /entry/ {
        # root   html;
            #index  index.html index.htm;
            proxy_pass http://Test.com/Test/entry;
        }
        
        location /busin/ {
        # root   html;
        #index  index.html index.htm;
        proxy_pass http://Test.com/Test/busin;
        }
        
    }

解析:
proxy_pass http://Test.com/Test/entry;
Test.com 为upstream 的名称,代表拼接的机器地址为upstream配置的地址
/Test/entry 为实际项目的上下文

请求nginx的上下文为:
http://localhost:8090/busin/
http://localhost:8090/entry/

location匹配命令解释
参数    解释
空    location 后没有参数直接跟着 标准 URI,表示前缀匹配,代表跟请求中的 URI 从头开始匹配。
=    用于标准 URI 前,要求请求字符串与其精准匹配,成功则立即处理,nginx停止搜索其他匹配。
^~    用于标准 URI 前,并要求一旦匹配到就会立即处理,不再去匹配其他的那些个正则 URI,一般用来匹配目录
~    用于正则 URI 前,表示 URI 包含正则表达式, 区分大小写
~*    用于正则 URI 前, 表示 URI 包含正则表达式, 不区分大小写
@    @ 定义一个命名的 location,@ 定义的locaiton名字一般用在内部定向,例如error_page, try_files命令中。它的功能类似于编程中的goto。

location  = / {
    # 只精准匹配 / 的查询.
  [ configuration A ] 
}
# 匹配成功: / 

location / {
    # 匹配任何请求,因为所有请求都是以”/“开始
    # 但是更长字符匹配或者正则表达式匹配会优先匹配
  [ configuration B ] 
}
#匹配成功:/index.html

location /documents {
    # 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索/
    # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条/
  [ configuration C ] 
}
# 匹配成功:/documents/document.html
# 匹配成功:/documents/abc

location ~ /documents/ABC {
    # 区分大小写的正则匹配
    # 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索/
    # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条/
  [ configuration CC ] 
}

location ^~ /images/ {
    # 匹配任何以 /images/ 开头的地址,匹配符合以后,立即停止往下搜索正则,采用这一条。/
  [ configuration D ] 
}
# 成功匹配:/images/a.gif

location ~* \.(gif|jpg|jpeg)$ {
    # 匹配所有以 .gif、.jpg 或 .jpeg 结尾的请求,不区分大小写
    # 然而,所有请求 /images/ 下的图片会被 [ config D ]  处理,因为 ^~ 到达不了这一条正则/
    [ configuration E ] 
}
# 成功匹配:/documents/a.jpg

location /images/ {
    # 字符匹配到 /images/,继续往下,会发现 ^~ 存在/
  [ configuration F ] 
}

location /images/abc {
    # 最长字符匹配到 /images/abc,继续往下,会发现 ^~ 存在/
    # F与G的放置顺序是没有关系的/
  [ configuration G ] 
}

location ~ /images/abc/ {
    # 只有去掉 [ config D ] 才有效:先最长匹配 [ config G ] 开头的地址,继续往下搜索,匹配到这一条正则,采用/
    [ configuration H ] 
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值