Gonzalo Hernandez
Gonzalo Hernandez

Reputation: 737

Nginx setting wrong content type

I have an online web server where I have deployed multiple applications and they all have to share the domain, so my nginx config is a bit messy. The last time I changed it, the PHP files didn't load getting the error ("access denied to file") so I had to do some changes, now the config for one of the applications is like this:

location ^~ /vuelos_baratos {      
   root /home/gonzalo;
   try_files $uri $uri/ /$uri/index.php /index.php$is_args$args $uri/index.php =404;
   #root /usr/share/nginx/html;
   fastcgi_split_path_info ^(.+\.php)(/.+)$;
   fastcgi_pass unix:/run/php/php7.0-fpm.sock;
   fastcgi_index index.php;
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   include /etc/nginx/fastcgi_params;
   include  /etc/nginx/mime.types;
   autoindex on;
}

and the urls of this application are like this:

mydomain.com/vuelos_baratos/index.php

mydomain.com/vuelos_baratos/style.css

... and so on.

And the PHP files and everything is working fine, except that If I try to acces:

mydomain.com/vuelos_baratos/image.png

I get the binary data of the image, I discovered that that is because the headers for all files under "vuelos_baratos" are set to type: text/html

How can I fix this?

Upvotes: 2

Views: 4215

Answers (1)

Richard Smith
Richard Smith

Reputation: 49722

I don't think that nginx is responsible for determining the content type in your current configuration, as you pass everything to php-fpm.

You could divide the static and dynamic sections into separate locations, for example:

location ^~ /vuelos_baratos {      
    include  /etc/nginx/mime.types;

    root /home/gonzalo;
    index index.php;

    try_files $uri $uri/ /index.php$is_args$args;

    location ~ \.php$ {
        try_files $uri =404;
        include /etc/nginx/fastcgi_params;

        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $request_filename;
    }
}

Which should mean that /vuelos_baratos/image.png is processed by nginx and that /etc/nginx/mime.types is used to determine the Content Type.

Upvotes: 3

Related Questions