Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serve different files for different accept headers #11

Open
b1rger opened this issue Jan 16, 2025 · 0 comments
Open

serve different files for different accept headers #11

b1rger opened this issue Jan 16, 2025 · 0 comments
Assignees

Comments

@b1rger
Copy link
Member

b1rger commented Jan 16, 2025

server {
    listen 80;
    server_name example.com;

    root /path/to/your/files;

    # Default location block
    location / {
        # Extract MIME type from the Accept header and map it to a file extension
        set $file_ext "";
        if ($http_accept ~* "text/html") {
            set $file_ext "html";
        }
        if ($http_accept ~* "text/turtle") {
            set $file_ext "ttl";
        }
        if ($http_accept ~* "application/json") {
            set $file_ext "json";
        }

        # Try to serve a file with the determined extension or fall back to a 404
        try_files $uri.$file_ext $uri $uri/ =404;

        # Set the Content-Type header based on the Accept header
        if ($file_ext = "html") {
            add_header Content-Type "text/html";
        }
        if ($file_ext = "ttl") {
            add_header Content-Type "text/turtle";
        }
        if ($file_ext = "json") {
            add_header Content-Type "application/json";
        }
    }
}
@b1rger b1rger self-assigned this Jan 16, 2025
@b1rger b1rger closed this as completed Jan 16, 2025
@b1rger b1rger reopened this Jan 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant