我是靠谱客的博主 火星上豆芽,最近开发中收集的这篇文章主要介绍简单安装和配置Haproxy、Nginx、Squid反向代理功能,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

简单安装和配置、Nginx、Squid反向代理功能

      • 实验环境准备
      • Apache Web服务配置(用作代理)
      • HAProxy 代理配置
        • 安装
        • 配置代理
      • Nginx 代理配置
        • 安装
        • 代理配置
      • Squid 代理配置
        • 安装
        • 配置
        • 问题
      • 小结

实验环境准备

VMWare Workstation 15:
    建议高版本,低版本不支持高版本Ubuntu
    
操作系统:Ubuntu 18.04.1 LTS

更新Ubuntu国内更新源:(提高下载速度)
# vi /etc/apt/sources.list
// 在sources.list添加 阿里 Ubuntu 18.04.1的源
deb http://mirrors.aliyun.com/ubuntu bionic main
deb http://mirrors.aliyun.com/ubuntu bionic-security main
deb http://mirrors.aliyun.com/ubuntu bionic-updates main

Ubuntu中安装相关软件命令:
// 在线查询源中软件
# apt-cache search haproxy
// 安装
# apt-get install haproxy

提示:
    可以统一使用apt命令,集合了apt-get、apt-cache、apt-config的命令;
    非root用户,在所有命令前面加sudo,切换root权限;
    以下大部分命令均在root权限操作;

Apache Web服务配置(用作代理)

// 本机IP地址:192.168.52.128
# apt-get install apache2  // 安装http服务

# ps -ef | grep apache2
# service apache2 stop
# service apache2 start 

// 修改配置文件,配置文件分拆多个文件和目录,这里以最简单的 
// /etc/apache2/apache2.conf  主配置文件
// /etc/apache2/ports.conf   端口配置
# vi /etc/apache2/ports.conf   // 修改Listen为8080、8081两个端口
    Listen 8080
    Listen 8081

// 修改虚拟主机对应站点的端口
# vi /etc/apache2/sites-enable/000-default.conf
    <VirtualHost *:8080>
        DocumnetRoot /var/www/html
        # ....其它默认
    </VirtualHost>
    
    <VirtualHost *:8081>
        DocumentRoot /var/www/myhtml
        ServerName  www.myweb.com
    </VirtualHost>
    
# cd /var/www
# mkdir myhtml
# vi index.html  // 新建首页,里面直接输入“Hello,www.myweb.com站点”
# service apache2 restart //重启apach2服务

// web默认站点目录:/var/www/html和myhtml
# cd /var/www

# vi /etc/hosts  //添加127.0.0.1 www.myweb.com

# curl 192.168.52.128:8080  // 查看apache默认主页,web服务成功
# curl 192.168.52.128:8081  // 访问myhtml站点
# curl www.myweb.com:8081   // 通过本机域名访问

HAProxy 代理配置

安装

# apt install haproxy
# ps -ef | grep haproxy  // 查看haproxy是否自动启动
# netstat -antp  // 查看haproxy服务监听端口,默认80端口 

# whereis haproxy  // 查看安装路径,默认在/usr/sbin/haproxy
                   // 配置文件在/etc/haproxy

haproxy服务启动和关闭:
// 使用service命令
# service haproxy start/stop/reload/restart/status  或者
# /etc/init.d/haproxy start/stop/reload/restart/status

// 使用haproxy命令,上面最终也是调用haprxoy程序
# haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid

配置代理

 # vi /etc/haproxy/haproxy.cfg  
 ## --- haproxy.cfg 主要配置项 ---
       defaults
            mode http
        
        frontend http-in
            bind *:80
            default_backend servers
        
        backend servers
            balance roundrobin
            server server1 192.168.52.128:8080
            server server2 www.myweb.com:8081
            server server3 192.168.52.128:8081

# service haproxy restart // 重启
# curl 192.168.52.128:80  // 从代理访问apache主页,连续三次,会轮询访问server1-3.

Nginx 代理配置

安装

    # apt-get install nginx
    # whereis nginx    // 查看安装目录和配置文件/etc/nginx
    # service nginx restart/stop/start // 服务的重启、停止操作

代理配置

    # cd /etc/nginx  // nginx配置目录,具备多个配置子文件和目录
    # cd /etc/nginx/sites-available/  // 进入站点配置目录
    # vi mydefault   // 新建一个自定义的配置,输入以下内容
   upstream ubuntu {
                server 192.168.52.128:8080 weight=10;
                server 192.168.52.128:8081 weight=10;
                server www.myweb.com:8081 weight=10;

            }
            
            server {
                    listen 90;
                    location / {
                        proxy_pass http://ubuntu;
                    }
                    
            }
    # cd /etc/nginx/sites-enabled/  // 进入站点配置启用目录
    # ln -s ../sites-available/mydefalut default // 建立符号链接启用
    # service nginx restart 
    # curl 192.168.52.128:90 // 从ngnix代理访问

Squid 代理配置

安装

    # apt-get install squid
    # whereis squid  // 查看安装目录
    # cd /etc/squid
    # mv squid.conf squid.conf.bak  // 将默认的配置文件备份

配置

    # cd /etc/squid
    # vi squid.conf  // 新建配置文件,输入以下内容保存
    
    // cache_peer 中指明被代理的IP地址和端口服务
    http_port 100 vhost
    visible_hostname 192.168.52.128
    cache_peer localost parent 0 no-query originserver name=ubuntu
    cahce_peer www.myweb.com parent 0 no-query originserver name=myweb
    http_access allow all
    cache_peer_access ubuntu allow all
    cache_peer_access myweb allow all
    # /etc/init.d/squid stop
    # /etc/init.d/squid start
    # curl 192.168.52.128:100

问题

1.squid:The administrator may not allow this cache to make direct connections to origin servers.
原因:不允许代理直接访问本机的web服务,cache_peer 192.168.52.128 parent 8080 0 no-query originserver name=ubuntu会禁止访问,将本机IP地址改为localhost即可成功代理本机web服务,见上述配置。

小结

1.本文配置基本实现代理的功能,另外简单配置负载均衡功能;
2.反向代理一般根据请求的路径或域名不同,固定将请求转发至后面的服务器上面,因此,不需要session同步之类的配置;
3.负载均衡功能,会将用户相同路径的请求转发至不同的服务器,因此,需要考虑会话保持、Session同步、均衡策略的问题,以上只是简单的配置,未考虑。

最后

以上就是火星上豆芽为你收集整理的简单安装和配置Haproxy、Nginx、Squid反向代理功能的全部内容,希望文章能够帮你解决简单安装和配置Haproxy、Nginx、Squid反向代理功能所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部