nginx正向代理和反向代理

一、正向代理

①添加网卡(仅主机模式)

[root@host1 conf.d]# pwd
/usr/local/nginx/conf.d

②配置

[root@host1 conf.d]# vim test1.conf

这里设置监听端口为8080

server {
        resolver 114.114.114.114;
        listen 8080;
        location / {
                proxy_pass http://$http_host$request_uri;
        }
}

③重启nginx

二、反向代理(host1 host2 localhost 分别为三台linux虚拟机)

#简单模拟

host2 和 localhost 安装httpd

①localhost配置

localhost 设置为仅主机模式添加ip 192.168.91.131

[root@localhost ~]# echo "this is a test" > /var/www/html/index.html
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0

 ②host1 配置

这里设置监听端口为8800

[root@host1 conf.d]# vim test2.conf

server {
        listen 8800;
        server_name localhost;
        location /{
                proxy_pass http://192.168.91.131;
        }
}

如下:测试语法无误

 重启nginx

 ③使用一台不能ping通linux电脑curl

此台电脑需要关掉防火墙并设置selinux

curl 8800 端口

1、配置用于测试的两台Web服务器(host2、host3)

host2、host3为两台web服务器

均安装epel(rhel7)

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

使用yum下载nginx

yum install nginx -y
mkdir /usr/share/nginx/html/{www,bbs}/logs -p

cd /etc/nginx/conf.d

vim vhost.conf

server {
        listen 80;
        server_name bbs.yunjisuan.com;
        location / {
                root /usr/share/nginx/html/bbs;
                index index.html index.htm;
        }
         access_log /usr/share/nginx/html/bbs/logs/access_bbs.log main;
}
server {
        listen 80;
        server_name www.yunjisuan.com;
        location / {
                root /usr/share/nginx/html/www;
                index index.html index.htm;
        }
         access_log /usr/share/nginx/html/www/logs/access_www.log main;
}
echo "`hostname -I `www" > /usr/share/nginx/html/www/index.html
echo "`hostname -I `www" > /usr/share/nginx/html/bbs/index.html

2、host1、host2关掉防火墙,设置selinux

systemctl stop firewalld
setenforce 0

3、host1、host2开启nginx

systemctl start nginx

检查语法
nginx -t

4、重新找一台电脑curl

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值