Nginx在windows和centos7中的应用

NGINX-Windows

 

1、NGINX作为应用非常广泛的反向代理服务器,其衍生了很多子产品,比如openresty和kong,这两个产品公司都在用,其中比较难以使用的openstry,因为官方文档是英文,使用者比较少,敢于在生产环境中使用有是少之又少。kong网管因为集合了很多插件,又有自己的UI界面,自身良好的性能,所以使用非常广泛。NGINX可以轻松实现百万级别的QPS。

2、针对NGINX的代理配置,主要是对conf/nginx.conf文件进行逻辑修改,以下是针对请求的转发配置。

在http模块中: 

upstream tomcat_server{
        server 127.0.0.1:8089 weight=2;
        #server 127.0.0.1:8089/xxx?matchId=13 weight=1;
    }

在server模块中:

server{

       listen       8088;
        server_name  localhost;

 location / {
        proxy_pass http://tomcat_server;
        index  index.html index.htm;
       }

}

用postman请求:localhost:8088/xxx?matchId=12 则请求会转发到 127.0.0.1:8089/xxx?matchId=12

upstreamm模块是对请求进行分发,可以由权重、轮询、ip_hash等策略(第三方fair等策略自行研究)

 

在centos7中安装Nginx做反向代理

 

1、排除一些不必要的麻烦,首先关闭防火墙。禁止防火墙自启动

关闭防火墙:systemctl stop firewalld.service

禁止自启动: systemctl disabled firewalld.service

2、新建存储nginx文件的目录

mkdir /data/service/nginx

#download nginx package
wget http://nginx.org/download/nginx-1.13.7.tar.gz

#unzip package
tar -zxvf nginx-1.13.7.tar.gz

#Enter dir
cd nginx-1.13.7


#configure配置,configure详解:https://www.jb51.net/LINUXjishu/363271.html
./configure
#Then make
make
make install
#default  nginx work packagt /usr/local/nginx

#modify nginx.conf 
vi /usr/local/nginx/conf/nginx.conf


3、nginx.conf文件修改部分内容

三个服务端口:8011 8012 8013 8014  开nginx端口 8082

#在http模块下加入权重分发
 upstream server_pass{
    server 127.0.0.1:8011 weight=1;
    server 127.0.0.1:8012 weight=1;
    server 127.0.0.1:8013 weight=1;
    server 127.0.0.1:8014 weight=1;
  }

#server模块下加入nginx监听端口,使用域名访问,server_name换成域名即可,域名端口要和port相同。
server{
         listen       8082;
        server_name  localhost;
        location / {
        proxy_pass http://server_pass;
            root   html;
            index  index.html index.htm;
        }
       #针对请求中带有bbb这个地址,可以走一下upstream
        location ^~/bbb/ {
            proxy_pass http://bbb;

        }
}
#加载执行conf配置文件
./usr/local/nginx/sbin/nginx -s reload


nginx请求 http://xx.xx.xx.xx:8082/demo/xxx?name=1212

执行以上请求,即可完成nginx对四个服务的转发。

4、中途出现错误

./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
statically from the source with nginx by using --with-pcre=<path> option.

It requires the PCRE library.so execute this shell.

yum -y install openssl openssl-devel

setUp complete is OK.

nginx默认监听端口80,查看端口占用情况: netstat -ano |findstr "80"pei

配置文件生效:nginx -s reload conf  (windows)

关闭nginx :nginx  -s stop

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值