Home >Web Front-end >CSS Tutorial >Why Isn\'t Nginx Loading My CSS Files?
Nginx Fails to Load CSS Files: A Troubleshooting Guide
When switching from Apache2 to Nginx, users may encounter an issue where CSS files fail to load. This can result in an error message similar to:
Error: The stylesheet http://example.com/style.css was not loaded because its MIME type, "text/html", is not "text/css".
While the MIME type is correctly configured in /etc/nginx/mime.types, the issue may still persist. This is typically due to the location of the include /etc/nginx/mime.types; directive.
Correct Configuration
To resolve the issue, ensure that the include directive is placed under the correct location block:
http { ... # Include MIME types from /etc/nginx/mime.types location / { include /etc/nginx/mime.types; ... } }
Incorrect Configuration
Avoid placing the include directive under the global http block:
http { # Incorrect: Include MIME types globally include /etc/nginx/mime.types; ... }
By placing the include directive under the specific location block, Nginx will correctly read and apply the MIME types for that location. This will ensure that CSS files are loaded properly and the website displays correctly.
The above is the detailed content of Why Isn\'t Nginx Loading My CSS Files?. For more information, please follow other related articles on the PHP Chinese website!