centos7 下配置虚拟主机以及负载均衡配置
作者:nango
阅读:1370次
来源:原创
时间:2016-11-17 14:09
首先,我的操作系统是centos7,并且已经配置好php开发环境nginx+php-fpm;如果你不知道怎么在centos7下搭建php环境,请参考CENTOS7 下部署php环境该文章。 ## nginx如何配置虚拟主机: 安装完nginx后,我的nginx配置目录在 ``` /usr/local/nginx/conf ``` 下,如果你找不到nginx的配置目录可以用 ``` #> whereis nginx ``` 命令来查找;在conf目录下,我们会看到vhost目录,该目录主要放置nginx的虚拟主机配置文件,那么我们在改目录下新建nonecms.conf文件(名字可随意定,一般命名为你的域名名字) ``` #> vim nonecms.conf ``` 然后输入一下代码 ``` http{ #添加 server { listen 80; #这里是你要配置的域名 server_name www.nonecms.com; location / { #网站指向的目录 root /usr/share/nginx/html/; index index.php index.html index.htm; #//url重写, rewrite方式代替php中的PATH_INFO #if (!-e $request_filename) { # rewrite ^(.*)$ /index.php?s=/$1 last; # break; #} } location ~ .php$ { root /usr/share/nginx/html/; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } } ``` 保存后退出,然后打开hosts文件添加 ``` #> vim /etc/hosts 127.0.0.1 www.nonecms.com ``` 保存退出,重启nginx即可; ## nignx负载均衡配置: 负载均衡配置我们依然在vhost目录下配置,我们新建一个balance.conf文件,并输入 ``` upstream www.nonecms.com { server 198.198.200.29:80 weight=5; server 198.198.200.28:80 weight=5; } server { listen 80; server_name www.nonecms.com; location / { proxy_set_header Host $host; proxy_set_header X-Real-Ip $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://www.nonecms.com; } } ``` > 这里www.l.com是你需要负载均衡的域名,server 198.198.200.29:80 weight=5;这个是你负载的几个服务器地址; weight意思是要按照权重来进行负载,weight越大被调用的几率越大,具体可以查看nginx负载均衡相关资料。 > 如果要在代理服务器部署代码,必须将web端口和代理监听端口分割开,一个启用80.一个8000或者其他;修改 nginx.conf 文件,在centos7 上该文件的路径为 /usr/local/nginx/conf/nginx.conf,将80端口改为8000(任意端口)即可 到此,我们就在centos7下配置好了虚拟主机以及负载均衡,更详细资料请大家在网络上搜索,也可以留言交流
NANGO
首 页
编程
聊天室
简介:
欢迎大家光临nango的博客,该博客由NoneCms搭建而成。
文章分类
PHP
js
centos
Python
MySQL
Laravel
最新文章
NoneCms 重大bug更新
docker compose编排的php开发环境
NoneCms 基于workerman的聊天室具体使用
centos7 + sendmail + php mail()函数实现邮件发送
文章归档
2018-12 (2)
2018-09 (1)
2018-01 (1)
2017-10 (1)
2017-07 (2)
2017-06 (2)
2017-03 (1)
2017-02 (2)
2017-01 (2)
2016-11 (4)
2016-10 (5)
2016-09 (3)
您的浏览器不支持 canvas.
© Nango
NANGO
文章分类