1

Currently, I'm trying to mirror a WordPress website, but getting some issues with nginx. The files (specifically js and css) are saved with ?ver=key on the end of the files so when nginx tries to serve them it serves a 404 error.

Here is an example file: wp-embed.min.js?ver=cce7ff2a6851b83ee00fd3873407f90c

How can I have nginx serve this file without giving a 404 error? I do not want to rename all these files as there are thousands of them. Here's my nginx config:

server {

    server_name [redacted];

    root /home/[redacted];
    index index.html index.php;

  location / {
        try_files $uri $uri/ =404;
    }
  location ~ \.php$ {
        include snippets/fastcgi-php.conf;
    #
    #   # With php7.0-cgi alone:
    #   fastcgi_pass 127.0.0.1:9000;
        # With php7.0-fpm:
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }


    listen [::]:443 ssl; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/[redacted]/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/[redacted]/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = [redacted]) {
        return 301 https://$host$request_uri;
    } # managed by Certbot



    server_name [redacted]

    listen 80;
    listen [::]:80;
    return 404; # managed by Certbot


}
3
  • 2
    Try and keep your tags specific, this has nothing to do with html / css or javascript stackoverflow.com/help/tagging Commented Dec 14, 2018 at 17:07
  • @Keith Apologizes, I'm serving those types of files so it is slightly related. Commented Dec 14, 2018 at 17:09
  • Problem is with those tags, people who are filtering for say Javascript, are now getting this question, and Javascript knowledge is of no use for setting up an nginx server. Same goes for html & css, I've left linux in here, as that potentially does have a bearing on configuring an ngnix server. Commented Dec 14, 2018 at 17:14

1 Answer 1

4

Using try_files, you can add another term such as $uri?$args which will look for a filename containing a literal ? followed by the query string.

One problem is that Nginx is confused as to the file extension and cannot determine the correct Content-Type. You can correct this by creating specific locations for each file type.

For example:

location / {
    try_files $uri $uri/ =404;
}

location ~ \.css$ {
    types {}
    default_type  text/css;
    try_files $uri $uri?$args =404;
}

location ~ \.js$ {
    types {}
    default_type  application/javascript;
    try_files $uri $uri?$args =404;
}

location ~ \.php$ {
    ...
}
Sign up to request clarification or add additional context in comments.

4 Comments

I've tried that, but now I get these errors in my console: Resource interpreted as Stylesheet but transferred with MIME type application/octet-stream:
That's weird as the MIME type is set explicitly in the location block. I cut&paste my answer and it seems to work correctly. Try: curl -I "https://example.com/wp-embed.min.js?ver=cce7ff2a6851b83ee00fd3873407f90c" to make sure that the browser's telling the truth.
Gives me this still content-type: application/octet-stream... With the nginx location block, do I need to add something on the end to include that the file may have get params?
dont work, for me, im also trying to do this but dont work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.