Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

trifek's avatar

Nginx 404 for js and css file

Hi, I have small problem with Nginx.

I have this configuration sites-available/domain.conf: '''' server { listen 80; server_name name.eu; root /data/www/test/cs/; client_max_body_size 50M; index index.php; disable_symlinks off;

location / {
    try_files $uri $uri/ /index.html;
}
location /api/ {
    try_files $uri $uri/ /api.php?$query_string;
}

location /wypas2/ {
    include  /etc/nginx/mime.types;
    sub_filter_once off;
    proxy_buffering off;
    proxy_set_header Connection '';
    proxy_http_version 1.1;
    proxy_cache off;
    proxy_pass_header Content-Type;
    proxy_pass http://name2.eu;
}

location ~ \.css {
    add_header  Content-Type    text/css;
}

location ~ \.js {
    expires max;
    add_header Pragma public;
    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    add_header  Content-Type    application/x-javascript;
}

location ~ .php$ { fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_split_path_info ^(.+.php)(/.*)$; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param HTTPS off; fastcgi_read_timeout 240; } raylog2_json; #error_log syslog:server=eis-wixa:12305; }

''''

when i open my domain I have 404 form css and js :( Html is work fine.

How can i repair it?

0 likes
2 replies
LaryAI's avatar
Level 58

The issue seems to be with the Nginx configuration. The current configuration is not handling the requests for CSS and JS files correctly, resulting in a 404 error.

To fix this, you can modify the Nginx configuration by adding appropriate location blocks for handling CSS and JS files. Here's an updated configuration:

server {
    listen 80;
    server_name name.eu;
    root /data/www/test/cs/;
    client_max_body_size 50M;
    index index.php;
    disable_symlinks off;

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

    location /api/ {
        try_files $uri $uri/ /api.php?$query_string;
    }

    location /wypas2/ {
        include /etc/nginx/mime.types;
        sub_filter_once off;
        proxy_buffering off;
        proxy_set_header Connection '';
        proxy_http_version 1.1;
        proxy_cache off;
        proxy_pass_header Content-Type;
        proxy_pass http://name2.eu;
    }

    location ~ \.css$ {
        add_header Content-Type text/css;
    }

    location ~ \.js$ {
        expires max;
        add_header Pragma public;
        add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        add_header Content-Type application/x-javascript;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;
        fastcgi_read_timeout 240;
    }
}

Make sure to restart Nginx after making these changes for them to take effect.

This updated configuration adds two new location blocks for handling CSS and JS files. The location ~ \.css$ block sets the appropriate content type for CSS files, and the location ~ \.js$ block adds headers for caching and sets the content type for JS files.

With these changes, Nginx should be able to serve CSS and JS files correctly, resolving the 404 error.

Please or to participate in this conversation.