Score:0

Unable to serve static files using Nginx server (Nginx Error 403 Forbidden)

bs flag

I have Nginx 1.18.0 installed on Ubuntu 20.04. I am trying to serve (many) custom HTML files via Nginx. The HTML files contain animation of online images and mapping. Some HTML files also have associated JS and CSS files.

Instead of using defaults, I created a server block my_domain like this:

mkdir -p /var/www/my_domain/html
chown -R my_username /var/www/my_domain/html
chmod -R 755 /var/www/my_domain
ln -s /etc/nginx/sites-available/my_domain /etc/nginx/sites-enabled/

Then, as a test, I put my_html1.html (here, no associated JS/CSS) inside /var/www/my_domain/html and tried to access via Mozilla Firefox browser. I am getting 403 Error Forbidden.

I googled the error and tried suggestions provided here, but so far, no success. For example, I tried assigning the ownership of my_domain directory to www-data user, but didn't help.

chown -R www-data:www-data /etc/nginx/sites-available/my_domain

This is how my nginx.conf look like.

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
        worker_connections 768;
}
http {
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        server_names_hash_bucket_size 64;
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

and /etc/nginx/sites-available/my_domain

server {
        listen 80;

        root /var/www/my_domain/html;
        index my_html1.html;

        server_name my_domain www.my_domain;

        location / {
                try_files $uri $uri/ =404;
                autoindex on;
                autoindex_exact_size off;
        }
}

Can somebody help me to fix this issue?

drookie avatar
za flag
Answer is always in the `error.log`.
khajlk avatar
bs flag
The error log tells about permission denied, specifically, `*16 open() "/var/www/my_domain/html/my_html_file.html" failed (13: Permission denied), client: 127.0.0.1, server: my_domain, request: "GET /my_html_file.html HTTP/1.1",` but, i guess, this is also clear from browser's 403 error.
drookie avatar
za flag
Then nginx merely lacks *read/execute* permission on directories and *read* on files.
khajlk avatar
bs flag
Did you mean user `www-data` lacks these permissions? so, `chown 755 -R www-data path/to/directory` is this what i need?
khajlk avatar
bs flag
I am being careful in giving unnecessary permissions and want to avoid chmod 777. Care to post the suggested answer?
drookie avatar
za flag
Nah. It's way below basic level.
khajlk avatar
bs flag
and what's the basic level that is needed?
drookie avatar
za flag
The one that comes after reading one random book about UNIX (probably not until it's end) and googling your question (probably hitting the button *Feeling lucky*).
djdomi avatar
za flag
please share the output of `ls -Rl var/www/my_domain/html/`
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.