配置nginx反向代理

阳光岛主 2020-06-01

1、配置nginx反向代理,实现api.x.com域名代理本地9001端口

1.1 安装nginx

1.1.1 安装依赖包

[ nginx]# yum install gcc pcre-devel openssl-devel zlib-devel -y

1.1.2 创建nginx用户

[ ~]# useradd -r -s /sbin/nologin nginx

1.1.3 官网下载nginx源码包,并解压,编译安装

[ src]# pwd
/usr/local/src
[ src]# ls
nginx-1.16.1.tar.gz
[ src]# tar xf nginx-1.16.1.tar.gz 
[ src]# ls
nginx-1.16.1  nginx-1.16.1.tar.gz
[ src]# mv nginx-1.16.1 nginx
[ src]# cd nginx/
[ nginx]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README
[ 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
[ nginx]# make -j 4 && make install

1.1.4 配置环境变量,方便启动nginx

[ sbin]# export PATH="/usr/local/nginx/sbin:$PATH"

1.1.5 修改nginx配置文件

[ nginx]# vim /usr/local/nginx/conf/nginx.conf  ###在配置文件增加如下一行
include       /usr/local/nginx/conf.d/*.conf;
[ conf.d]# pwd
/usr/local/nginx/conf.d
[ 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 准备测试网页

[ ~]# mkdir /data/nginx/
[ ~]# echo proxypass > /data/nginx/index.html

1.3 测试

1.3.1 修改/etc/hosts文件

[ ~]# 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服务器上测试

[ conf.d]# curl api.x.com
proxypass
[ conf.d]# curl api.x.com
proxypass
[ conf.d]# curl api.x.com
proxypass
[ conf.d]# curl api.x.com
proxypass
[ conf.d]# curl api.x.com
proxypass
[ conf.d]# curl api.x.com
proxypass

相关推荐