nginx安装后文件的大致结构
- 所有的配置文件都在/etc/nginx下,并且每个虚拟主机已经安排在了/etc/nginx/sites-available下
- 程序文件在/usr/sbin/nginx
- 日志放在了/var/log/nginx中
- 并已经在/etc/init.d/下创建了启动脚本nginx
- 默认的虚拟主机的目录设置在了/var/www/nginx-default (有的版本默认的虚拟主机的目录设置在了/var/www, 请参考/etc/nginx/sites-available里的配置)
安装nginx
sudo apt-get install nginx
查看nginx版本
# 进入目录
cd usr/sbin/
# 查看版本
./ nginx -v
# 查看nginx状态
ps -ef | grep nginx
启动Nginx服务
sudo /etc/init.d/nginx start
优雅停止Nginx服务
sudo /etc/init.d/nginx quit
加载最新配置
sudo /etc/init.d/nginx reload
立即停止Nginx服务
sudo /etc/init.d/nginx stop
nginx 配置文件组成
- 第一部分 全局块
worker_process 1; 支持的并发处理数量
- events块
worker_connections 1024; 支持的最大连接数
- http块
- http全局块
- server块
- 全局server
- location
nginx相关操作命令
nginx 配置反向代理:修改配置文件中server中listen和server_name值,一般选择修改server_name.
再修改location中参数 ~ /xxx/ {http://proxy_pass: address:port}
location中参数说明可以百度查阅
实例之反向代理
主要是配置server下的端口和访问ip, 默认是80和localhost
然后配置server下的location参数,修改地址为指定的服务器地址。
实例之负载均衡实现
- 在nginx中配置 http 块中配置upstream,修改server中ip,ui后修改server
http {
upstream myserver{
ip_hash;
server ip:port weight=1;
server ip:port weight=1;
}
}
...
server {
location ~ / {
proxy_pass http://myserver(在http块中的名称);
proxy_connect_timeout 10;
}
}
- 负载均衡策略
- 轮询(默认)
- weight(权重)
- ip_hash
- fair(第三方)
- 按后端响应时间分配,响应时间短的优先分配
实例之-动静分离
主要是配置location, 将动态文件和静态文件分开。并且修改server的ip地址
配置高可用
-
准备工作
- 准备两台linux服务器
- 分别安装nginx
- 分别安装keepalived
-
修改keepalived.conf
-
global_defs { # 全局配置
…
router_id LVS_DEVELBACK # etc/host文件中
}
-
vrrp_script chk_http_port{
script “/usr/local/src/nginx_check.sh”
interval 2 #(检测脚本和权重)
weight -20 # 权重
}
-
vrrp_instance VI_1 {
state MASTER # 备份服务器上将MASTER修改为 BACKUP
interface ens33 # 网卡
virtual_router_id 51 # 主、从机的virtual_router_id必须相同
priority 100 # 主、从机取不同的优先级、主机一般较大
authentication { # 权限校验方式
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
ip # VRRP 虚拟地址, 外网访问的网址,主从均绑定该ip
}
}
-
添加第二步中脚本文件到指定目录usr/local/src
脚本文件内容
#!/bin/bash A=`ps -C nginx -no-header |wc -1` if [ $A -eq 0]; then /usr/local/nginx/sbin/nginx sleep 2 if [`ps -C nginx --no-header |wc -1` -eq 0];then killall keepalived fi fi
-
-
完成高可用配置
-
启动两台服务器的nginx和keepalive
keepalived 启动命令 :systemctl start keeplived.service
查看启动状态:ps -ef | grep keepalived
-
通过liunx命令 ip a可以看到虚拟ip的绑定
-
配置高可用
-
准备工作
- 准备两台linux服务器
- 分别安装nginx
- 分别安装keepalived
-
修改keepalived.conf
-
global_defs { # 全局配置
…
router_id LVS_DEVELBACK # etc/host文件中
}
-
vrrp_script chk_http_port{
script “/usr/local/src/nginx_check.sh”
interval 2 #(检测脚本和权重)
weight -20 # 权重
}
-
vrrp_instance VI_1 {
state MASTER # 备份服务器上将MASTER修改为 BACKUP
interface ens33 # 网卡
virtual_router_id 51 # 主、从机的virtual_router_id必须相同
priority 100 # 主、从机取不同的优先级、主机一般较大
authentication { # 权限校验方式
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
ip # VRRP 虚拟地址, 外网访问的网址,主从均绑定该ip
}
}
-
添加第二步中脚本文件到指定目录usr/local/src
脚本文件内容
#!/bin/bash A=`ps -C nginx -no-header |wc -1` if [ $A -eq 0]; then /usr/local/nginx/sbin/nginx sleep 2 if [`ps -C nginx --no-header |wc -1` -eq 0];then killall keepalived fi fi
-
-
完成高可用配置
-
启动两台服务器的nginx和keepalive
keepalived 启动命令 :systemctl start keeplived.service
查看启动状态:ps -ef | grep keepalived
-
通过liunx命令 ip a可以看到虚拟ip的绑定
-
如有错误,还望指正,一起学习,共同提高!