docker配置nginx踩坑---nginx配置文件问题

docker中安装的nginx,使用的是嵌套式的nginx配置,即主配置在/etc/nginx/nginx.conf中,然而平时用到的server配置在/etc/nginx/conf.d中两个配置文件如下:

nginx.conf


user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
worker_connections  1024;
}


http { 
include       /etc/nginx/mime.types;
default_type  application/octet-stream;
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

}

在底部include了另外一部分配置文件,而这部分配置文件就是基本的server配置,默认模板如下:

server {
    listen       80;
    server_name  localhost;
 
    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
 
    location / {
        root   /usr/nginx/dacheng-wechat-web;
       # root   /usr/nginx/html;
        index  index.html index.htm;
        autoindex  on;
	try_files $uri /index/index/page.html;
        #try_files $uri /index/map/page.html;
    }
 
    #error_page  404              /404.html;
 
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
 
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}
 
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}
 
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
 

此处按照nginx配置文件中的server标准配置location配置路由以及页面路径(location / 这个/就是页面访问路径,此处为根路径。location / {}大括号内配置详细页面路径,root后面跟页面目录,index后面跟默认显示页面)。

此处要特别注意,页面目录不能使用自己主机的目录而要使用docker安装的nginx下的目录配置,因为这个配置文件最终会被docker容器解析给nginx所以应该配置docker的nginx容器下的路径,然后再使用docker容器构建时候的目录映射功能将docker下的nginx目录映射到自己主机上操作。

因为此处只做示例作用就没有将nginx.conf映射出来,容器构建代码如下:

docker run 
-p 80:80 
--name mynginx 
-v D:\Docker\nginx\www:/www 
-v D:\Docker\nginx\conf.d:/etc/nginx/conf.d 
-v D:\Docker\nginx\wwwlogs:/wwwlogs 
-d nginx

构建之后的conf.d文件夹中存储server配置信息,即:

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /www/dist/stategrid;
        index  index.html;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

此处location配置root页面路径时前面/www为docker内路径,而因为映射到了外部主机上,所以通过/www即可访问外部主机资源/dist/stategrid(docker内路径/www,自己主机资源存储路径D:\Docker\nginx\www\dist\stategrid)

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在 Docker 中部署配置 Nginx-RTMP,可以按照以下步骤进行操作: 1. 创建一个 Dockerfile 文件,以便构建自己的 Nginx 镜像。Dockerfile 内容如下: ``` FROM nginx:latest RUN apt-get update && apt-get install -y wget build-essential libpcre3 libpcre3-dev libssl-dev zlib1g-dev RUN wget https://github.com/arut/nginx-rtmp-module/archive/master.zip && \ unzip master.zip RUN wget http://nginx.org/download/nginx-1.19.1.tar.gz && \ tar -zxvf nginx-1.19.1.tar.gz && \ cd nginx-1.19.1 && \ ./configure --with-http_ssl_module --add-module=../nginx-rtmp-module-master && \ make && \ make install COPY nginx.conf /usr/local/nginx/conf/ ``` 2. 在 Dockerfile 目录中创建一个名为 nginx.conf 的 Nginx 配置文件,内容如下: ``` worker_processes 1; events { worker_connections 1024; } rtmp { server { listen 1935; chunk_size 4096; application live { live on; record off; } application hls { live on; hls on; hls_path /tmp/hls; } } } http { server { listen 80; location /hls { types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; } root /tmp; add_header Cache-Control no-cache; } } } ``` 3. 构建 Docker 镜像,执行以下命令: ``` docker build -t nginx-rtmp . ``` 4. 运行 Docker 容器,执行以下命令: ``` docker run -d -p 1935:1935 -p 80:80 nginx-rtmp ``` 这将会在 Docker 容器中运行 Nginx,并将 RTMP 端口映射到主机的 1935 端口,将 HTTP 端口映射到主机的 80 端口。 现在,您可以使用 OBS 或其他 RTMP 客户端将视频流发送到 RTMP://<your-ip-address>:1935/live,并使用 VLC 播放器或其他支持 HLS 的浏览器,在 http://<your-ip-address>/hls 中查看视频。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值