# Nginx

# 一、CentOS 源码安装

  1. 安装相关包
sudo yum -y install openssl openssl-devel pcre pcre-devel zlib zlib-devel gcc gcc-c++
1
  1. 官方下载最新版然后解压

官方:http://nginx.org/en/download.html

tar -xzf nginx-1.20.2.tar.gz
1
  1. 编译安装
# -prefix=要安装到的目录
./configure --prefix=/opt/nginx
make && make install
1
2
3
  1. 启动 Nginx
/opt/nginx/sbin
./nginx
1
2
  1. 非root用户不能开1024以下端口报错处理
# 让当前用户的某个应用也可以使用 1024 以下的端口
sudo setcap cap_net_bind_service=+eip /opt/nginx/sbin/nginx
1
2
  1. 查看启动情况
hdp1-➜  sbin ps -ef |grep nginx
root     18637     1  0 14:23 ?        00:00:00 nginx: master process ./nginx
nobody   18638 18637  0 14:23 ?        00:00:00 nginx: worker process
root     18647 13660  0 14:24 pts/0    00:00:00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox nginx
1
2
3
4
  1. 重启 Nginx
./nginx -s reload
1
  1. 关闭 Nginx
./nginx -s stop
1
  1. 通过配置文件启动
# 其中-c 是指定配置文件,而且配置文件路径必须指定绝对路径
/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
1
2
  1. 配置检查
/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf –t
1

# 二、配置负载均衡

  1. 打开nginx配置文件
vim /opt/nginx/conf/nginx.conf
1
  1. 修改内容如下


 
 
 
 
 






 
 

 
 











http {
    # 省略设置
    upstream logcluster{
      server hdp1:8081 weight=1; 
      server hdp2:8081 weight=1; 
      server hdp3:8081 weight=1;
    }
    
    server {
        listen       80;
        server_name  localhost;

        location / {
            # root   html;
            # index  index.html index.htm;
            # 代理的服务器集群 命名随意, 但是不能出现下划线 
            proxy_pass http://logcluster;
            proxy_connect_timeout 10;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        # 省略
    }
    # 省略
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
更新时间: 1/10/2022, 1:41:41 PM