Abiola Oyeniyi
Abiola Oyeniyi

Reputation: 55

Nginx is not serving static files using alias and root?

I have been trying to serve some static files using NGINX server but despite the configurations I haven't been able to get it running. I have tried using alias, root and even regex match - what could be missing?

/home/user/coka/staticfiles/ contains all the files i want to serve but whenever i visit http://127.0.0.1/staticfiles/file.css or http://example.com/staticfiles/file.css it is not showing up.

My configuration is shown below:

server {
    listen              443 ssl http2;
    listen              [::]:443 ssl http2;
    server_name         example.com;
   
    
    location /staticfiles/ {

        root /home/user/coka/staticfiles/;

        access_log /home/user/coka/logs/nginx-static-access.log;
        error_log /home/user/coka/logs/nginx-static-error.log;
    }
}

server {
    listen      80;
    listen      [::]:80;
    server_name example.com;
    return      301 https://example.com$request_uri;
}

I have been getting error 404.

Upvotes: 0

Views: 600

Answers (2)

Nick_Ning
Nick_Ning

Reputation: 56

It may be caused by the src url in your html file.

Use relative path with "./":

<link rel="stylesheet" href="./style.css">

instead of static "/":

<link rel="stylesheet" href="/style.css">

If you are using a build tool like Vite or Webpack, it usually can be set in config file with something like "base" option.

Upvotes: 0

IVO GELOV
IVO GELOV

Reputation: 14259

Can you try the following?

        ssl_session_cache shared:SSL:4m; # measured in megabytes, not minutes
        ssl_buffer_size 4k; # reduced from the default 16k to minimize TTFB
        ssl_session_timeout 60m;
        ssl_session_tickets off;
        ssl_dhparam /etc/ssl/nginx/dhparam.pem; # create with "openssl dhparam -out dhparam.pem 4096"
        ssl_ecdh_curve X25519:sect571r1:secp521r1:secp384r1;
        ssl_prefer_server_ciphers off;
        ssl_protocols TLSv1.3 TLSv1.2;
        ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384; 

        ssl_certificate /etc/ssl/chain.pem;
        ssl_certificate_key /etc/ssl/key.pem; 

server {
    listen              443 ssl http2;
    listen              [::]:443 ssl http2;
    server_name         example.com;
   
    root /home/user/coka;
    access_log /home/user/coka/logs/nginx-static-access.log;
    error_log /home/user/coka/logs/nginx-static-error.log;
}

server {
    listen      80;
    listen      [::]:80;
    server_name example.com;
    return      301 https://example.com$request_uri;
}

Upvotes: 1

Related Questions