Nginx: CSS and JS files inside the wordpress blog directory are served wrong

nginxphp-fpm

I set up an Amazon ec2 LEMP server for my photography website, which previously was on apache, which I'm a lot more familiar with.

I have everything running generally ok, except for in the blog directory. The CSS and JS files seem to be served by PHP and have content type text/html, for example here are the response headers for my theme's stylesheet (/blog/wp-content/themes/twentyseventeen/style.css?ver=4.9.8):

content-type: text/html
date: Fri, 26 Oct 2018 02:33:26 GMT
server: nginx/1.12.2
status: 200
x-powered-by: PHP/5.4.16

vs the headers for my own stylesheet (/include/css/style.css):

accept-ranges: bytes
cache-control: max-age=315360000
content-length: 34199
content-type: text/css
date: Fri, 26 Oct 2018 02:48:04 GMT
etag: "5b7f653b-8597"
expires: Thu, 31 Dec 2037 23:55:55 GMT
last-modified: Fri, 24 Aug 2018 01:54:03 GMT
server: nginx/1.12.2
status: 200

I've read lots of threads that deal with very similar problems. However, I'm confused because my problem is confined to the /blog/ directory.

A few of the other questions/answers I read mentioned security.limit_extensions and indeed mine (/etc/php-fpm.d/www.conf) was set up like so:

security.limit_extensions =
;security.limit_extensions = .php .php3 .php4 .php5 .ttf

I changed it:

;security.limit_extensions =
security.limit_extensions = .php .php3 .php4 .php5 .ttf

and restarted nginx via service nginx restart – but the problem still persists..

Can't imagine what I'm missing.. Ready to throw in the towel and switch back to apache.. 🙁

Anyone see what I missed?

UPDATE: Config files

/etc/nginx/nginx.conf:

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/
#user ec2-user;

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    server_names_hash_bucket_size 64;


    client_max_body_size 2M;

    include             mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    include /etc/nginx/sites-enabled/*;
}

/etc/nginx/sites-available/mikewillisphotography.com.conf

server {
    listen 80 default_server;
    server_name www.mikewillisphotography.com mikewillisphotography.com;
    return 301 https://www.mikewillisphotography.com$request_uri;
}

server {
    listen 443 ssl http2;
    server_name mikewillisphotography.com;
    return 301 https://www.mikewillisphotography.com$request_uri;
}

server {
    listen       443 ssl default_server;
    server_name  www.mikewillisphotography.com;
    #server_name localhost;

    include /etc/nginx/sites-available/includes/restrictions.conf;
    include /etc/nginx/sites-available/includes/wordpress.conf;
    #       include /etc/nginx/sites-available/includes/php.conf;

    ssl_certificate /etc/letsencrypt/live/mikewillisphotography.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mikewillisphotography.com/privkey.pem;

    location /.well-known/acme-challenge {
        #root /var/www/html/letsencrypt/wordpress/;
        root /usr/share/nginx/sites/mikewillisphotography.com/htdocs/letsencrypt/wordpress/;
    }

    client_max_body_size 2M;

    # note that these lines are originally from the "location /" block
    root   /usr/share/nginx/sites/mikewillisphotography.com/htdocs;
    index index.php index.html index.htm;

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    location = /50x.html {
        root /usr/share/nginx/sites/mikewillisphotography.com/htdocs;
    }

    location ~ \.php$ {
        include /etc/nginx/sites-available/includes/php.conf;
    }
}

/etc/nginx/sites-available/includes/php.conf

fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;

#wordpress stuff
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;

/etc/nginx/sites-available/includes/wordpress.conf

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires max;
    log_not_found off;
}

location ^~ /blog {
    root /usr/share/nginx/sites/mikewillisphotography.com/htdocs;
    index index.php index.html index.htm;
    include /etc/nginx/sites-available/includes/php.conf;
    rewrite /wp-admin$ $scheme://$host$uri/index.php?q=$1 permanent;
    try_files $uri $uri/ @blog;
}

location @blog {
    rewrite ^/blog(.*) /blog/index.php?q=$1;
}

Best Answer

I found the problem - I hadn't checked the wordpress.conf file and sure enough it was including the php.conf file for every request under the /blog/ directory.

location ^~ /blog {
    root /usr/share/nginx/sites/mikewillisphotography.com/htdocs;
    index index.php index.html index.htm;
    include /etc/nginx/sites-available/includes/php.conf;
    rewrite /wp-admin$ $scheme://$host$uri/index.php?q=$1 permanent;
    try_files $uri $uri/ @blog;
}

I changed it to use a nested location block to catch .php files, which solved the problem. Not sure if this is the most efficient method, but it works.

location ^~ /blog {
    root /usr/share/nginx/sites/mikewillisphotography.com/htdocs;
    index index.php index.html index.htm;
    rewrite /wp-admin$ $scheme://$host$uri/index.php?q=$1 permanent;
    try_files $uri $uri/ @blog;

    location ~ \.php$ {
        include /etc/nginx/sites-available/includes/php.conf;
    }
}