Docker-alpine-nginx

alpine 安装nginx

1
2
3
4
5
apk update
#apk add nginx #安装
apk add nginx-mod-rtmp #安装带rtmp插件的nginx
ps aux | grep nginx #查看是否运行
vi /etc/nginx/nginx.conf #修改配置文件

问题1 nginx: [emerg] open() "/run/nginx/nginx.pid" failed (2: No such file or directory)

解决:mkdir /var/run/nginx

问题2 nginx: [emerg] unknown directive "rtmp" in /etc/nginx/nginx.conf:16

解决:在/etc/nginx/nginx.conf添加include /etc/nginx/modules/*.conf;

nginx常用调试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#查看ngixn是否启动
ps -ef|grep nginx
#查看错误日志,需要开启error_log /var/log/nginx/error.log warn;
cat /var/log/nginx/error.log
#重新加载配置
nginx -s reload
#重启nginx
nginx -s reopen
#停止nginx
nginx -s stop
#启动nginx
nginx
#测试配置文件语法问题
nginx -t

nginx路径配置解释

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
location /test {        
root /tmp/video;
}
#用http://<url>/test/...访问的文件地址为/tmp/video/test
location /video {
root /tmp/video;
}
#用http://<url>/video/...访问的文件地址为/tmp/video/video
location / {
root /tmp/video;
}
#用http://<url>/...访问的文件地址为/tmp/video/


location /test/ {
proxy_pass http://127.0.0.1:9266/;
}
#访问地址 http://<url>/test/....转发地址 http://127.0.0.1:9266/...

location /test {
proxy_pass http://127.0.0.1:9266/;
}
#访问地址 http://<url>/test/....转发地址 http://127.0.0.1:9266//...

location /test/ {
proxy_pass http://127.0.0.1:9266;
}
#访问地址 http://<url>/test/....转发地址 http://127.0.0.1:9266/test/...

常用配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
http {                                    
include mime.types;
default_type application/octet-stream;

#access_log logs/access.log main;

sendfile on;

keepalive_timeout 65;

#gzip on;

server {
listen 8888;
server_name localhost;
location / {
root /tmp/hikvision/video;
}
}

}

nginx 代理

参考 简明 Nginx Location Url 配置笔记

  • 正则匹配(~),URL包含weather都会走代理

    1
    2
    3
    location ~ /weather/ {
    proxy_pass http://apicloud.mob.com;
    }
  • 前缀匹配(^~),前缀是/v1/weather/开头的才走代理

    1
    2
    3
    location ^~ /v1/weather/ {
    proxy_pass http://apicloud.mob.com;
    }
  • 精确匹配(=),URL是/demo 多了少了都不行,才能进代理

    1
    2
    3
    location = /demo/ {
    proxy_pass http://apicloud.mob.com;
    }