0

I am inside /root directory and I have a folder inside it called testfolder. Inside that folder I have a bunch of folders and subfolders which I want to host on the nginx server. I am running the following command to start my Nginx server:

docker run --name file-server -v $(pwd)/testfolder:/app -p 8080:80 -d nginx

/etc/nginx/sites-available/default file has the following contents:

    location /testfolder {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
            alias /root/testfolder/;
            autoindex on;
    try_files $uri $uri/ =404;
}

Now when I start my server and hit /testfolder, It gives me a 403 error

Demon
  • 1
  • 1

1 Answers1

1

Serving static files using nginx as web server is a good option.

For making the static files available you need to copy your testfolder to /usr/share/nginx/html inside the nginx image. After which you will be able to see the files on your browser on port 8080.

Docker cmd:-

docker run -it --rm -d -p 8080:80 --name web -v ~/code/docker/testfolder:/usr/share/nginx/html nginx

For accessing the directory in list view for static files, we need to create a custom nginx conf file and pass it to the nginx container.

Ex:- Docker command:-

docker run -it --rm -d -p 8080:80 --name web -v ~/code/nginx-static:/usr/share/nginx/html -v ~/code/nginx-static/default.conf:/etc/nginx/conf.d/default.conf nginx

default.conf:-

server{
    listen 80 default_server;
    listen [::]:80 default_server;
    location / {
        autoindex  on;
        root /usr/share/nginx/html;
    }
}

  • I tried -v option to copy into /usr/share/nginx/html, when I do that, I get Forbidden 403 error in the server – Demon Mar 04 '21 at 05:21
  • Hey @Demon, I hope you are trying to access server files and not directory itself. Example:- I hosted a static folder containing one image inside it and copied it to `usr/share/nginx/html`. Now when I try to access my image file I will be able to access it. (http://localhost:8080/static/image.png). But I would not be able to access the static directory directly. (http://localhost:8080/static) --> this would give me 403 error. I hope this will help. – abhishek rana Mar 04 '21 at 06:24
  • I need to access the directory itself. – Demon Mar 04 '21 at 12:23
  • @Demon I have updated the answer with directory access too. Please have a look – abhishek rana Mar 05 '21 at 06:40