【问题标题】:404 error when Accessing from local host : NGINX uWSGI从本地主机访问时出现 404 错误:NGINX uWSGI
【发布时间】:2016-11-28 00:06:28
【问题描述】:

我正在尝试使用 nginx(在容器中运行)配置一个 docker compose 示例,该示例面向 3 个在 uwsgi 中运行 python 应用程序的容器。

当通过容器的 docker ip 尝试 nginx 时,应用程序可以正常访问;但是,当尝试使用 localhost:9999 从主机 url(nginx 绑定到使用端口 9999 的主机)访问它时,我可以访问 nginx 页面,但是任何应用程序 url 或 /admin/ uri 都会显示 404 错误。

我已经浏览了大量的论坛和 Stackoverflow 答案,但没有任何运气,有人可以帮我解决这个问题吗?

下面附上更多信息:

我的 docker compose 文件如下所示:

version:  '2'
services:
  nginxlb:
    build:  ./nginx
    ports:
      - "9999:80"
    links:
      - my-app1:my-app1
      - my-app2:my-app2
      - my-app3:my-app3
    volumes:
      - ./logs/nginx/:/opt/my-logs/nginx/
  my-app1:
    build: ./my
    expose:
      - 8080
      - 8081
    volumes:
      - ./logs/node1:/opt/my-logs/node1/
  my-app2:
    build: ./my
    volumes:
      - ./logs/node2/:/opt/my-logs/node2/
    expose:
      - 8080
      - 8081
  my-app3:
    build: ./my
    volumes:
      - ./logs/node3:/opt/my-logs/node3
    expose:
      - 8080
      - 8081

为 nginx 启用站点的 conf 如下所示:

upstream node-app {
      least_conn;
      server my-app1:8080;
      server my-app2:8080;
      server my-app3:8080;
}

server {
      listen 80 default;
      server_name _;
      location / {
        proxy_pass http://node-app;
        include /etc/nginx/uwsgi_params;
      }
}

编辑这是我的 NGINX Docker 文件我没有对默认的 nginx.conf 文件进行任何更改

From nginx

MAINTAINER  DevOps

# Update the container
RUN apt-get update

# Create directories and set working directory
RUN mkdir /opt/myapp/
RUN mkdir /opt/myapp/django-static/
RUN mkdir /opt/myapp/django-media/
RUN mkdir /opt/myapp/webroot/
run mkdir -p /opt/myapp-logs/nginx/
WORKDIR /opt/myapp/

# Copy configs and code to Container
ADD config/nginx.conf /etc/nginx/nginx.conf
ADD config/nginx-site.conf /etc/nginx/sites-enabled/nginx.conf
ADD config/uwsgi_params /etc/nginx/uwsgi_params

EXPOSE 80

【问题讨论】:

  • 404 表明 nginx 没有到达你的 server nginx 配置块;也许默认 nginx 配置文件中的参数会覆盖您的配置?您是否尝试删除它?如果这没有帮助,请发布您的 nginx/Dockerfile 的内容。
  • nginx 服务器通过 nginx 容器 ip 访问时提供 uwsgi 内容(通过 'sudo docker inspect | grep IPAddress' 获得。
  • 刚刚添加了我的 nginx Dockerfile,我在想我是否正在使用 compose 文件进行 docker 网络。

标签: django nginx docker uwsgi


【解决方案1】:

默认站点的 Nginx 配置文件包括以下行:

    server_name  localhost;

所以当你去http://localhost:9999时使用这个配置;另外 - nginx 不处理 /etc/nginx/sites-enabled 目录及其内容;从默认的nginx.conf:

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

修改您的 Dockerfile - 而不是:

ADD config/nginx-site.conf /etc/nginx/sites-enabled/nginx.conf

使用:

RUN rm /etc/nginx/conf.d/default.conf
ADD config/nginx-site.conf /etc/nginx/conf.d/nginx.conf

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-06
    • 2013-08-14
    • 2018-06-27
    • 2011-11-29
    • 2016-02-16
    • 2012-07-10
    • 1970-01-01
    • 2020-06-30
    相关资源
    最近更新 更多