nginx正向代理和反向代理

一、正向代理

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

nginx正向代理和反向代理_第1张图片

[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

nginx正向代理和反向代理_第2张图片

[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

nginx正向代理和反向代理_第3张图片

 

你可能感兴趣的:(#,nginx,运维,linux,rhel7)