Home >Web Front-end >CSS Tutorial >Why Isn\'t Nginx Loading My CSS Files?

Why Isn\'t Nginx Loading My CSS Files?

DDD
DDDOriginal
2024-12-02 08:08:13909browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn