查看防火墙开放端口
[root@localhost /]# firewall-cmd --list-all
[root@localhost conf]# firewall-cmd --add-port=80/tcp --permanent //添加端口号
[root@localhost conf]# firewall-cmd --reload //重启防火墙
命令:
1.关闭nginx
[root@localhost sbin]# /usr/local/nginx/sbin/nginx -s stop
2.启动
[root@localhost sbin /usr/local/nginx/sbin/nginx
3.重新加载
[root@localhost sbin]# /usr/local/nginx/sbin/nginx -s reload
4.查看版本
[root@localhost sbin /usr/local/nginx/sbin/nginx -v
相关文件
配置文件: usr/local/nginx/conf/nginx.conf
第一部分 全局块
从配置文件到events块之间的内容,用于设置nginx服务运行的配置指令,包括配置nginx的用户(组),允许生成的work_process数,进程PID存放路径,日志存放路径和类型以及配置文件的引入等.
第二部分 events 块
exents 主要影响Nginx服务器与用户的网络连接
第三部分 http 块
用于配置反向代理,负载均衡,动静分离及高可用性.
http块又分为:
- http 全局块
http全局块配置包括文件引入,MIME-TYPE定义,日志自定义,连接超时时间,单链接请求上限等. - server 块
用于配置虚拟主机
实例
1) 配置反向代理
修改客户端hosts文件
C:\Windows\System32\drivers\etc\hosts
添加 IP地址 网址
192.168.199.100 www.test.com
配置nginx
pc端访问www.test.com 即访问百度
2) 配置正则 对应跳转不同页面
server {
listen 80;
server_name 192.168.199.103;
location ~ /bd/ {
proxy_pass http://www.baidu.com;
}
location ~ /by/ {
proxy_pass http://cn.bing.com;
}
location [ = | ~ | ~* |^~] url {
}
1. = : 用于不包含正则表达式的uri前,要求与uri严格匹配.
2. ~: 用于表示uri 包含正则表达式,并且区分大小写
3. ~\* : 用于表示uri包含正则表达式,并且不区分大小写*
4. ^~: 用于不包含正则表达式的uri前, 服务器找到表示uri和请求字符串最高的location后,立即使用此location处理请求,而不是在使用location块中的正则uri和请求字符串做匹配.
注意: 如果 uri包含正则表达式,则必须要有~ 或 ~\*
实例二 负载均衡
http {
# 配置负载均衡 添加 upstream 服务器名
upstream myservername{
# 服务器地址端口号
server 192.168.199.103:80;
server 192.168.199.104:80;
}
server {
listen 80;
server_name 192.168.199.103;
location / {
#/ 添加 proxy_pass http;// 服务器名
proxy_pass http://myservername;
root html;
index index.html index.htm;
}
}
负载方式:
1)轮询 默认 后台服务器down时自动剔除.
2.weight weight代表权重默认唯一,权重越高,分配的客户端越多
upstream myservername{
# 服务器地址端口号 增加权重
server 192.168.199.103:80 weigth=5;
server 192.168.199.104:80 weight=10;
}
3. ip_hash 按照访问ip的hash结果分配,每个访客访问固定的一个服务器,可以解决session问题.
upstream myservername{
ip_hash;
# 服务器地址端口号 增加权重
server 192.168.199.103:80;
server 192.168.199.104:80;
}
4. fair(第三方) 按照后端服务器效应时间来分配,响应越快分配的客户越多
3) 配置动静分离
# 静态资源访问uri
location /image/ {
root /usr/local/; #/静态资源根目录
autoindex on ; #列出当前文件夹
}
例2:
建立文件夹data/www/
修改nginx.conf文件
location /www/ {
root data/; #注意dtat前无/ 有/默认为绝对路径 或写为/usr/local/nginx/data/
index index.html index.htm;
}
查看
补充:
alias与root的区别
root 实际访问文件路径会拼接URL中的路径
alias 实际访问文件路径不会拼接URL中的路径
示例如下:
location /www/ {
alias /usr/local/nginx/html/;
}
请求:http://test.com/www/t1.html
实际访问:/usr/local/nginx/html/t1.html 文件
location /www/ {
root /usr/local/nginx/html/;
}
请求:http://test.com/www/t1.html
实际访问:/usr/local/nginx/html/www/t1.html 文件
4) 高可用性配置(防止单台nginx宕机)
安装keepalived
[root@localhost usr]# yum -y install keepalived
相关配置文件
/etc/keepalived/keepalived.conf
开启 keepalived
systemctl start keepalived
vim /usr/local/nginx/conf/nginx.conf
修改配置
http {
# 添加 配置负载均衡
upstream mysername {
ip_hash;
server 192.168.199.5:80;
server 192.168.199.4:80;
}
#配置虚拟主机监听
server {
listen 80;
server_name 192.168.199.100; #此处配置为虚拟主机的ip地址
location / {
root html;
proxy_pass http://mysername;
index index.html index.htm;
}
}
# 原始主机 配置
server {
listen 80;
server_name 192.168.199.103; #修改
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://mysername; #添加
root html;
index index.html index.htm;
}
配置keepalived.conf 文件
vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
root@localhost.localdomain
}
notification_email_from notify@keepalived.com
smtp_server 192.168.199.103
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/etc/keepalived/ch_nginx.sh" #监控脚本
interval 2 # 监控时间
weight -20 # 权重 服务器宕机 优先级将降低多少
}
vrrp_instance VI_1 {
state MASTER # 设置为 主服务器
interface ens33 # 配置监控网卡
virtual_router_id 51 #主 备服务器必须配置相同的id值
priority 120 # 权重值 MASTRE 一定要高于 BAUCKUP
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port # 执行监控的服务
}
virtual_ipaddress {
192.168.199.100 # VRRP 地址
}
}
配置监听脚本
vim /etc/keepalived/ch_nginx.sh
#!/bin/bash
A=`ps -C nginx --no-header |wc -l` ## 查看是否有 nginx进程 把值赋给变量A
if [ $A -eq 0 ];then ## 如果没有进程值得为 零
echo 'nginx未开启'
/usr/local/nginx/sbin/nginx
sleep 3
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
echo '开启nginx失败--关闭keepalived'
/etc/init.d/keepalived stop ## 则结束 keepalived 进程
fi
else
echo 'nginx 为启动状态'
fi
脚本增加权限 chmod +x /etc/keepalived/ch_nginx.sh
测试脚本: bash /etc/keepalived/ch_nginx.sh
重启服务 查看进程
/usr/local/nginx/sbin/nginx -s reload
systemctl start keepalived
ps -ef |grep keepalived
ip a 查看网卡信息
nginx扩展
运行方式 master + work 进程 1个master 管理多个work。
worker_connections 最大连接数1024.
静态页面占用 2个worker_connection,反向代理占用4 个work_connection。
nginx最大并发数 work*worker_connections /2
反向代理并发数 work*worker_connections /4