nginx error 403 - directory index is forbidden

10,689

It looks like what might be going on is the default.conf file in the nginx image is taking over the / location. You nginx run command has:

-v $ROOT/web/flask/conf/nginx-default.conf:/etc/nginx/conf.d/default \

This should be overwriting the default.conf instead of just default. As it currently stands, it just adds another blank default file and leaves the default default.conf which has a location for /.

You static route does work because there is an explicit route in the nginx-flask.conf to /static and you call the file explicitly. You get a 403 on the / location because indexes are disabled by default (controlled by the autoindex option).

Share:
10,689
user706838
Author by

user706838

Updated on June 04, 2022

Comments

  • user706838
    user706838 almost 2 years

    I am trying to connect docker nginx with docker flask. Here is the structure of my project:

    .
    ├── storage
    │   ├── nginx
    │   │   └── static
    │   │       └── image.gif
    └── web
        └── flask
            ├── app
            │   ├── run.py
            │   └── templates
            │       └── index.html
            ├── conf
            │   ├── nginx-default.conf
            │   ├── nginx-flask.conf
            │   └── requirements.txt
            └── Dockerfile
    

    Although curl 127.0.0.1:50 and curl 127.0.0.1:80/static/image.gif work fine, I get a '403 Forbidden' error when I do curl 127.0.0.1. More specifically, nginx gives the following error:

    2016/03/05 17:54:37 [error] 8#8: *1 directory index of "/usr/share/nginx/html/" is forbidden, client: 172.17.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost"
    172.17.0.1 - - [05/Mar/2016:17:54:37 +0000] "GET / HTTP/1.1" 403 169 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0" "-"
    

    I create the containers like this:

    docker create \
    -v $ROOT/web/flask/app/:/web/flask/app/ \
    --name flask_data flask:0.1
    
    docker run \
    -d -p 127.0.0.1:50:50 \
    --volumes-from flask_data \
    --name flask_service flask:0.1
    
    docker create \
    -v $ROOT/storage/nginx/:/usr/share/nginx/html/ \
    --name nginx_data nginx:1.9
    
    docker run \
    -v $ROOT/web/flask/conf/nginx-flask.conf:/etc/nginx/conf.d/nginx-flask.conf \
    -v $ROOT/web/flask/conf/nginx-default.conf:/etc/nginx/conf.d/default \
    -d -p 127.0.0.1:80:80 \
    --volumes-from nginx_data \
    --link flask_service:flask_service_alias \
    --name nginx_service nginx:1.9
    

    where, Dockerfile is:

    FROM ubuntu:14.04
    
    RUN apt-get update && apt-get install -y --no-install-recommends python-pip python-dev
    
    COPY . /web/flask/
    
    RUN pip install -r /web/flask/conf/requirements.txt
    
    CMD gunicorn run:app --workers=4 --bind=0.0.0.0:50 --log-level=debug --timeout=43200 --chdir=/web/flask/app/
    

    and requirements.txt is:

    gunicorn==19.4.5
    Flask==0.10.1
    Flask-Redis==0.1.0
    Flask-SQLAlchemy==2.1
    Flask-MongoAlchemy==0.7.2
    

    nginx-default is empty, and nginx-flask is:

    server {
    
        listen          80;
        charset         utf-8;
    
        location /avatar {
            alias /usr/share/nginx/html/avatar;
        }
    
        location /scan {
            alias /usr/share/nginx/html/scan;
        }
    
        location /static {
            alias /usr/share/nginx/html/static;
        }
    
        location / {
            proxy_pass http://flask_service_alias:50/;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    
    }
    

    any ideas?

    • Mox
      Mox about 8 years
      did you check your file/directory permission?
    • user706838
      user706838 about 8 years
      sure. permissions of all files and (sub-) directories are set to drwxrwxr-x.
    • Mox
      Mox about 8 years
      /usr/share/nginx/html/ <-- including all the folders along this path ?
    • user706838
      user706838 about 8 years
      well, I mean the ones on the host machine (or in other words, all those listed above). the /usr/share/nginx/html/ refers to the nginx container, so no idea about this. I assume I shouldn't touch this, right?
    • Mox
      Mox about 8 years
      oh well you might give it a try by setting 755 for those folders too
    • user706838
      user706838 about 8 years
      ok. may I ask you to provide the required commands (access the docker and change the permissions)? I am not that confident with dockers, yet!
    • user706838
      user706838 about 8 years
      ok. I access it with docker exec -it nginx_service bash and I saw that folder html and all of its subdirectories are set to drwxrwxr-x 5 1000 1000. is this ok?
  • user706838
    user706838 about 8 years
    ohh dear. now I see. :) you are right. there are 3 files under /etc/nginx/conf.d/ inside the nginx container (not just two)! how can I start the nginx container so as to avoid this behaviour? thank you so much!