Centos7实现nginx反向代理

反向代理服务器架设在服务器端,通过缓冲经常被请求的页面来缓解服务器的工作量,将客户机请求转发给内部网络上的目标服务器;并将从服务器上得到的结果返回给Internet上请求连接的客户端,此时代理服务器与目标主机一起对外表现为一个服务器。目前web网站使用反向代理,除了可以防止外网对内网服务器的恶性攻击、缓存以减少服务器的压力和访问安全控制之外,下面为大家详细讲解一下Centos7实现nginx反向代理具体方法

成都创新互联专注于企业成都全网营销推广、网站重做改版、桐城网站定制设计、自适应品牌网站建设、成都h5网站建设电子商务商城网站建设、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为桐城等各大城市提供网站开发制作服务。

实验环境:

192.168.1.188 nginx负载均衡器

192.168.1.189 web01服务器

192.168.1.190 web02服务器

软件准备:

centos7.4 x86_64

nginx-1.6.3.tar.gz

安装nginx软件

安装依赖软件包命令集合

[root@localhost ~]# yum -y install openssl openssl-devel pcre pcre-devel gcc

安装nginx软件包命令集合

[root@localhost ~]# mkdir /app

[root@localhost ~]# cd /app

[root@localhost ~]# wget -q http://nginx.org/download/nginx-1.6.3.tar.gz

[root@localhost ~]# useradd -s /sbin/nologin -M

[root@localhost ~]# tar xf nginx-1.6.3.tar.gz

[root@localhost ~]# cd nginx-1.6.3

[root@localhost ~]# ./configure --user=nginx --group=nginx --prefix=/app/nginx --with-http_stub_status_module --with-http_ssl_module

[root@localhost ~]# make && make install

配置文件

(以下操作在web01和web02上进行)

[root@localhost ~]# vim /app/nginx/conf/nginx.conf

将配置文件修改为以下内容

worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "http_referer" '
'"$http_user_agent" " $http_x_forwarded_for"';
server {
listen 80;
server_name bbs.dengchuanghai.org;
location / {
root html/bbs;
index index.html index.htm;
}
access_log logs/access_bbs.log main;
}

}
[root@localhost ~]# mkdir /app/nginx/html/bbs

[root@localhost ~]# echo "192.168.1.189 bbs" >>/app/nginx/html/bbs/index.html            

[root@localhost ~]# echo "192.168.1.189 bbs.dengchuanghai.org" >> /etc/hosts                    

[root@localhost ~]# echo "192.168.1.190 bbs" >>/app/nginx/html/bbs/index.html  

[root@localhost ~]# echo "192.168.1.190 bbs.dengchuanghai.org" >> /etc/hosts

(分别在两台web服务器上输入以上内容)

然后分别启动nginx

[root@localhost ~]# /app/nginx/sbin/nginx -t (检查配置文件有无错误)

[root@localhost ~]# /app/nginx/sbin/nginx  启动

[root@localhost ~]# ss -tnlp | grep 80

使用curl bbs.dengch ,以下操作在nginx负载均衡器上进行

[root@localhost ~]# vim /app/nginx/conf/nginx.conf

更改为如下内容

worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream www_server_pools{
server 192.168.1.190:80 weight=1;
server 192.168.1.189:80 weight=1;
}
server {
listen 80;
server_name www.dengchuanghai.org;
location / {
proxy_pass http://www_server_pools;
}
}
}
[root@localhost ~]# echo "192.168.1.188 www.dengchuanghai,org" >> /etc/hosts

检查语法

[root@localhost ~]# /app/nginx/sbin/nginx -t

启动服务

[root@localhost ~]# /app/nginx/sbin/nginx

使用curl www.dengchuanghai.org 发现结果输出为两台服务器轮流输出

网页题目:Centos7实现nginx反向代理
网站URL:http://www.shufengxianlan.com/qtweb/news32/548382.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联