1.1 安装nginx
1.1.1 安装依赖包
[root@c1 nginx]# yum install gcc pcre-devel openssl-devel zlib-devel -y
1.1.2 创建nginx用户
[root@c1 ~]# useradd -r -s /sbin/nologin nginx
1.1.3 官网下载nginx源码包,并解压,编译安装
[root@c1 src]# pwd
/usr/local/src
[root@c1 src]# ls
nginx-1.16.1.tar.gz
[root@c1 src]# tar xf nginx-1.16.1.tar.gz
[root@c1 src]# ls
nginx-1.16.1 nginx-1.16.1.tar.gz
[root@c1 src]# mv nginx-1.16.1 nginx
[root@c1 src]# cd nginx/
[root@c1 nginx]# ls
auto CHANGES.ru configure html man src
CHANGES conf contrib LICENSE README
[root@c1 nginx]# ./configure --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_dav_module --with-http_stub_status_module --with-threads --with-file-aio
[root@c1 nginx]# make -j 4 && make install
1.1.4 配置环境变量,方便启动nginx
[root@c1 sbin]# export PATH="/usr/local/nginx/sbin:$PATH"
1.1.5 修改nginx配置文件
[root@c1 nginx]# vim /usr/local/nginx/conf/nginx.conf ###在配置文件增加如下一行
include /usr/local/nginx/conf.d/*.conf;
[root@c1 conf.d]# pwd
/usr/local/nginx/conf.d
[root@c1 conf.d]# cat proxy.conf
server {
server_name api.x.com;
location / {
proxy_pass http://localhost:9001;
}
}
server {
listen 9001;
server_name _;
root /data/nginx;
index index.html;
}
1.1.6 准备测试网页
[root@c1 ~]# mkdir /data/nginx/
[root@c1 ~]# echo proxypass > /data/nginx/index.html
1.3 测试
1.3.1 修改/etc/hosts文件
[root@c2 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.1.1.242 c1 api.x.com
1.3.2 在c2服务器上测试
[root@c2 conf.d]# curl api.x.com
proxypass
[root@c2 conf.d]# curl api.x.com
proxypass
[root@c2 conf.d]# curl api.x.com
proxypass
[root@c2 conf.d]# curl api.x.com
proxypass
[root@c2 conf.d]# curl api.x.com
proxypass
[root@c2 conf.d]# curl api.x.com
proxypass
原文:https://blog.51cto.com/rickzhu/2500271