温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

基于Linux Centos7 实现nginx的代理服务器以及动静分离的方法和步骤

发布时间:2020-04-16 14:18:36 来源:亿速云 阅读:289 作者:三月 栏目:系统运维

下文给大家带来基于Linux Centos7 实现nginx的代理云服务器以及动静分离的方法和步骤,希望能够给大家在实际运用中带来一定的帮助,负载均衡涉及的东西比较多,理论也不多,网上有很多书籍,今天我们就用亿速云在行业内累计的经验来做一个解答。

一:环境
准备一个nginx代理云服务器 三台http服务器两台处理静态和一台处理动态。(nginx/1.17.3)

二、在nginx主配置文件配置nginx反向代理upstream(地址池)指向真实服务器

         vim /etc/nginx/nginx.conf

在http标签中加

    upstream static {
    server 10.30.161.214:80 weight=2 max_fails=2 fail_timeout=2s;
    server 10.30.161.242:80 weight=2 max_fails=2 fail_timeout=2s;
    }   
    upstream php {
    server 10.30.161.241:80 weight=2 max_fails=2 fail_timeout=2s;   
    }

三、在子配置文件中

    vim /etc/nginx/conf.d/proxy.conf

1、动态资源加载
在server标签中添加

基于Linux Centos7 实现nginx的代理服务器以及动静分离的方法和步骤

    location ~ \.(php|jsp)$ {
    proxy_pass http://phpserver;
    #指向动态服务器地址池
    proxy_set_header Host $host:$server_port;
    #重新定义或者添加发往后端服务器的请求头$host真实服务器主机名$server_port端口
    proxy_set_header X-Real-IP $remote_addr;
    #启用客户端真实地址(否则日志中显示的是代理在访问网站)
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    #显示http请求端的真实IP
    }

2、静态资源加载

        location ~ .*\.(html|gif|jpg|png|bmp|swf|css|js)$ {
        proxy_pass http://static;
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            }
        }

至此代理服务器配置完成

四、两台静态服务器的单独配置

    server {
    listen 80;
    server_name     localhost;

    location ~ \.(html|jpg|png|js|css|gif|bmp|jpeg) {
    root   /web1;
    index  index.html;
}

}
配置静态项目
静态服务器1

        mkdir /web1
        echo "this is jingtai11111" > /web1/index.html

静态服务器2

        mkdir /web1
        echo "this id jingtai22222" > /web1/index.html

五、一台动态服务器的单独配置

    yum 安装php7.1
[root@nginx-server ~]#rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
[root@nginx-server ~]#rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[root@nginx-server ~]#yum install php71w-xsl php71w php71w-ldap php71w-cli php71w-common php71w-devel php71w-gd php71w-pdo php71w-mysql php71w-mbstring php71w-bcmath php71w-mcrypt -y
[root@nginx-server ~]#yum install -y php71w-fpm
[root@nginx-server ~]#systemctl start php-fpm
[root@nginx-server ~]#systemctl enable php-fpm

编辑nginx的配置文件:

server {
        listen      80;
        server_name     localhost;
        location ~ \.php$ {
            root           /web1;  #指定网站目录
            fastcgi_pass   127.0.0.1:9000;    #指定访问地址
            fastcgi_index  index.php;       #指定默认文件
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; #站点根目录,取决于root配置项
            include        fastcgi_params;  #包含nginx常量定义
                }
        }

配置动态web1上项目

mkdir /web1
 cd /web1
 vim index.php 
 <?php
phpinfo();
?>

六、测试
1,静态访问测试
用浏览器访问代理服务器IP  10.30.161.51/index.html
即可访问到静态服务器/web1上的项目,并且两台服务器来回切换,这就实现了nginx的代理和负载均衡

2,动态访问测试
用浏览器访问代理服务器IP  10.30.161.51/index.php
即可访问到动态服务器/web1上的项目,从而实现了nginx的动静分离

看了以上关于基于Linux Centos7 实现nginx的代理服务器以及动静分离的方法和步骤,如果大家还有什么地方需要了解的可以在亿速云行业资讯里查找自己感兴趣的或者找我们的专业技术工程师解答的,亿速云技术工程师在行业内拥有十几年的经验了。亿速云官网链接www.yisu.com

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI