编程技术分享平台

网站首页 > 技术教程 正文

Nginx调度后端的不同虚拟主机项目

xnh888 2024-09-26 00:07:38 技术教程 18 ℃ 0 评论

Nginx调度后端的不同虚拟主机项目(老男孩)

如果对运维课程感兴趣,可以在b站上搜索我的账号: 运维实战课程,可以关注我,学习更多免费的运维实战技术视频

项目 源码安装nginx服务并配置调度器,后端的两个web,都有虚拟主机:baidu和souhu,域名都是:

www.baidu.com 和 www.souhu.com 只是网页内容有点不同以作为区别。当访问百度时候,会轮询调度到后端的两个百度上,当访问搜狐的时候,也会轮询访问后面的两个搜狐上,需要做设置,否则当访问搜狐的时候,可能会访问到百度页面。

在192.168.4.5(nginx调度器) //安装nginx并配置调度器

[root@localhost 桌面]# yum -y install gcc gcc-c++

[root@localhost 桌面]# yum -y install openssl-devel openssl zlib zlib-devel pcre pcre-devel

[root@localhost 桌面]# ls

nginx-1.6.2.tar.gz

[root@localhost 桌面]# tar -zxf nginx-1.6.2.tar.gz

[root@localhost 桌面]# ls

nginx-1.6.2 nginx-1.6.2.tar.gz

[root@localhost 桌面]# cd nginx-1.6.2

[root@localhost nginx-1.6.2]# ls

auto CHANGES.ru configure html man src

CHANGES conf contrib LICENSE README

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

[root@localhost nginx-1.6.2]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

[root@localhost nginx-1.6.2]# make && make install

[root@localhost nginx-1.6.2]# ls /usr/local/nginx/

conf html logs sbin

[root@localhost nginx-1.6.2]# cd /root/桌面

[root@localhost 桌面]# /usr/local/nginx/sbin/nginx -t //检查语法

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@localhost 桌面]# /usr/local/nginx/sbin/nginx //启动nginx

[root@localhost 桌面]# /usr/local/nginx/sbin/nginx -s reload //平滑重新加载

[root@localhost 桌面]# netstat -anptu |grep nginx

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5825/nginx

[root@localhost 桌面]# vim /usr/local/nginx/conf/nginx.conf

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

upstream myserver {

server 192.168.4.205:80 max_fails=3 fail_timeout=30s;

server 192.168.4.206:80 max_fails=3 fail_timeout=30s;

}

server {

listen 80;

server_name www.baidu.com;

location / {

root html;

index index.html index.htm;

proxy_pass http://myserver;

proxy_set_header Host $host; //添加

proxy_set_header X-Forwarded-For $remote_addr; //添加

}

}

server {

listen 80;

server_name www.souhu.com;

location / {

root html;

index index.html index.htm;

proxy_pass http://myserver;

proxy_set_header Host $host; //添加

proxy_set_header X-Forwarded-For $remote_addr; //添加

}

}

}

wq

在192.168.4.205(web1服务器) //yum安装httpd1服务器,虚拟主机:baidu,souhu

[root@localhost 桌面]# yum -y install httpd

[root@localhost 桌面]# /etc/init.d/httpd start

[root@localhost 桌面]# vim /etc/httpd/conf/httpd.conf

NameVirtualHost *:80

<VirtualHost *:80>

ServerAdmin 1441107787@qq.com

DocumentRoot /baidu

ServerName www.baidu.com

ErrorLog logs/baidu.com-error_log

CustomLog logs/baidu.com-access_log common

</VirtualHost>

<VirtualHost *:80>

ServerAdmin 1441107787@qq.com

DocumentRoot /souhu

ServerName www.souhu.com

ErrorLog logs/souhu.com-error_log

CustomLog logs/souhu.com-access_log common

</VirtualHost>

wq

[root@localhost 桌面]# /etc/init.d/httpd restart

[root@localhost 桌面]# mkdir /baidu

[root@localhost 桌面]# echo "baidu205" > /baidu/index.html

[root@localhost 桌面]# mkdir /souhu

[root@localhost 桌面]# echo "souhu205" > /souhu/index.html

在192.168.4.206(web2服务器) //yum安装httpd2服务器,虚拟主机:baidu.souhu

[root@localhost 桌面]# yum -y install httpd

[root@localhost 桌面]# /etc/init.d/httpd start

[root@localhost 桌面]# vim /etc/httpd/conf/httpd.conf

NameVirtualHost *:80

<VirtualHost *:80>

ServerAdmin 1441107787@qq.com

DocumentRoot /baidu

ServerName www.baidu.com

ErrorLog logs/baidu.com-error_log

CustomLog logs/baidu.com-access_log common

</VirtualHost>

<VirtualHost *:80>

ServerAdmin 1441107787@qq.com

DocumentRoot /souhu

ServerName www.souhu.com

ErrorLog logs/souhu.com-error_log

CustomLog logs/souhu.com-access_log common

</VirtualHost>

wq

[root@localhost 桌面]# /etc/init.d/httpd restart

[root@localhost 桌面]# mkdir /baidu

[root@localhost 桌面]# echo "baidu206" > /baidu/index.html

[root@localhost 桌面]# mkdir /souhu

[root@localhost 桌面]# echo "souhu206" > /souhu/index.html

在192.168.4.66(客户端) //y客户端测试,访问调度器的百度,对应调度到后面不同服务器的百度页面,访问调度器的搜狐,对应调度到后面不同服务器的搜狐页面,

[root@localhost 桌面]# vim /etc/hosts

127.0.0.1 www.baidu.com www.souhu.com

wq

[root@localhost 桌面]# curl http://www.baidu.com

baidu206

[root@localhost 桌面]# curl http://www.baidu.com

baidu205

[root@localhost 桌面]# curl http://www.baidu.com

baidu206

[root@localhost 桌面]# curl http://www.baidu.com

baidu205

[root@localhost 桌面]# curl http://www.souhu.com

souhu206

[root@localhost 桌面]# curl http://www.souhu.com

souhu205

[root@localhost 桌面]# curl http://www.souhu.com

souhu206

[root@localhost 桌面]# curl http://www.souhu.com

souhu205

如果对运维课程感兴趣,可以在b站上搜索我的账号: 运维实战课程,可以关注我,学习更多免费的运维实战技术视频

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表