Nginx: Complete server path being inserted before images, CSS, JS...

December 27, 2013 2.2k views
I'm running Nginx on CentOS. My primary site is running as it should be on the site - no issues there. However, I've created a demo site for a client and I am running it in a sub-directory under mydomain.com/sites/clientsite/ and see the PHP/HTML no problem but anything related to images, CSS or JS is having the full root server path inserted before it (e.g. mydomain.com/usr/share/nginx/html/sites/clientsite/bg.jpg). I'm certain this is related to Nginx configuration file but I've modified it several times and restarted with no luck. Has anyone else run into this issue?
4 Answers
Please pastebin your nginx config, is your app generating the asset links or are they hardcoded in?
I thought the same about the hardcoding but it is only happening on my staging server here at Digital Ocean and not on the production environment. Here is my default.conf:

#
# The default server
#
server {
listen 80;
server_name mydomain.com;


location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}

error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

_______________

I assumed it was the "root /usr/share/nginx/html" towards the end but after commenting it out, it still does the same thing. Here is my virtual.conf (ip has been changed for anonymity):

_______________
#
# A virtual host using mix of IP-, name-, and port-based configuration
#

server {
# listen 80;
listen 12.34.55.78:80;
server_name mydomain.com www.mydomain.com;

location / {
root /usr/share/nginx/html/;
index index.php index.html index.htm;


#location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
# expires 365d;

#}

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

}
}
I think I see the problem... Nginx doesn't recognize .htaccess files.
I think I see the problem... Nginx doesn't recognize .htaccess files.

Nope, see http://wiki.nginx.org/LikeApache-htaccess.

Have you managed to get it working?
Have another answer? Share your knowledge.