Nginx finds static files (css, js, etc.) when accessing by IP but returns 404 on files when accessing by domain name

Viewed 23

I use Nginx as a reverse-proxy with default nginx.conf, no modifications applied and have static landing page published via nginx's static blocks feature. Landing page's config section is here:

server {
    listen 80;
    listen [::]:80;

    root /var/www/landing/html;
    server_name 111.222.333.444 example.com www.example.com;

    location / {
            index index.html
            try_files $uri $uri/ =404;
    }
}

The root directory has the following structure - inside the html there are exist subdirectories with static content:

html/
    css/
       *.css
    img/
       *.png, *.svg, etc.
    fonts/
       *.tff
    index.html

In the index.html itself I use following pattern to access the static files

<img src="img/picture.jpg">

which means that code look up for files in the directory 'img' that located in the same folder as index.html.

If I open the site in browser via IP address - http://111.222.333.444 - it works and all static files are loaded. If I open the site via domain name - http://example.com, www.example.com - nginx returns index.html itself, but all static files are not found and responded with 404 code.

Could you guide me, please, what may I missing here?

0 Answers
Related