nginx常用命令--学习笔记
- macos 报错
nginx: [error] open() "/usr/local/var/run/nginx.pid" failed (2: No such file or directory)
执行 nginx -c /usr/local/etc/nginx/nginx.conf
常用命令
cd /usr/local/nginx/sbin #安装目录
#macos /usr/local/Cellar/nginx/1.19.0
./nginx -v # 查看版本
./nginx #启动 nginx
./nginx -t #测试配置文件是否有语法错误
./nginx -s reopen #重启Nginx
./nginx -s reload #重新加载Nginx配置文件,然后以优雅的方式重启Nginx
./nginx -s stop #强制停止Nginx服务
./nginx -s quit #优雅地停止Nginx服务(即处理完所有请求后再停止服务)
配置文件
cd /usr/local/nginx/conf
#macos /usr/local/etc/nginx/nginx.conf
#docker中的 /etc/nginx/nginx.conf
vim nginx.conf
worker_processes 1; # 并发处理的关键配置,值越大,支持并发处理量也越多,但是会受到硬件、软件等设备的制约
worker_connections 1024; #最大连接数
样例1
server {
listen 80;
server_name 192.168.1.200;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
proxy_pass http://127.0.0.1:8080;
index index.html index.htm;
}
location语法规则: location [=|~|~*|^~] /uri/ { … }
- = 开头表示精确匹配
- ^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可
- ~ 表示区分大小写的正则匹配
- ~* 表示不区分大小写的正则匹配
- !~和 !~* 分别为区分大小写不匹配及不区分大小写不匹配 的正则
- / 通用匹配,任何请求都会匹配到。
多个location配置的情况下匹配顺序为(当有匹配成功时候,停止匹配,按当前匹配规则处理请求):
- 优先匹配 =
- 其次匹配 ^~
- 按照文件中的匹配顺序执行
- 最后匹配 /
样例2
server {
listen 9001;
# listen somename:8080;
server_name 192.168.1.200;
location ~ /edu/ {
proxy_pass http://192.168.1.200:8080;
}
location ~ /vod/ {
proxy_pass http://192.168.1.200:8081;
}
}
- 负载均衡 样例 默认轮询策略
http {
...
upstream myserver {
server 192.168.1.200:8080;
server 192.168.1.200:8081;
}
server {
listen 80;
server_name 192.168.1.200;
location / {
root html;
proxy_pass http://myserver;
index index.html index.htm;
}
...
- 权重策略
upstream myserver {
server 192.168.1.200:8080 weight=5; #权重默认是1
server 192.168.1.200:8081 weight=10;
}
- hash策略
upstream myserver {
ip_hash;
server 192.168.1.200:8080;
server 192.168.1.200:8081;
}
- 第三方策略(根据服务器响应时间来分配,响应时间短的优先分配)
upstream myserver {
server 192.168.1.200:8080;
server 192.168.1.200:8081;
fair;
}
动静分离
server {
listen 80;
server_name 192.168.1.200;
location /www/ {
root /data/;
index index.html index.htm;
}
location /image/ {
root /data/;
autoindex on; #列出文件目录
}
高可用
keepalived.conf
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.17.129
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/usr/local/src/nginx_check.sh"
interval 2 #(检测脚本执行的间隔)
weight 2
}
vrrp_instance VI_1 {
state MASTER # 备份服务器上将 MASTER 改为 BACKUP
interface eth0 //网卡
virtual_router_id 51 # 主、备机的 virtual_router_id 必须相同
priority 100 # 主、备机取不同的优先级,主机值较大,备份机值较小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.17.50 // VRRP H 虚拟地址
}
}
nginx_check.sh
#!/bin/bash
A=`ps -C nginx – no-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
macos 配置端口转发
server {
listen 80;
server_name web.aaa.com;
location / {
proxy_pass http://localhost:8080;
}
}

浙公网安备 33010602011771号