二十一、配置nginx反向代理
1.配置nginx反向代理,实现api.x.com域名代理本地9001端口
1.准备nginx安装环境
[root@nginx ~]# systemctl stop firewalld
[root@nginx ~]# setenforce 0
[root@nginx ~]# yum install gcc pcre-devel openssl-devel zlib-devel -y
[root@nginx ~]# useradd -r -s /sbin/nologin nginx
2.解压nginx,并进行编译安装
[root@nginx ~]# tar xvf nginx-1.20.1.tar.gz
[root@nginx ~]# mkdir /data/nginx
[root@nginx ~]# cd nginx-1.20.1
[root@nginx nginx-1.20.1]# ./configure --prefix=/data/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module
[root@nginx nginx-1.20.1]# make && make install
3.配置nginx
[root@nginx nginx-1.20.1]# vi /etc/profile
PATH=$PATH:/data/nginx/sbin/
export PATH
[root@nginx nginx-1.20.1]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/data/nginx/sbin/
[root@nginx nginx-1.20.1]# cd /data/nginx/
[root@nginx nginx]# cp conf/nginx.conf conf/nginx.conf.bak
[root@nginx nginx]# vi conf/nginx.conf
server {
listen 80;
server_name api.x.com;
location / {
proxy_pass http://127.0.0.1:9001/;
}
server {
listen 9001;
server_name 127.0.0.1;
location / {
root /data/nginx/html/;
index index.html;
}
}
[root@nginx nginx]# nginx -t
nginx: the configuration file /data/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /data/nginx/conf/nginx.conf test is successful
4.创建测试页面,并进行测试
[root@nginx nginx]# echo 9001 > /data/nginx/html/index.html
[root@nginx nginx]# echo "10.0.0.11 api.x.com" >> /etc/hosts
[root@nginx nginx]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.11 api.x.com
[root@nginx ~]# curl api.x.com
9001