Nginx基础配置
程序员文章站
2022-06-11 14:42:23
...
基本命令
Mac下安装Nginx
brew install nginx
查看版本号
nginx -v
启动nginx
nginx
关闭nginx
nginx -s stop
4、重新加载 nginx
nginx -s reload
nginx配置
nginx路径
# nginx配置文件目录
/usr/local/etc/nginx/
# nginx安装到的目录
/usr/local/Cellar/nginx
配置文件
全局块
- 配置服务器整体运行的配置指令 比如 worker_processes 1;处理并发数的配置
events块
- 影响 Nginx 服务器与用户的网络连接 比如 worker_connections 1024; 支持的最大连接数为 1024
http块
- 还包含两部分: http 全局块 server 块
功能
反向代理
简单代理
server {
listen 80;
server_name 192.168.10.59;
location / {
root html;
proxy_pass http://127.0.0.1:8080;
index index.html index.htm;
}
}
按照path进行代理(~区分大小写匹配,~*不区分大小写,=完全匹配)
server {
listen 9001;
server_name 192.168.10.59;
location ~ /dev/ {
proxy_pass http://127.0.0.1:8081;
}
location ~ /src/ {
proxy_pass http://127.0.0.1:8082;
}
}
负载均衡
设置服务器
upstream myserver{
server 192.168.10.59:8081 weight=1;
server 192.168.10.59:8082 weight=1;
}
分配方式
轮询(默认)
- 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除。
weight
- weight 代表权,重默认为 1,权重越高被分配的客户端越多
ip_hash
- 每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决 session 的问题。
upstream myserver{
ip_hash;
server 192.168.10.59:8081;
server 192.168.10.59:8082;
}
fair(第三方)
upstream myserver{
server 192.168.10.59:8081;
server 192.168.10.59:8082;
fair;
}
进行配置
server {
listen 80;
server_name 192.168.10.59;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
#加上自己定义的服务器组
proxy_pass http://myserver;
index index.html index.htm;
}
动静分离
location的root中加入要托管的目录,实现动静分离
server {
listen 8090;
server_name 127.0.0.1;
location / {
root /Users/liyi/works/repos/banyuan/pj-mall-admin-web/dist;
index index.html;
}
}
高可用的主从模式(keepalived)
安装
# yum install keepalived -y
启动
systemctl start keepalived //启动 keepalived
systemctl enable keepalived //加入开机自启动
keepalived配置文件
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1 # 服务器IP地址
smtp_connect_timeout 30
router_id LVS_DEVEL # 访问到主机,host中自定义的名字
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_http_port {
script "/user/local/src/nginx_check.sh" # 执行的脚本地址
interval 2 # 执行脚本的间隔时间
weight 2 # 权重
}
vrrp_instance VI_1 {
state MASTER # 标记服务器是主从服务器,MASTER是主,BACKUP是从
interface eth0 # 网卡
virtual_router_id 51 # id主备服务器必须相同
priority 100 # 主备服务器优先级,主机较大
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { # 虚拟ip地址
192.168.200.16
192.168.200.17
192.168.200.18
}
}
virtual_server 192.168.200.100 443 {
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.201.100 443 {
weight 1
SSL_GET {
url {
path /
digest ff20ad2481f97b1754ef3e12ecd3a9cc
}
url {
path /mrtg/
digest 9b3a0c85a887a256d6939da88aabd8cd
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
virtual_server 10.10.10.2 1358 {
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
sorry_server 192.168.200.200 1358
real_server 192.168.200.2 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl3/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.200.3 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334c
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334c
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
virtual_server 10.10.10.3 1358 {
delay_loop 3
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.200.4 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl3/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.200.5 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl3/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
执行脚本
#!/bin/bash
A=`ps -C nginx ¨Cno-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
上一篇: nginx基础配置