美文网首页
【Docker学习】在Docker下安装NGINX,通过upst

【Docker学习】在Docker下安装NGINX,通过upst

作者: 伊丽莎白2015 | 来源:发表于2022-09-14 21:32 被阅读0次

【资源】

1. 需求

本地用IntelliJ运行了一个程序,配置了两个port,启动后可以访问:

想要安装一个nginx,然后通过http:<nginx ip>:<nginx port>/hello,指向上述的两个地址,实现轮循访问5010和5011。

2. 安装docker nginx

参考:https://www.runoob.com/docker/docker-install-nginx.html

运行容器:

docker run --name nginx-test -p 8080:80 -d nginx

检查:

docker ps

image.png

访问:http://localhost:8080,安装成功:

image.png

3. 配置nginx

在#2的基础上,进入容器内部:

docker exec -it <containerID> bash

进入后,nginx.conf位于:

cd /etc/nginx

我们可以复制一个sample出来,放到本地。

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
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 /etc/nginx/conf.d/*.conf,可以看到在nginx内有default.conf


image.png

有两个选择:

  • 可以直接在nginx.conf中配置server信息
  • 也可以在conf.d下配置各自的server,命名需要以conf结尾。

我先择直接在nginx.conf中配置:
注:这里的upstream中需要通过ifconfig确定宿主机的ip,如果直接使用localhost,docker中的nginx访问不了。

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
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;

    upstream test_session {
       server 192.168.24.1:5010;
       server 192.168.24.1:5011;
    }

    server {
        listen       80 ;
        server_name  localhost;
        location / {
            proxy_pass http://test_session;
            proxy_set_header Host $host;
        }
    }
}

再次启动nginx(可以先删除2中的container):
我们需要将本地的nginx.conf挂载到docker容器内部,本次port用的是5015(而不是8080):

docker run --name nginx-test -v /Users/xxx/docker-nginx/test/nginx.conf:/etc/nginx/nginx.conf -p 5015:80 -d nginx

测试:
访问:http://localhost:5015/hello,可以看到正常返回结果。

image.png

4. 遇到的问题

4.1 网络问题

如果不确定nginx中能否连通宿主机的程序,可以在使用docker exec -it <container id> bash进入容器后,执行curl http://localhost:5010/hello,不出意外会报connection refused。因为docker中的nginx认为的localhost是它自己,而不是宿主机。

4.2 访问http://localhost:5015/hello 报404

原因是我在配置的时候,在nginx.conf中还配置了include /etc/nginx/conf.d/*.conf;,然后发现这个目录下有default.conf文件,它配了一个默认的server,路径也是"/",它会导致我在nginx.conf中server配置失效!!!

4.3 访问http://localhost:5015/hello 报400

原因是我在配置nginx.conf的location的时候,只配置了一个proxy_pass,后来参考了文章:https://blog.csdn.net/liuxiao723846/article/details/125688050,发现需要配置proxy_set_header Host $host,重新启动即可!!!

        location / {
            proxy_pass http://test_session;
        }

相关文章

网友评论

      本文标题:【Docker学习】在Docker下安装NGINX,通过upst

      本文链接:https://www.haomeiwen.com/subject/sielortx.html