码迷,mamicode.com
首页 > Web开发 > 详细

Linux中Nginx反向代理和负载均衡和LNMP架构上线网站

时间:2019-05-29 09:15:46      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:静态文件   aci   lnp   lpn   对比   dmi   mui   gif   tar   

Nginx和Apache对比(重点)
1.轻量级,采用 C 进行编写,同样的 web 服务,会占用更少的内存及资源
2.nginx 处理静态文件好,静态处理性能比 apache 高三倍以上,apache 在处理动态请求有优势
3.nginx 作为负载均衡服务器,支持 7 层负载均衡
4.抗并发,nginx 以 epoll and kqueue 作为开发模型
 
nginx部署:
第一步:配置yum源(原基础上添加)
[root@ken ~]vim  /etc/yum.repos.d
[root@ken yum.repos.d] ls
[root@ken yum.repos.d]vim local.repo 
[epel]
name=epel
enabled=1
gpgcheck=0
 
 
第二步:下载nginx
[root@ken yum.repos.d]# yum install nginx -y
 
 
第三步:启动nginx
[root@ken yum.repos.d]# systemctl restart nginx
 
 
nginx反向代理–动静分离(设置三台虚拟机)
192.168.64.123 主服务器
192.168.64.45 静态节点
192.168.64.3 动态节点
 
 
第一步:在主服务器部署nginx并配置动静分离规则
  [root@ken html]#vim /etc/nginx/nginx.conf
   location ~ html$ {
   proxy_pass http://192.168.64.45;
    }
   location ~ php$ {
   proxy_pass http://192.168.64.3;
   }
 
 
第二步:检测语法重启nginx
[root@ken html]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@ken html]# systemctl restart nginx
 
 
第二步:配置静态服务器(打开静态服务器)
[root@localhost ~]# cd  /var/www/html
[root@localhost html]# ls
[root@localhost html]# echo "zhangpan" >> /var/www/html/index.html
[root@localhost html]# cat index.html
 
 
第三步:配置动态服务器
[root@localhost ~]# cd  /var/www/html
[root@localhost html]# yum install php httpd -y
[root@localhost html]#vim  /var/www/html/index.php(写入下边的动态服务)
<?php
phpinfo();
?>
 
 
第四步:浏览器访问主服务器
 
 
nginx反向代理–不完全代理
第一步:编辑nginx文件,编辑规则(在主服务器中编辑)(把之前的删掉)
[root@ken html]#vim /etc/nginx/nginx.conf
location /admin{
proxy_pass http://192.168.64.45;
}
 
 
 
第二步:检测并重启
[root@ken html]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@ken html]# systemctl restart nginx
 
 
 
第三步:浏览器访问
访问不到
 
 
 
第四步:创建文件(在静态服务器中)
[root@ken html]# mkdir admin
[root@ken html]# echo “56566” > admin/index.html
 
 
 
第五步:浏览器再次访问
访问成功
总结:不完全代理的话location后面定义的访问uri会自动填补到IP地址的后面,location /admin{
proxy_pass http://192.168.64.5;
}
比如上面这个规则,意思就是http://192.168.64.5/admin
 
 
nginx反向代理–完全代理
第一步:编辑nginx文件,编辑规则(在主服务器中编辑)(把之前的删掉)
[root@ken html]#vim /etc/nginx/nginx.conf
location /admin{
proxy_pass http://192.168.64.45/;
}
 
 
 
第二步:检测并重启
[root@ken html]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@ken html]# systemctl restart nginx
 
 
第三步:浏览器访问
可以访问
总结:完全代理的话location后面定义的访问uri不会自动填补到IP地址的后面,location /admin{
proxy_pass http://192.168.64.5/;
}
比如上面这个规则,意思就是直接http://192.168.64.5
 
 
nginx负载均衡演示
第一步:编写规则
[root@ken html]#vim /etc/nginx/nginx.conf(在主服务器中)
upstream pan {
server 192.168.64.5 weight=6 max_fails=2 fail_timeout=2;
server 192.168.64.7 weight=1 max_fails=2 fail_timeout=2;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /var/www/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://pan;
}
 
 
第二步:检查语法并重启
[root@ken html]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@ken html]# systemctl restart nginx
 
 
第三步:浏览器访问主节点
 
 
LNMP架构上线网站
第一步:下载相关的软件包(在主服务器中)
[root@ken html]# yum install nginx php php-mysql mariadb-server php-fpm -y
 
 
 
第二步:编辑php匹配规则
[root@ken html]#vim /etc/nginx/nginx.conf(在主服务器中)
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /var/www/html;
index index.php index.html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
location ~ php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
}
 
 
 
第三步:检测并重启
[root@ken html]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@ken html]# systemctl restart nginx
 
 
第四步:启动php-fpm(在原基础上添加)
[root@ken ~]# cd /etc/yum.repos.d
[root@ken /etc/yum.repos.d ]# ls
[root@ken /etc/yum.repos.d ]#vim local.repo
 
[centos]
name=centos base
enabled=1
gpgcheck=0
 
 
[root@ken yum.repos.d]# systemctl restart php-fpm
[root@ken yum.repos.d]# ss -tnl
 
 
第五步:重启数据库
[root@ken yum.repos.d]# systemctl restart mariadb
 
 
 
第六步:创建数据库添加用户
MariaDB [(none)]> create database pan;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all on pan.* to ken@’localhost’ identified by ‘123’;
Query OK, 0 rows affected (0.00 sec)
 
 
 
第七步:上传wordpress安装包至nginx网站根目录下/var/www/html并解压
[root@ken html]# yum install unzip -y
[root@ken html]# unzip wordpress-3.3.1-zh_CN.zip
 
 
 
第八步:配置数据库文件
[root@ken html]# cp wp-config-sample.php wp-config.php
[root@ken html]# vim wp-config.php
 
define(‘DB_NAME’, ‘pan’);
 
/** MySQL 数据库用户名 */
define(‘DB_USER’, ‘pan’);
 
/** MySQL 数据库密码 */
define(‘DB_PASSWORD’, ‘123’);
 
 
第九步:浏览器访问

Linux中Nginx反向代理和负载均衡和LNMP架构上线网站

标签:静态文件   aci   lnp   lpn   对比   dmi   mui   gif   tar   

原文地址:https://www.cnblogs.com/zp-1996/p/10941691.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!